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

1.20      harris41    1: # The LearningOnline Network with CAPA
1.1       www         2: # Create a user
                      3: #
1.355   ! www         4: # $Id: loncreateuser.pm,v 1.354 2011/01/07 23:35:45 www 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.311     raeburn   114:                      krb5     => 'krb',
1.188     raeburn   115:                      krb4     => 'krb',
                    116:                      internal => 'int',
                    117:                      localuth => 'loc',
                    118:                      unix     => 'fsys',
                    119:                    );
                    120:     return %abv_auth;
                    121: }
1.43      www       122: 
1.134     raeburn   123: # ====================================================
                    124: 
                    125: sub portfolio_quota {
                    126:     my ($ccuname,$ccdomain) = @_;
                    127:     my %lt = &Apache::lonlocal::texthash(
1.267     raeburn   128:                    'usrt'      => "User Tools",
                    129:                    'disk'      => "Disk space allocated to user's portfolio files",
                    130:                    'cuqu'      => "Current quota",
                    131:                    'cust'      => "Custom quota",
                    132:                    'defa'      => "Default",
                    133:                    'chqu'      => "Change quota",
1.134     raeburn   134:     );
1.149     raeburn   135:     my ($currquota,$quotatype,$inststatus,$defquota) = 
                    136:         &Apache::loncommon::get_user_quota($ccuname,$ccdomain);
                    137:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($ccdomain);
                    138:     my ($longinsttype,$showquota,$custom_on,$custom_off,$defaultinfo);
                    139:     if ($inststatus ne '') {
                    140:         if ($usertypes->{$inststatus} ne '') {
                    141:             $longinsttype = $usertypes->{$inststatus};
                    142:         }
                    143:     }
                    144:     $custom_on = ' ';
                    145:     $custom_off = ' checked="checked" ';
                    146:     my $quota_javascript = <<"END_SCRIPT";
                    147: <script type="text/javascript">
1.301     bisitz    148: // <![CDATA[
1.149     raeburn   149: function quota_changes(caller) {
                    150:     if (caller == "custom") {
                    151:         if (document.cu.customquota[0].checked) {
                    152:             document.cu.portfolioquota.value = "";
                    153:         }
                    154:     }
                    155:     if (caller == "quota") {
                    156:         document.cu.customquota[1].checked = true;
                    157:     }
                    158: }
1.301     bisitz    159: // ]]>
1.149     raeburn   160: </script>
                    161: END_SCRIPT
                    162:     if ($quotatype eq 'custom') {
                    163:         $custom_on = $custom_off;
                    164:         $custom_off = ' ';
                    165:         $showquota = $currquota;
                    166:         if ($longinsttype eq '') {
1.230     bisitz    167:             $defaultinfo = &mt('For this user, the default quota would be [_1]'
                    168:                             .' Mb.',$defquota);
1.149     raeburn   169:         } else {
1.231     raeburn   170:             $defaultinfo = &mt("For this user, the default quota would be [_1]".
                    171:                                " Mb, as determined by the user's institutional".
                    172:                                " affiliation ([_2]).",$defquota,$longinsttype);
1.149     raeburn   173:         }
                    174:     } else {
                    175:         if ($longinsttype eq '') {
1.230     bisitz    176:             $defaultinfo = &mt('For this user, the default quota is [_1]'
                    177:                             .' Mb.',$defquota);
1.149     raeburn   178:         } else {
1.231     raeburn   179:             $defaultinfo = &mt("For this user, the default quota of [_1]".
                    180:                                " Mb, is determined by the user's institutional".
                    181:                                " affiliation ([_2]).",$defquota,$longinsttype);
1.149     raeburn   182:         }
                    183:     }
1.267     raeburn   184: 
                    185:     my $output = $quota_javascript."\n".
                    186:                  '<h3>'.$lt{'usrt'}.'</h3>'."\n".
                    187:                  &Apache::loncommon::start_data_table();
                    188: 
                    189:     if (&Apache::lonnet::allowed('mut',$ccdomain)) {
1.275     raeburn   190:         $output .= &build_tools_display($ccuname,$ccdomain,'tools');
1.267     raeburn   191:     }
                    192:     if (&Apache::lonnet::allowed('mpq',$ccdomain)) {
                    193:         $output .= '<tr class="LC_info_row">'."\n".
                    194:                    '    <td>'.$lt{'disk'}.'</td>'."\n".
                    195:                    '  </tr>'."\n".
                    196:                    &Apache::loncommon::start_data_table_row()."\n".
                    197:                    '  <td>'.$lt{'cuqu'}.': '.
                    198:                    $currquota.'&nbsp;Mb.&nbsp;&nbsp;'.
                    199:                    $defaultinfo.'</td>'."\n".
                    200:                    &Apache::loncommon::end_data_table_row()."\n".
                    201:                    &Apache::loncommon::start_data_table_row()."\n".
                    202:                    '  <td><span class="LC_nobreak">'.$lt{'chqu'}.
                    203:                    ': <label>'.
                    204:                    '<input type="radio" name="customquota" value="0" '.
                    205:                    $custom_off.' onchange="javascript:quota_changes('."'custom'".')"'.
                    206:                    ' />'.$lt{'defa'}.'&nbsp;('.$defquota.' Mb).</label>&nbsp;'.
                    207:                    '&nbsp;<label><input type="radio" name="customquota" value="1" '. 
                    208:                    $custom_on.'  onchange="javascript:quota_changes('."'custom'".')" />'.
                    209:                    $lt{'cust'}.':</label>&nbsp;'.
                    210:                    '<input type="text" name="portfolioquota" size ="5" value="'.
                    211:                    $showquota.'" onfocus="javascript:quota_changes('."'quota'".')" '.
                    212:                    '/>&nbsp;Mb</span></td>'."\n".
                    213:                    &Apache::loncommon::end_data_table_row()."\n";
                    214:     }  
                    215:     $output .= &Apache::loncommon::end_data_table();
1.134     raeburn   216:     return $output;
                    217: }
                    218: 
1.275     raeburn   219: sub build_tools_display {
                    220:     my ($ccuname,$ccdomain,$context) = @_;
1.306     raeburn   221:     my (@usertools,%userenv,$output,@options,%validations,%reqtitles,%reqdisplay,
1.332     raeburn   222:         $colspan,$isadv,%domconfig);
1.275     raeburn   223:     my %lt = &Apache::lonlocal::texthash (
                    224:                    'blog'       => "Personal User Blog",
                    225:                    'aboutme'    => "Personal Information Page",
                    226:                    'portfolio'  => "Personal User Portfolio",
                    227:                    'avai'       => "Available",
                    228:                    'cusa'       => "availability",
                    229:                    'chse'       => "Change setting",
                    230:                    'usde'       => "Use default",
                    231:                    'uscu'       => "Use custom",
                    232:                    'official'   => 'Can request creation of official courses',
1.299     raeburn   233:                    'unofficial' => 'Can request creation of unofficial courses',
                    234:                    'community'  => 'Can request creation of communities',
1.275     raeburn   235:     );
1.279     raeburn   236:     if ($context eq 'requestcourses') {
1.275     raeburn   237:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.299     raeburn   238:                       'requestcourses.official','requestcourses.unofficial',
                    239:                       'requestcourses.community');
                    240:         @usertools = ('official','unofficial','community');
1.309     raeburn   241:         @options =('norequest','approval','autolimit','validate');
1.306     raeburn   242:         %validations = &Apache::lonnet::auto_courserequest_checks($ccdomain);
                    243:         %reqtitles = &courserequest_titles();
                    244:         %reqdisplay = &courserequest_display();
                    245:         $colspan = ' colspan="2"';
1.332     raeburn   246:         %domconfig =
                    247:             &Apache::lonnet::get_dom('configuration',['requestcourses'],$ccdomain);
                    248:         $isadv = &Apache::lonnet::is_advanced_user($ccuname,$ccdomain);
1.275     raeburn   249:     } else {
                    250:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    251:                           'tools.aboutme','tools.portfolio','tools.blog');
                    252:         @usertools = ('aboutme','blog','portfolio');
                    253:     }
                    254:     foreach my $item (@usertools) {
1.306     raeburn   255:         my ($custom_access,$curr_access,$cust_on,$cust_off,$tool_on,$tool_off,
                    256:             $currdisp,$custdisp,$custradio);
1.275     raeburn   257:         $cust_off = 'checked="checked" ';
                    258:         $tool_on = 'checked="checked" ';
                    259:         $curr_access =  
                    260:             &Apache::lonnet::usertools_access($ccuname,$ccdomain,$item,undef,
                    261:                                               $context);
1.306     raeburn   262:         if ($userenv{$context.'.'.$item} ne '') {
                    263:             $cust_on = ' checked="checked" ';
                    264:             $cust_off = '';
                    265:         }
                    266:         if ($context eq 'requestcourses') {
                    267:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   268:                 $custom_access = &mt('Currently from default setting.');
1.306     raeburn   269:             } else {
                    270:                 $custom_access = &mt('Currently from custom setting.');
1.275     raeburn   271:             }
                    272:         } else {
1.306     raeburn   273:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   274:                 $custom_access =
1.306     raeburn   275:                     &mt('Availability determined currently from default setting.');
                    276:                 if (!$curr_access) {
                    277:                     $tool_off = 'checked="checked" ';
                    278:                     $tool_on = '';
                    279:                 }
                    280:             } else {
1.314     raeburn   281:                 $custom_access =
1.306     raeburn   282:                     &mt('Availability determined currently from custom setting.');
                    283:                 if ($userenv{$context.'.'.$item} == 0) {
                    284:                     $tool_off = 'checked="checked" ';
                    285:                     $tool_on = '';
                    286:                 }
1.275     raeburn   287:             }
                    288:         }
                    289:         $output .= '  <tr class="LC_info_row">'."\n".
1.306     raeburn   290:                    '   <td'.$colspan.'>'.$lt{$item}.'</td>'."\n".
1.275     raeburn   291:                    '  </tr>'."\n".
1.306     raeburn   292:                    &Apache::loncommon::start_data_table_row()."\n";
                    293:         if ($context eq 'requestcourses') {
                    294:             my ($curroption,$currlimit);
1.332     raeburn   295:             if ($userenv{$context.'.'.$item} ne '') {
                    296:                 $curroption = $userenv{$context.'.'.$item};
                    297:             } else {
                    298:                 my (@inststatuses);
                    299:                 $curroption =
                    300:                     &Apache::loncoursequeueadmin::get_processtype($ccuname,$ccdomain,$isadv,$ccdomain,
                    301:                                                                $item,\@inststatuses,\%domconfig);
                    302:             }
1.306     raeburn   303:             if (!$curroption) {
                    304:                 $curroption = 'norequest';
                    305:             }
                    306:             if ($curroption =~ /^autolimit=(\d*)$/) {
                    307:                 $currlimit = $1;
1.314     raeburn   308:                 if ($currlimit eq '') {
                    309:                     $currdisp = &mt('Yes, automatic creation');
                    310:                 } else {
                    311:                     $currdisp = &mt('Yes, up to [quant,_1,request]/user',$currlimit);
                    312:                 }
1.306     raeburn   313:             } else {
                    314:                 $currdisp = $reqdisplay{$curroption};
                    315:             }
                    316:             $custdisp = '<table>';
                    317:             foreach my $option (@options) {
                    318:                 my $val = $option;
                    319:                 if ($option eq 'norequest') {
                    320:                     $val = 0;
                    321:                 }
                    322:                 if ($option eq 'validate') {
                    323:                     my $canvalidate = 0;
                    324:                     if (ref($validations{$item}) eq 'HASH') {
                    325:                         if ($validations{$item}{'_custom_'}) {
                    326:                             $canvalidate = 1;
                    327:                         }
                    328:                     }
                    329:                     next if (!$canvalidate);
                    330:                 }
                    331:                 my $checked = '';
                    332:                 if ($option eq $curroption) {
                    333:                     $checked = ' checked="checked"';
                    334:                 } elsif ($option eq 'autolimit') {
                    335:                     if ($curroption =~ /^autolimit/) {
                    336:                         $checked = ' checked="checked"';
                    337:                     }
                    338:                 }
                    339:                 $custdisp .= '<tr><td><span class="LC_nobreak"><label>'.
                    340:                              '<input type="radio" name="crsreq_'.$item.
                    341:                              '" value="'.$val.'"'.$checked.' />'.
                    342:                              $reqtitles{$option}.'</label>&nbsp;';
                    343:                 if ($option eq 'autolimit') {
                    344:                     $custdisp .= '<input type="text" name="crsreq_'.
                    345:                                  $item.'_limit" size="1" '.
1.314     raeburn   346:                                  'value="'.$currlimit.'" /></span><br />'.
                    347:                                  $reqtitles{'unlimited'};
                    348:                  } else {
                    349:                      $custdisp .= '</span>';
1.306     raeburn   350:                  }
1.314     raeburn   351:                  $custdisp .= '</td></tr>';
1.306     raeburn   352:             }
                    353:             $custdisp .= '</table>';
                    354:             $custradio = '</span></td><td>'.&mt('Custom setting').'<br />'.$custdisp;
                    355:         } else {
                    356:             $currdisp = ($curr_access?&mt('Yes'):&mt('No'));
                    357:             $custdisp = '<span class="LC_nobreak"><label>'.
1.314     raeburn   358:                         '<input type="radio" name="'.$context.'_'.$item.'"'.
1.306     raeburn   359:                         ' value="1"'. $tool_on.'/>'.&mt('On').'</label>&nbsp;<label>'.
                    360:                         '<input type="radio" name="'.$context.'_'.$item.'" value="0" '.
                    361:                         $tool_off.'/>'.&mt('Off').'</label></span>';
                    362:             $custradio = ('&nbsp;'x2).'--'.$lt{'cusa'}.':&nbsp;'.$custdisp.
                    363:                           '</span>';
                    364:         }
                    365:         $output .= '  <td'.$colspan.'>'.$custom_access.('&nbsp;'x4).
                    366:                    $lt{'avai'}.': '.$currdisp.'</td>'."\n".
1.275     raeburn   367:                    &Apache::loncommon::end_data_table_row()."\n".
                    368:                    &Apache::loncommon::start_data_table_row()."\n".
1.306     raeburn   369:                    '  <td style="vertical-align:top;"><span class="LC_nobreak">'.
                    370:                    $lt{'chse'}.': <label>'.
1.275     raeburn   371:                    '<input type="radio" name="custom'.$item.'" value="0" '.
1.306     raeburn   372:                    $cust_off.'/>'.$lt{'usde'}.'</label>'.('&nbsp;' x3).
                    373:                    '<label><input type="radio" name="custom'.$item.'" value="1" '.
                    374:                    $cust_on.'/>'.$lt{'uscu'}.'</label>'.$custradio.'</td>'.
1.275     raeburn   375:                    &Apache::loncommon::end_data_table_row()."\n";
                    376:     }
                    377:     return $output;
                    378: }
                    379: 
1.300     raeburn   380: sub coursereq_externaluser {
                    381:     my ($ccuname,$ccdomain,$cdom) = @_;
1.306     raeburn   382:     my (@usertools,@options,%validations,%userenv,$output);
1.300     raeburn   383:     my %lt = &Apache::lonlocal::texthash (
                    384:                    'official'   => 'Can request creation of official courses',
                    385:                    'unofficial' => 'Can request creation of unofficial courses',
                    386:                    'community'  => 'Can request creation of communities',
                    387:     );
                    388: 
                    389:     %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    390:                       'reqcrsotherdom.official','reqcrsotherdom.unofficial',
                    391:                       'reqcrsotherdom.community');
                    392:     @usertools = ('official','unofficial','community');
1.309     raeburn   393:     @options = ('approval','validate','autolimit');
1.306     raeburn   394:     %validations = &Apache::lonnet::auto_courserequest_checks($cdom);
                    395:     my $optregex = join('|',@options);
                    396:     my %reqtitles = &courserequest_titles();
1.300     raeburn   397:     foreach my $item (@usertools) {
1.306     raeburn   398:         my ($curroption,$currlimit,$tooloff);
1.300     raeburn   399:         if ($userenv{'reqcrsotherdom.'.$item} ne '') {
                    400:             my @curr = split(',',$userenv{'reqcrsotherdom.'.$item});
1.314     raeburn   401:             foreach my $req (@curr) {
                    402:                 if ($req =~ /^\Q$cdom\E\:($optregex)=?(\d*)$/) {
                    403:                     $curroption = $1;
                    404:                     $currlimit = $2;
                    405:                     last;
1.306     raeburn   406:                 }
                    407:             }
1.314     raeburn   408:             if (!$curroption) {
                    409:                 $curroption = 'norequest';
                    410:                 $tooloff = ' checked="checked"';
                    411:             }
1.306     raeburn   412:         } else {
                    413:             $curroption = 'norequest';
                    414:             $tooloff = ' checked="checked"';
                    415:         }
                    416:         $output.= &Apache::loncommon::start_data_table_row()."\n".
1.314     raeburn   417:                   '  <td><span class="LC_nobreak">'.$lt{$item}.': </span></td><td>'.
                    418:                   '<table><tr><td valign="top">'."\n".
1.306     raeburn   419:                   '<label><input type="radio" name="reqcrsotherdom_'.$item.
1.314     raeburn   420:                   '" value=""'.$tooloff.' />'.$reqtitles{'norequest'}.
                    421:                   '</label></td>';
1.306     raeburn   422:         foreach my $option (@options) {
                    423:             if ($option eq 'validate') {
                    424:                 my $canvalidate = 0;
                    425:                 if (ref($validations{$item}) eq 'HASH') {
                    426:                     if ($validations{$item}{'_external_'}) {
                    427:                         $canvalidate = 1;
                    428:                     }
                    429:                 }
                    430:                 next if (!$canvalidate);
                    431:             }
                    432:             my $checked = '';
                    433:             if ($option eq $curroption) {
                    434:                 $checked = ' checked="checked"';
                    435:             }
1.314     raeburn   436:             $output .= '<td valign="top"><span class="LC_nobreak"><label>'.
1.306     raeburn   437:                        '<input type="radio" name="reqcrsotherdom_'.$item.
                    438:                        '" value="'.$option.'"'.$checked.' />'.
1.314     raeburn   439:                        $reqtitles{$option}.'</label>';
1.306     raeburn   440:             if ($option eq 'autolimit') {
1.314     raeburn   441:                 $output .= '&nbsp;<input type="text" name="reqcrsotherdom_'.
1.306     raeburn   442:                            $item.'_limit" size="1" '.
1.314     raeburn   443:                            'value="'.$currlimit.'" /></span>'.
                    444:                            '<br />'.$reqtitles{'unlimited'};
                    445:             } else {
                    446:                 $output .= '</span>';
1.300     raeburn   447:             }
1.314     raeburn   448:             $output .= '</td>';
1.300     raeburn   449:         }
1.314     raeburn   450:         $output .= '</td></tr></table></td>'."\n".
1.300     raeburn   451:                    &Apache::loncommon::end_data_table_row()."\n";
                    452:     }
                    453:     return $output;
                    454: }
                    455: 
1.306     raeburn   456: sub courserequest_titles {
                    457:     my %titles = &Apache::lonlocal::texthash (
                    458:                                    official   => 'Official',
                    459:                                    unofficial => 'Unofficial',
                    460:                                    community  => 'Communities',
                    461:                                    norequest  => 'Not allowed',
1.309     raeburn   462:                                    approval   => 'Approval by Dom. Coord.',
1.306     raeburn   463:                                    validate   => 'With validation',
                    464:                                    autolimit  => 'Numerical limit',
1.314     raeburn   465:                                    unlimited  => '(blank for unlimited)',
1.306     raeburn   466:                  );
                    467:     return %titles;
                    468: }
                    469: 
                    470: sub courserequest_display {
                    471:     my %titles = &Apache::lonlocal::texthash (
1.309     raeburn   472:                                    approval   => 'Yes, need approval',
1.306     raeburn   473:                                    validate   => 'Yes, with validation',
                    474:                                    norequest  => 'No',
                    475:    );
                    476:    return %titles;
                    477: }
                    478: 
1.2       www       479: # =================================================================== Phase one
1.1       www       480: 
1.42      matthew   481: sub print_username_entry_form {
1.351     raeburn   482:     my ($r,$context,$response,$srch,$forcenewuser,$crstype,$brcrum) = @_;
1.101     albertel  483:     my $defdom=$env{'request.role.domain'};
1.160     raeburn   484:     my $formtoset = 'crtuser';
                    485:     if (exists($env{'form.startrolename'})) {
                    486:         $formtoset = 'docustom';
                    487:         $env{'form.rolename'} = $env{'form.startrolename'};
1.207     raeburn   488:     } elsif ($env{'form.origform'} eq 'crtusername') {
                    489:         $formtoset =  $env{'form.origform'};
1.160     raeburn   490:     }
                    491: 
                    492:     my ($jsback,$elements) = &crumb_utilities();
                    493: 
                    494:     my $jscript = &Apache::loncommon::studentbrowser_javascript()."\n".
1.165     albertel  495:         '<script type="text/javascript">'."\n".
1.301     bisitz    496:         '// <![CDATA['."\n".
                    497:         &Apache::lonhtmlcommon::set_form_elements($elements->{$formtoset})."\n".
                    498:         '// ]]>'."\n".
1.162     raeburn   499:         '</script>'."\n";
1.160     raeburn   500: 
1.324     raeburn   501:     my %existingroles=&Apache::lonuserutils::my_custom_roles($crstype);
                    502:     if (($env{'form.action'} eq 'custom') && (keys(%existingroles) > 0)
                    503:         && (&Apache::lonnet::allowed('mcr','/'))) {
                    504:         $jscript .= &customrole_javascript();
                    505:     }
1.224     raeburn   506:     my $helpitem = 'Course_Change_Privileges';
                    507:     if ($env{'form.action'} eq 'custom') {
                    508:         $helpitem = 'Course_Editing_Custom_Roles';
                    509:     } elsif ($env{'form.action'} eq 'singlestudent') {
                    510:         $helpitem = 'Course_Add_Student';
                    511:     }
1.351     raeburn   512:     my %breadcrumb_text = &singleuser_breadcrumb($crstype);
                    513:     if ($env{'form.action'} eq 'custom') {
                    514:         push(@{$brcrum},
                    515:                  {href=>"javascript:backPage(document.crtuser)",       
                    516:                   text=>"Pick custom role",
                    517:                   help => $helpitem,}
                    518:                  );
                    519:     } else {
                    520:         push (@{$brcrum},
                    521:                   {href => "javascript:backPage(document.crtuser)",
                    522:                    text => $breadcrumb_text{'search'},
                    523:                    help => $helpitem,
                    524:                    faq  => 282,
                    525:                    bug  => 'Instructor Interface',}
                    526:                   );
                    527:     }
                    528:     my %loaditems = (
                    529:                 'onload' => "javascript:setFormElements(document.$formtoset)",
                    530:                     );
                    531:     my $args = {bread_crumbs           => $brcrum,
                    532:                 bread_crumbs_component => 'User Management',
                    533:                 add_entries            => \%loaditems,};
                    534:     $r->print(&Apache::loncommon::start_page('User Management',$jscript,$args));
                    535: 
1.71      sakharuk  536:     my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   537:                     'srst' => 'Search for a user and enroll as a student',
1.318     raeburn   538:                     'srme' => 'Search for a user and enroll as a member',
1.229     raeburn   539:                     'srad' => 'Search for a user and modify/add user information or roles',
1.71      sakharuk  540: 		    'usr'  => "Username",
                    541:                     'dom'  => "Domain",
1.324     raeburn   542:                     'ecrp' => "Define or Edit Custom Role",
                    543:                     'nr'   => "role name",
1.282     schafran  544:                     'cre'  => "Next",
1.71      sakharuk  545: 				       );
1.351     raeburn   546: 
1.214     raeburn   547:     if ($env{'form.action'} eq 'custom') {
1.190     raeburn   548:         if (&Apache::lonnet::allowed('mcr','/')) {
1.324     raeburn   549:             my $newroletext = &mt('Define new custom role:');
                    550:             $r->print('<form action="/adm/createuser" method="post" name="docustom">'.
                    551:                       '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
                    552:                       '<input type="hidden" name="phase" value="selected_custom_edit" />'.
                    553:                       '<h3>'.$lt{'ecrp'}.'</h3>'.
                    554:                       &Apache::loncommon::start_data_table().
                    555:                       &Apache::loncommon::start_data_table_row().
                    556:                       '<td>');
                    557:             if (keys(%existingroles) > 0) {
                    558:                 $r->print('<br /><label><input type="radio" name="customroleaction" value="new" checked="checked" onclick="setCustomFields();" /><b>'.$newroletext.'</b></label>');
                    559:             } else {
                    560:                 $r->print('<br /><input type="hidden" name="customroleaction" value="new" /><b>'.$newroletext.'</b>');
                    561:             }
                    562:             $r->print('</td><td align="center">'.$lt{'nr'}.'<br /><input type="text" size="15" name="newrolename" onfocus="setCustomAction('."'new'".');" /></td>'.
                    563:                       &Apache::loncommon::end_data_table_row());
                    564:             if (keys(%existingroles) > 0) {
                    565:                 $r->print(&Apache::loncommon::start_data_table_row().'<td><br />'.
                    566:                           '<label><input type="radio" name="customroleaction" value="edit" onclick="setCustomFields();"/><b>'.
                    567:                           &mt('View/Modify existing role:').'</b></label></td>'.
                    568:                           '<td align="center"><br />'.
                    569:                           '<select name="rolename" onchange="setCustomAction('."'edit'".');">'.
1.326     raeburn   570:                           '<option value="" selected="selected">'.
1.324     raeburn   571:                           &mt('Select'));
                    572:                 foreach my $role (sort(keys(%existingroles))) {
1.326     raeburn   573:                     $r->print('<option value="'.$role.'">'.$role.'</option>');
1.324     raeburn   574:                 }
                    575:                 $r->print('</select>'.
                    576:                           '</td>'.
                    577:                           &Apache::loncommon::end_data_table_row());
                    578:             }
                    579:             $r->print(&Apache::loncommon::end_data_table().'<p>'.
                    580:                       '<input name="customeditor" type="submit" value="'.
                    581:                       $lt{'cre'}.'" /></p>'.
                    582:                       '</form>');
1.190     raeburn   583:         }
1.213     raeburn   584:     } else {
1.229     raeburn   585:         my $actiontext = $lt{'srad'};
1.213     raeburn   586:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn   587:             if ($crstype eq 'Community') {
                    588:                 $actiontext = $lt{'srme'};
                    589:             } else {
                    590:                 $actiontext = $lt{'srst'};
                    591:             }
1.213     raeburn   592:         }
1.324     raeburn   593:         $r->print("<h3>$actiontext</h3>");
1.213     raeburn   594:         if ($env{'form.origform'} ne 'crtusername') {
                    595:             $r->print("\n".$response);
                    596:         }
1.318     raeburn   597:         $r->print(&entry_form($defdom,$srch,$forcenewuser,$context,$response,$crstype));
1.107     www       598:     }
1.110     albertel  599: }
                    600: 
1.324     raeburn   601: sub customrole_javascript {
                    602:     my $js = <<"END";
                    603: <script type="text/javascript">
                    604: // <![CDATA[
                    605: 
                    606: function setCustomFields() {
                    607:     if (document.docustom.customroleaction.length > 0) {
                    608:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    609:             if (document.docustom.customroleaction[i].checked) {
                    610:                 if (document.docustom.customroleaction[i].value == 'new') {
                    611:                     document.docustom.rolename.selectedIndex = 0;
                    612:                 } else {
                    613:                     document.docustom.newrolename.value = '';
                    614:                 }
                    615:             }
                    616:         }
                    617:     }
                    618:     return;
                    619: }
                    620: 
                    621: function setCustomAction(caller) {
                    622:     if (document.docustom.customroleaction.length > 0) {
                    623:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    624:             if (document.docustom.customroleaction[i].value == caller) {
                    625:                 document.docustom.customroleaction[i].checked = true;
                    626:             }
                    627:         }
                    628:     }
                    629:     setCustomFields();
                    630:     return;
                    631: }
                    632: 
                    633: // ]]>
                    634: </script>
                    635: END
                    636:     return $js;
                    637: }
                    638: 
1.160     raeburn   639: sub entry_form {
1.318     raeburn   640:     my ($dom,$srch,$forcenewuser,$context,$responsemsg,$crstype) = @_;
1.229     raeburn   641:     my ($usertype,$inexact);
1.214     raeburn   642:     if (ref($srch) eq 'HASH') {
                    643:         if (($srch->{'srchin'} eq 'dom') &&
                    644:             ($srch->{'srchby'} eq 'uname') &&
                    645:             ($srch->{'srchtype'} eq 'exact') &&
                    646:             ($srch->{'srchdomain'} ne '') &&
                    647:             ($srch->{'srchterm'} ne '')) {
1.353     raeburn   648:             my (%curr_rules,%got_rules);
1.214     raeburn   649:             my ($rules,$ruleorder) =
                    650:                 &Apache::lonnet::inst_userrules($srch->{'srchdomain'},'username');
1.353     raeburn   651:             $usertype = &Apache::lonuserutils::check_usertype($srch->{'srchdomain'},$srch->{'srchterm'},$rules,\%curr_rules,\%got_rules);
1.229     raeburn   652:         } else {
                    653:             $inexact = 1;
1.214     raeburn   654:         }
1.207     raeburn   655:     }
1.214     raeburn   656:     my $cancreate =
                    657:         &Apache::lonuserutils::can_create_user($dom,$context,$usertype);
1.160     raeburn   658:     my $userpicker = 
1.179     raeburn   659:        &Apache::loncommon::user_picker($dom,$srch,$forcenewuser,
1.214     raeburn   660:                                        'document.crtuser',$cancreate,$usertype);
1.160     raeburn   661:     my $srchbutton = &mt('Search');
1.229     raeburn   662:     if ($env{'form.action'} eq 'singlestudent') {
                    663:         $srchbutton = &mt('Search and Enroll');
                    664:     } elsif ($cancreate && $responsemsg ne '' && $inexact) {
                    665:         $srchbutton = &mt('Search or Add New User');
                    666:     }
1.207     raeburn   667:     my $output = <<"ENDBLOCK";
1.160     raeburn   668: <form action="/adm/createuser" method="post" name="crtuser">
1.190     raeburn   669: <input type="hidden" name="action" value="$env{'form.action'}" />
1.160     raeburn   670: <input type="hidden" name="phase" value="get_user_info" />
                    671: $userpicker
1.179     raeburn   672: <input name="userrole" type="button" value="$srchbutton" onclick="javascript:validateEntry(document.crtuser)" />
1.160     raeburn   673: </form>
1.207     raeburn   674: ENDBLOCK
1.229     raeburn   675:     if ($env{'form.phase'} eq '') {
1.207     raeburn   676:         my $defdom=$env{'request.role.domain'};
                    677:         my $domform = &Apache::loncommon::select_dom_form($defdom,'srchdomain');
                    678:         my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   679:                   'enro' => 'Enroll one student',
1.318     raeburn   680:                   'enrm' => 'Enroll one member',
1.229     raeburn   681:                   'admo' => 'Add/modify a single user',
                    682:                   'crea' => 'create new user if required',
                    683:                   'uskn' => "username is known",
1.207     raeburn   684:                   'crnu' => 'Create a new user',
                    685:                   'usr'  => 'Username',
                    686:                   'dom'  => 'in domain',
1.229     raeburn   687:                   'enrl' => 'Enroll',
                    688:                   'cram'  => 'Create/Modify user',
1.207     raeburn   689:         );
1.229     raeburn   690:         my $sellink=&Apache::loncommon::selectstudent_link('crtusername','srchterm','srchdomain');
                    691:         my ($title,$buttontext,$showresponse);
1.318     raeburn   692:         if ($env{'form.action'} eq 'singlestudent') {
                    693:             if ($crstype eq 'Community') {
                    694:                 $title = $lt{'enrm'};
                    695:             } else {
                    696:                 $title = $lt{'enro'};
                    697:             }
1.229     raeburn   698:             $buttontext = $lt{'enrl'};
                    699:         } else {
                    700:             $title = $lt{'admo'};
                    701:             $buttontext = $lt{'cram'};
                    702:         }
                    703:         if ($cancreate) {
                    704:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'crea'}.')</span>';
                    705:         } else {
                    706:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'uskn'}.')</span>';
                    707:         }
                    708:         if ($env{'form.origform'} eq 'crtusername') {
                    709:             $showresponse = $responsemsg;
                    710:         }
1.207     raeburn   711:         $output .= <<"ENDDOCUMENT";
1.229     raeburn   712: <br />
1.207     raeburn   713: <form action="/adm/createuser" method="post" name="crtusername">
                    714: <input type="hidden" name="action" value="$env{'form.action'}" />
                    715: <input type="hidden" name="phase" value="createnewuser" />
                    716: <input type="hidden" name="srchtype" value="exact" />
1.233     raeburn   717: <input type="hidden" name="srchby" value="uname" />
1.207     raeburn   718: <input type="hidden" name="srchin" value="dom" />
                    719: <input type="hidden" name="forcenewuser" value="1" />
                    720: <input type="hidden" name="origform" value="crtusername" />
1.229     raeburn   721: <h3>$title</h3>
                    722: $showresponse
1.207     raeburn   723: <table>
                    724:  <tr>
                    725:   <td>$lt{'usr'}:</td>
                    726:   <td><input type="text" size="15" name="srchterm" /></td>
                    727:   <td>&nbsp;$lt{'dom'}:</td><td>$domform</td>
1.229     raeburn   728:   <td>&nbsp;$sellink&nbsp;</td>
                    729:   <td>&nbsp;<input name="userrole" type="submit" value="$buttontext" /></td>
1.207     raeburn   730:  </tr>
                    731: </table>
                    732: </form>
1.160     raeburn   733: ENDDOCUMENT
1.207     raeburn   734:     }
1.160     raeburn   735:     return $output;
                    736: }
1.110     albertel  737: 
                    738: sub user_modification_js {
1.113     raeburn   739:     my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_;
                    740:     
1.110     albertel  741:     return <<END;
                    742: <script type="text/javascript" language="Javascript">
1.301     bisitz    743: // <![CDATA[
1.314     raeburn   744: 
1.110     albertel  745:     function pclose() {
                    746:         parmwin=window.open("/adm/rat/empty.html","LONCAPAparms",
                    747:                  "height=350,width=350,scrollbars=no,menubar=no");
                    748:         parmwin.close();
                    749:     }
                    750: 
                    751:     $pjump_def
                    752:     $dc_setcourse_code
                    753: 
                    754:     function dateset() {
                    755:         eval("document.cu."+document.cu.pres_marker.value+
                    756:             ".value=document.cu.pres_value.value");
                    757:         pclose();
                    758:     }
                    759: 
1.113     raeburn   760:     $nondc_setsection_code
1.301     bisitz    761: // ]]>
1.110     albertel  762: </script>
                    763: END
1.2       www       764: }
                    765: 
                    766: # =================================================================== Phase two
1.160     raeburn   767: sub print_user_selection_page {
1.351     raeburn   768:     my ($r,$response,$srch,$srch_results,$srcharray,$context,$opener_elements,$crstype,$brcrum) = @_;
1.160     raeburn   769:     my @fields = ('username','domain','lastname','firstname','permanentemail');
                    770:     my $sortby = $env{'form.sortby'};
                    771: 
                    772:     if (!grep(/^\Q$sortby\E$/,@fields)) {
                    773:         $sortby = 'lastname';
                    774:     }
                    775: 
                    776:     my ($jsback,$elements) = &crumb_utilities();
                    777: 
                    778:     my $jscript = (<<ENDSCRIPT);
                    779: <script type="text/javascript">
1.301     bisitz    780: // <![CDATA[
1.160     raeburn   781: function pickuser(uname,udom) {
                    782:     document.usersrchform.seluname.value=uname;
                    783:     document.usersrchform.seludom.value=udom;
                    784:     document.usersrchform.phase.value="userpicked";
                    785:     document.usersrchform.submit();
                    786: }
                    787: 
                    788: $jsback
1.301     bisitz    789: // ]]>
1.160     raeburn   790: </script>
                    791: ENDSCRIPT
                    792: 
                    793:     my %lt=&Apache::lonlocal::texthash(
1.179     raeburn   794:                                        'usrch'          => "User Search to add/modify roles",
                    795:                                        'stusrch'        => "User Search to enroll student",
1.318     raeburn   796:                                        'memsrch'        => "User Search to enroll member",
1.179     raeburn   797:                                        'usel'           => "Select a user to add/modify roles",
1.318     raeburn   798:                                        'stusel'         => "Select a user to enroll as a student",
                    799:                                        'memsel'         => "Select a user to enroll as a member",
1.160     raeburn   800:                                        'username'       => "username",
                    801:                                        'domain'         => "domain",
                    802:                                        'lastname'       => "last name",
                    803:                                        'firstname'      => "first name",
                    804:                                        'permanentemail' => "permanent e-mail",
                    805:                                       );
1.302     raeburn   806:     if ($context eq 'requestcrs') {
                    807:         $r->print('<div>');
                    808:     } else {
1.318     raeburn   809:         my %breadcrumb_text = &singleuser_breadcrumb($crstype);
1.351     raeburn   810:         my $helpitem;
                    811:         if ($env{'form.action'} eq 'singleuser') {
                    812:             $helpitem = 'Course_Change_Privileges';
                    813:         } elsif ($env{'form.action'} eq 'singlestudent') {
                    814:             $helpitem = 'Course_Add_Student';
                    815:         }
                    816:         push (@{$brcrum},
                    817:                   {href => "javascript:backPage(document.usersrchform,'','')",
                    818:                    text => $breadcrumb_text{'search'},
                    819:                    faq  => 282,
                    820:                    bug  => 'Instructor Interface',},
                    821:                   {href => "javascript:backPage(document.usersrchform,'get_user_info','select')",
                    822:                    text => $breadcrumb_text{'userpicked'},
                    823:                    faq  => 282,
                    824:                    bug  => 'Instructor Interface',
                    825:                    help => $helpitem}
                    826:                   );
                    827:         $r->print(&Apache::loncommon::start_page('User Management',$jscript,{bread_crumbs => $brcrum}));
1.302     raeburn   828:         if ($env{'form.action'} eq 'singleuser') {
                    829:             $r->print("<b>$lt{'usrch'}</b><br />");
1.318     raeburn   830:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
1.302     raeburn   831:             $r->print('<h3>'.$lt{'usel'}.'</h3>');
                    832:         } elsif ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn   833:             $r->print($jscript."<b>");
                    834:             if ($crstype eq 'Community') {
                    835:                 $r->print($lt{'memsrch'});
                    836:             } else {
                    837:                 $r->print($lt{'stusrch'});
                    838:             }
                    839:             $r->print("</b><br />");
                    840:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
                    841:             $r->print('</form><h3>');
                    842:             if ($crstype eq 'Community') {
                    843:                 $r->print($lt{'memsel'});
                    844:             } else {
                    845:                 $r->print($lt{'stusel'});
                    846:             }
                    847:             $r->print('</h3>');
1.302     raeburn   848:         }
1.179     raeburn   849:     }
1.160     raeburn   850:     $r->print('<form name="usersrchform" method="post">'.
                    851:               &Apache::loncommon::start_data_table()."\n".
                    852:               &Apache::loncommon::start_data_table_header_row()."\n".
                    853:               ' <th> </th>'."\n");
                    854:     foreach my $field (@fields) {
                    855:         $r->print(' <th><a href="javascript:document.usersrchform.sortby.value='.
                    856:                   "'".$field."'".';document.usersrchform.submit();">'.
                    857:                   $lt{$field}.'</a></th>'."\n");
                    858:     }
                    859:     $r->print(&Apache::loncommon::end_data_table_header_row());
                    860: 
                    861:     my @sorted_users = sort {
1.167     albertel  862:         lc($srch_results->{$a}->{$sortby})   cmp lc($srch_results->{$b}->{$sortby})
1.160     raeburn   863:             ||
1.167     albertel  864:         lc($srch_results->{$a}->{lastname})  cmp lc($srch_results->{$b}->{lastname})
1.160     raeburn   865:             ||
                    866:         lc($srch_results->{$a}->{firstname}) cmp lc($srch_results->{$b}->{firstname})
1.167     albertel  867: 	    ||
                    868: 	lc($a) cmp lc($b)
1.160     raeburn   869:         } (keys(%$srch_results));
                    870: 
                    871:     foreach my $user (@sorted_users) {
                    872:         my ($uname,$udom) = split(/:/,$user);
1.302     raeburn   873:         my $onclick;
                    874:         if ($context eq 'requestcrs') {
1.314     raeburn   875:             $onclick =
1.302     raeburn   876:                 'onclick="javascript:gochoose('."'$uname','$udom',".
                    877:                                                "'$srch_results->{$user}->{firstname}',".
                    878:                                                "'$srch_results->{$user}->{lastname}',".
                    879:                                                "'$srch_results->{$user}->{permanentemail}'".');"';
                    880:         } else {
1.314     raeburn   881:             $onclick =
1.302     raeburn   882:                 ' onclick="javascript:pickuser('."'".$uname."'".','."'".$udom."'".');"';
                    883:         }
1.160     raeburn   884:         $r->print(&Apache::loncommon::start_data_table_row().
1.302     raeburn   885:                   '<td><input type="button" name="seluser" value="'.&mt('Select').'" '.
                    886:                   $onclick.' /></td>'.
1.160     raeburn   887:                   '<td><tt>'.$uname.'</tt></td>'.
                    888:                   '<td><tt>'.$udom.'</tt></td>');
                    889:         foreach my $field ('lastname','firstname','permanentemail') {
                    890:             $r->print('<td>'.$srch_results->{$user}->{$field}.'</td>');
                    891:         }
                    892:         $r->print(&Apache::loncommon::end_data_table_row());
                    893:     }
                    894:     $r->print(&Apache::loncommon::end_data_table().'<br /><br />');
1.179     raeburn   895:     if (ref($srcharray) eq 'ARRAY') {
                    896:         foreach my $item (@{$srcharray}) {
                    897:             $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
                    898:         }
                    899:     }
1.160     raeburn   900:     $r->print(' <input type="hidden" name="sortby" value="'.$sortby.'" />'."\n".
                    901:               ' <input type="hidden" name="seluname" value="" />'."\n".
                    902:               ' <input type="hidden" name="seludom" value="" />'."\n".
1.179     raeburn   903:               ' <input type="hidden" name="currstate" value="select" />'."\n".
1.190     raeburn   904:               ' <input type="hidden" name="phase" value="get_user_info" />'."\n".
1.214     raeburn   905:               ' <input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n");
1.302     raeburn   906:     if ($context eq 'requestcrs') {
                    907:         $r->print($opener_elements.'</form></div>');
                    908:     } else {
1.351     raeburn   909:         $r->print($response.'</form>');
1.302     raeburn   910:     }
1.160     raeburn   911: }
                    912: 
                    913: sub print_user_query_page {
1.351     raeburn   914:     my ($r,$caller,$brcrum) = @_;
1.160     raeburn   915: # FIXME - this is for a network-wide name search (similar to catalog search)
                    916: # To use frames with similar behavior to catalog/portfolio search.
                    917: # To be implemented. 
                    918:     return;
                    919: }
                    920: 
1.42      matthew   921: sub print_user_modification_page {
1.351     raeburn   922:     my ($r,$ccuname,$ccdomain,$srch,$response,$context,$permission,$crstype,$brcrum) = @_;
1.185     raeburn   923:     if (($ccuname eq '') || ($ccdomain eq '')) {
1.215     raeburn   924:         my $usermsg = &mt('No username and/or domain provided.');
                    925:         $env{'form.phase'} = '';
1.351     raeburn   926: 	&print_username_entry_form($r,$context,$usermsg,'','',$crstype,$brcrum);
1.58      www       927:         return;
                    928:     }
1.213     raeburn   929:     my ($form,$formname);
                    930:     if ($env{'form.action'} eq 'singlestudent') {
                    931:         $form = 'document.enrollstudent';
                    932:         $formname = 'enrollstudent';
                    933:     } else {
                    934:         $form = 'document.cu';
                    935:         $formname = 'cu';
                    936:     }
1.188     raeburn   937:     my %abv_auth = &auth_abbrev();
1.227     raeburn   938:     my (%rulematch,%inst_results,$newuser,%alerts,%curr_rules,%got_rules);
1.185     raeburn   939:     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
                    940:     if ($uhome eq 'no_host') {
1.215     raeburn   941:         my $usertype;
                    942:         my ($rules,$ruleorder) =
                    943:             &Apache::lonnet::inst_userrules($ccdomain,'username');
                    944:             $usertype =
1.353     raeburn   945:                 &Apache::lonuserutils::check_usertype($ccdomain,$ccuname,$rules,
                    946:                                                        \%curr_rules,\%got_rules);
1.215     raeburn   947:         my $cancreate =
                    948:             &Apache::lonuserutils::can_create_user($ccdomain,$context,
                    949:                                                    $usertype);
                    950:         if (!$cancreate) {
1.292     bisitz    951:             my $helplink = 'javascript:helpMenu('."'display'".')';
1.215     raeburn   952:             my %usertypetext = (
                    953:                 official   => 'institutional',
                    954:                 unofficial => 'non-institutional',
                    955:             );
                    956:             my $response;
                    957:             if ($env{'form.origform'} eq 'crtusername') {
1.330     bisitz    958:                 $response =  '<span class="LC_warning">'.&mt('No match found for the username [_1] in LON-CAPA domain: [_2]','<b>'.$ccuname.'</b>',$ccdomain).
1.215     raeburn   959:                             '</span><br />';
                    960:             }
1.292     bisitz    961:             $response .= '<p class="LC_warning">'
                    962:                         .&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.")
                    963:                         .' '
                    964:                         .&mt('Please contact the [_1]helpdesk[_2] for assistance.'
                    965:                             ,'<a href="'.$helplink.'">','</a>')
                    966:                         .'</p><br />';
1.215     raeburn   967:             $env{'form.phase'} = '';
1.351     raeburn   968:             &print_username_entry_form($r,$context,$response,undef,undef,$crstype,$brcrum);
1.215     raeburn   969:             return;
                    970:         }
1.188     raeburn   971:         $newuser = 1;
1.193     raeburn   972:         my $checkhash;
                    973:         my $checks = { 'username' => 1 };
1.196     raeburn   974:         $checkhash->{$ccuname.':'.$ccdomain} = { 'newuser' => $newuser };
1.193     raeburn   975:         &Apache::loncommon::user_rule_check($checkhash,$checks,
1.196     raeburn   976:             \%alerts,\%rulematch,\%inst_results,\%curr_rules,\%got_rules);
                    977:         if (ref($alerts{'username'}) eq 'HASH') {
                    978:             if (ref($alerts{'username'}{$ccdomain}) eq 'HASH') {
                    979:                 my $domdesc =
1.193     raeburn   980:                     &Apache::lonnet::domain($ccdomain,'description');
1.196     raeburn   981:                 if ($alerts{'username'}{$ccdomain}{$ccuname}) {
                    982:                     my $userchkmsg;
                    983:                     if (ref($curr_rules{$ccdomain}) eq 'HASH') {  
                    984:                         $userchkmsg = 
                    985:                             &Apache::loncommon::instrule_disallow_msg('username',
1.193     raeburn   986:                                                                  $domdesc,1).
                    987:                         &Apache::loncommon::user_rule_formats($ccdomain,
                    988:                             $domdesc,$curr_rules{$ccdomain}{'username'},
                    989:                             'username');
1.196     raeburn   990:                     }
1.215     raeburn   991:                     $env{'form.phase'} = '';
1.351     raeburn   992:                     &print_username_entry_form($r,$context,$userchkmsg,undef,undef,$crstype,$brcrum);
1.196     raeburn   993:                     return;
1.215     raeburn   994:                 }
1.193     raeburn   995:             }
1.185     raeburn   996:         }
1.187     raeburn   997:     } else {
1.188     raeburn   998:         $newuser = 0;
1.185     raeburn   999:     }
1.160     raeburn  1000:     if ($response) {
1.215     raeburn  1001:         $response = '<br />'.$response;
1.160     raeburn  1002:     }
1.149     raeburn  1003: 
1.52      matthew  1004:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
1.88      raeburn  1005:     my $dc_setcourse_code = '';
1.119     raeburn  1006:     my $nondc_setsection_code = '';                                        
1.112     albertel 1007:     my %loaditem;
1.114     albertel 1008: 
1.216     raeburn  1009:     my $groupslist = &Apache::lonuserutils::get_groupslist();
1.88      raeburn  1010: 
1.216     raeburn  1011:     my $js = &validation_javascript($context,$ccdomain,$pjump_def,
                   1012:                                $groupslist,$newuser,$formname,\%loaditem);
1.318     raeburn  1013:     my %breadcrumb_text = &singleuser_breadcrumb($crstype);
1.224     raeburn  1014:     my $helpitem = 'Course_Change_Privileges';
                   1015:     if ($env{'form.action'} eq 'singlestudent') {
                   1016:         $helpitem = 'Course_Add_Student';
                   1017:     }
1.351     raeburn  1018:     push (@{$brcrum},
                   1019:         {href => "javascript:backPage($form)",
                   1020:          text => $breadcrumb_text{'search'},
                   1021:          faq  => 282,
                   1022:          bug  => 'Instructor Interface',});
                   1023:     if ($env{'form.phase'} eq 'userpicked') {
                   1024:        push(@{$brcrum},
                   1025:               {href => "javascript:backPage($form,'get_user_info','select')",
                   1026:                text => $breadcrumb_text{'userpicked'},
                   1027:                faq  => 282,
                   1028:                bug  => 'Instructor Interface',});
                   1029:     }
                   1030:     push(@{$brcrum},
                   1031:             {href => "javascript:backPage($form,'$env{'form.phase'}','modify')",
                   1032:              text => $breadcrumb_text{'modify'},
                   1033:              faq  => 282,
                   1034:              bug  => 'Instructor Interface',
                   1035:              help => $helpitem});
                   1036:     my $args = {'add_entries'           => \%loaditem,
                   1037:                 'bread_crumbs'          => $brcrum,
                   1038:                 'bread_crumbs_component' => 'User Management'};
                   1039:     if ($env{'form.popup'}) {
                   1040:         $args->{'no_nav_bar'} = 1;
                   1041:     }
                   1042:     my $start_page =
                   1043:         &Apache::loncommon::start_page('User Management',$js,$args);
1.3       www      1044: 
1.25      matthew  1045:     my $forminfo =<<"ENDFORMINFO";
1.216     raeburn  1046: <form action="/adm/createuser" method="post" name="$formname">
1.190     raeburn  1047: <input type="hidden" name="phase" value="update_user_data" />
1.188     raeburn  1048: <input type="hidden" name="ccuname" value="$ccuname" />
                   1049: <input type="hidden" name="ccdomain" value="$ccdomain" />
1.157     albertel 1050: <input type="hidden" name="pres_value"  value="" />
                   1051: <input type="hidden" name="pres_type"   value="" />
                   1052: <input type="hidden" name="pres_marker" value="" />
1.25      matthew  1053: ENDFORMINFO
1.329     raeburn  1054:     my (%inccourses,$roledom);
                   1055:     if ($context eq 'course') {
                   1056:         $inccourses{$env{'request.course.id'}}=1;
                   1057:         $roledom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   1058:     } elsif ($context eq 'author') {
                   1059:         $roledom = $env{'request.role.domain'};
                   1060:     } elsif ($context eq 'domain') {
                   1061:         foreach my $key (keys(%env)) {
                   1062:             $roledom = $env{'request.role.domain'};
                   1063:             if ($key=~/^user\.priv\.cm\.\/($roledom)\/($match_username)/) {
                   1064:                 $inccourses{$1.'_'.$2}=1;
                   1065:             }
                   1066:         }
                   1067:     } else {
                   1068:         foreach my $key (keys(%env)) {
                   1069: 	    if ($key=~/^user\.priv\.cm\.\/($match_domain)\/($match_username)/) {
                   1070: 	        $inccourses{$1.'_'.$2}=1;
                   1071:             }
1.2       www      1072:         }
1.24      matthew  1073:     }
1.216     raeburn  1074:     if ($newuser) {
1.134     raeburn  1075:         my $portfolioform;
1.267     raeburn  1076:         if ((&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) ||
                   1077:             (&Apache::lonnet::allowed('mut',$env{'request.role.domain'}))) {
                   1078:             # Current user has quota or user tools modification privileges
1.188     raeburn  1079:             $portfolioform = '<br />'.&portfolio_quota($ccuname,$ccdomain);
1.134     raeburn  1080:         }
1.227     raeburn  1081:         &initialize_authen_forms($ccdomain,$formname);
1.188     raeburn  1082:         my %lt=&Apache::lonlocal::texthash(
                   1083:                 'cnu'            => 'Create New User',
1.213     raeburn  1084:                 'ast'            => 'as a student',
1.318     raeburn  1085:                 'ame'            => 'as a member',
1.188     raeburn  1086:                 'ind'            => 'in domain',
                   1087:                 'lg'             => 'Login Data',
1.190     raeburn  1088:                 'hs'             => "Home Server",
1.188     raeburn  1089:         );
1.185     raeburn  1090: 	$r->print(<<ENDTITLE);
1.110     albertel 1091: $start_page
1.160     raeburn  1092: $response
1.25      matthew  1093: $forminfo
1.31      matthew  1094: <script type="text/javascript" language="Javascript">
1.301     bisitz   1095: // <![CDATA[
1.20      harris41 1096: $loginscript
1.301     bisitz   1097: // ]]>
1.31      matthew  1098: </script>
1.20      harris41 1099: <input type='hidden' name='makeuser' value='1' />
1.216     raeburn  1100: <h2>$lt{'cnu'} "$ccuname" $lt{'ind'} $ccdomain
1.185     raeburn  1101: ENDTITLE
1.213     raeburn  1102:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1103:             if ($crstype eq 'Community') {
                   1104:                 $r->print(' ('.$lt{'ame'}.')');
                   1105:             } else {
                   1106:                 $r->print(' ('.$lt{'ast'}.')');
                   1107:             }
1.213     raeburn  1108:         }
                   1109:         $r->print('</h2>'."\n".'<div class="LC_left_float">');
1.206     raeburn  1110:         my $personal_table = 
1.210     raeburn  1111:             &personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1112:                                    $inst_results{$ccuname.':'.$ccdomain});
1.206     raeburn  1113:         $r->print($personal_table);
1.187     raeburn  1114:         my ($home_server_pick,$numlib) = 
                   1115:             &Apache::loncommon::home_server_form_item($ccdomain,'hserver',
                   1116:                                                       'default','hide');
                   1117:         if ($numlib > 1) {
                   1118:             $r->print("
1.185     raeburn  1119: <br />
1.187     raeburn  1120: $lt{'hs'}: $home_server_pick
                   1121: <br />");
                   1122:         } else {
                   1123:             $r->print($home_server_pick);
                   1124:         }
1.304     raeburn  1125:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.318     raeburn  1126:             $r->print('<br /><h3>'.&mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.304     raeburn  1127:                       &Apache::loncommon::start_data_table().
                   1128:                       &build_tools_display($ccuname,$ccdomain,
                   1129:                                            'requestcourses').
                   1130:                       &Apache::loncommon::end_data_table());
                   1131:         }
1.188     raeburn  1132:         $r->print('</div>'."\n".'<div class="LC_left_float"><h3>'.
                   1133:                   $lt{'lg'}.'</h3>');
1.185     raeburn  1134:         my ($fixedauth,$varauth,$authmsg); 
1.193     raeburn  1135:         if (ref($rulematch{$ccuname.':'.$ccdomain}) eq 'HASH') {
                   1136:             my $matchedrule = $rulematch{$ccuname.':'.$ccdomain}{'username'};
                   1137:             my ($rules,$ruleorder) = 
                   1138:                 &Apache::lonnet::inst_userrules($ccdomain,'username');
1.185     raeburn  1139:             if (ref($rules) eq 'HASH') {
1.193     raeburn  1140:                 if (ref($rules->{$matchedrule}) eq 'HASH') {
                   1141:                     my $authtype = $rules->{$matchedrule}{'authtype'};
1.185     raeburn  1142:                     if ($authtype !~ /^(krb4|krb5|int|fsys|loc)$/) {
1.190     raeburn  1143:                         $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.275     raeburn  1144:                     } else { 
1.193     raeburn  1145:                         my $authparm = $rules->{$matchedrule}{'authparm'};
1.273     raeburn  1146:                         $authmsg = $rules->{$matchedrule}{'authmsg'};
1.185     raeburn  1147:                         if ($authtype =~ /^krb(4|5)$/) {
                   1148:                             my $ver = $1;
                   1149:                             if ($authparm ne '') {
                   1150:                                 $fixedauth = <<"KERB"; 
                   1151: <input type="hidden" name="login" value="krb" />
                   1152: <input type="hidden" name="krbver" value="$ver" />
                   1153: <input type="hidden" name="krbarg" value="$authparm" />
                   1154: KERB
                   1155:                             }
                   1156:                         } else {
                   1157:                             $fixedauth = 
                   1158: '<input type="hidden" name="login" value="'.$authtype.'" />'."\n";
1.193     raeburn  1159:                             if ($rules->{$matchedrule}{'authparmfixed'}) {
1.185     raeburn  1160:                                 $fixedauth .=    
                   1161: '<input type="hidden" name="'.$authtype.'arg" value="'.$authparm.'" />'."\n";
                   1162:                             } else {
1.273     raeburn  1163:                                 if ($authtype eq 'int') {
                   1164:                                     $varauth = '<br />'.
1.301     bisitz   1165: &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  1166:                                 } elsif ($authtype eq 'loc') {
                   1167:                                     $varauth = '<br />'.
                   1168: &mt('[_1] Local Authentication with argument [_2]','','<input type="text" name="'.$authtype.'arg" value="" />')."\n";
                   1169:                                 } else {
                   1170:                                     $varauth =
1.185     raeburn  1171: '<input type="text" name="'.$authtype.'arg" value="" />'."\n";
1.273     raeburn  1172:                                 }
1.185     raeburn  1173:                             }
                   1174:                         }
                   1175:                     }
                   1176:                 } else {
1.190     raeburn  1177:                     $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.185     raeburn  1178:                 }
                   1179:             }
                   1180:             if ($authmsg) {
                   1181:                 $r->print(<<ENDAUTH);
                   1182: $fixedauth
                   1183: $authmsg
                   1184: $varauth
                   1185: ENDAUTH
                   1186:             }
                   1187:         } else {
1.190     raeburn  1188:             $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc)); 
1.187     raeburn  1189:         }
1.215     raeburn  1190:         $r->print($portfolioform);
                   1191:         if ($env{'form.action'} eq 'singlestudent') {
                   1192:             $r->print(&date_sections_select($context,$newuser,$formname,
                   1193:                                             $permission));
                   1194:         }
                   1195:         $r->print('</div><div class="LC_clear_float_footer"></div>');
1.216     raeburn  1196:     } else { # user already exists
1.79      albertel 1197: 	my %lt=&Apache::lonlocal::texthash(
1.191     raeburn  1198:                     'cup'  => "Modify existing user: ",
1.213     raeburn  1199:                     'ens'  => "Enroll one student: ",
1.318     raeburn  1200:                     'enm'  => "Enroll one member: ",
1.72      sakharuk 1201:                     'id'   => "in domain",
                   1202: 				       );
1.26      matthew  1203: 	$r->print(<<ENDCHANGEUSER);
1.110     albertel 1204: $start_page
1.25      matthew  1205: $forminfo
1.213     raeburn  1206: <h2>
1.26      matthew  1207: ENDCHANGEUSER
1.213     raeburn  1208:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1209:             if ($crstype eq 'Community') {
                   1210:                 $r->print($lt{'enm'});
                   1211:             } else {
                   1212:                 $r->print($lt{'ens'});
                   1213:             }
1.213     raeburn  1214:         } else {
                   1215:             $r->print($lt{'cup'});
                   1216:         }
                   1217:         $r->print(' "'.$ccuname.'" '.$lt{'id'}.' "'.$ccdomain.'"</h2>'.
                   1218:                   "\n".'<div class="LC_left_float">');
1.206     raeburn  1219:         my ($personal_table,$showforceid) = 
1.210     raeburn  1220:             &personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1221:                                    $inst_results{$ccuname.':'.$ccdomain});
1.206     raeburn  1222:         $r->print($personal_table);
                   1223:         if ($showforceid) {
1.203     raeburn  1224:             $r->print(&Apache::lonuserutils::forceid_change($context));
1.199     raeburn  1225:         }
1.275     raeburn  1226:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.318     raeburn  1227:             $r->print('<h3>'.&mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.300     raeburn  1228:                       &Apache::loncommon::start_data_table());
1.314     raeburn  1229:             if ($env{'request.role.domain'} eq $ccdomain) {
1.300     raeburn  1230:                 $r->print(&build_tools_display($ccuname,$ccdomain,'requestcourses'));
                   1231:             } else {
                   1232:                 $r->print(&coursereq_externaluser($ccuname,$ccdomain,
                   1233:                                                   $env{'request.role.domain'}));
                   1234:             }
                   1235:             $r->print(&Apache::loncommon::end_data_table());
1.275     raeburn  1236:         }
1.199     raeburn  1237:         $r->print('</div>');
1.227     raeburn  1238:         my $user_auth_text =  &user_authentication($ccuname,$ccdomain,$formname);
1.275     raeburn  1239:         my ($user_quota_text,$user_tools_text,$user_reqcrs_text);
1.267     raeburn  1240:         if ((&Apache::lonnet::allowed('mpq',$ccdomain)) ||
                   1241:             (&Apache::lonnet::allowed('mut',$ccdomain))) {
1.188     raeburn  1242:             # Current user has quota modification privileges
                   1243:             $user_quota_text = &portfolio_quota($ccuname,$ccdomain);
1.267     raeburn  1244:         }
                   1245:         if (!&Apache::lonnet::allowed('mpq',$ccdomain)) {
                   1246:             if (&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) {
                   1247:                 # Get the user's portfolio information
                   1248:                 my %portq = &Apache::lonnet::get('environment',['portfolioquota'],
                   1249:                                                  $ccdomain,$ccuname);
                   1250:                 my %lt=&Apache::lonlocal::texthash(
                   1251:                     'dska'  => "Disk space allocated to user's portfolio files",
                   1252:                     'youd'  => "You do not have privileges to modify the portfolio quota for this user.",
                   1253:                     'ichr'  => "If a change is required, contact a domain coordinator for the domain",
                   1254:                 );
                   1255:                 $user_quota_text = <<ENDNOPORTPRIV;
1.188     raeburn  1256: <h3>$lt{'dska'}</h3>
                   1257: $lt{'youd'} $lt{'ichr'}: $ccdomain
                   1258: ENDNOPORTPRIV
1.267     raeburn  1259:             }
                   1260:         }
                   1261:         if (!&Apache::lonnet::allowed('mut',$ccdomain)) {
                   1262:             if (&Apache::lonnet::allowed('mut',$env{'request.role.domain'})) {
                   1263:                 my %lt=&Apache::lonlocal::texthash(
                   1264:                     'utav'  => "User Tools Availability",
1.285     weissno  1265:                     'yodo'  => "You do not have privileges to modify Portfolio, Blog or Personal Information Page settings for this user.",
1.267     raeburn  1266:                     'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   1267:                 );
                   1268:                 $user_tools_text = <<ENDNOTOOLSPRIV;
                   1269: <h3>$lt{'utav'}</h3>
                   1270: $lt{'yodo'} $lt{'ifch'}: $ccdomain
                   1271: ENDNOTOOLSPRIV
                   1272:             }
1.188     raeburn  1273:         }
                   1274:         if ($user_auth_text ne '') {
                   1275:             $r->print('<div class="LC_left_float">'.$user_auth_text);
                   1276:             if ($user_quota_text ne '') {
                   1277:                 $r->print($user_quota_text);
                   1278:             }
1.267     raeburn  1279:             if ($user_tools_text ne '') {
                   1280:                 $r->print($user_tools_text);
                   1281:             }
1.213     raeburn  1282:             if ($env{'form.action'} eq 'singlestudent') {
                   1283:                 $r->print(&date_sections_select($context,$newuser,$formname));
                   1284:             }
1.188     raeburn  1285:         } elsif ($user_quota_text ne '') {
1.213     raeburn  1286:             $r->print('<div class="LC_left_float">'.$user_quota_text);
1.267     raeburn  1287:             if ($user_tools_text ne '') {
                   1288:                 $r->print($user_tools_text);
                   1289:             }
                   1290:             if ($env{'form.action'} eq 'singlestudent') {
                   1291:                 $r->print(&date_sections_select($context,$newuser,$formname));
                   1292:             }
                   1293:         } elsif ($user_tools_text ne '') {
                   1294:             $r->print('<div class="LC_left_float">'.$user_tools_text);
1.213     raeburn  1295:             if ($env{'form.action'} eq 'singlestudent') {
                   1296:                 $r->print(&date_sections_select($context,$newuser,$formname));
                   1297:             }
                   1298:         } else {
                   1299:             if ($env{'form.action'} eq 'singlestudent') {
                   1300:                 $r->print('<div class="LC_left_float">'.
                   1301:                           &date_sections_select($context,$newuser,$formname));
                   1302:             }
1.188     raeburn  1303:         }
1.213     raeburn  1304:         $r->print('</div><div class="LC_clear_float_footer"></div>');
1.217     raeburn  1305:         if ($env{'form.action'} ne 'singlestudent') {
1.329     raeburn  1306:             &display_existing_roles($r,$ccuname,$ccdomain,\%inccourses,$context,
                   1307:                                     $roledom,$crstype);
1.217     raeburn  1308:         }
1.25      matthew  1309:     } ## End of new user/old user logic
1.218     raeburn  1310:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1311:         my $btntxt;
                   1312:         if ($crstype eq 'Community') {
                   1313:             $btntxt = &mt('Enroll Member');
                   1314:         } else {
                   1315:             $btntxt = &mt('Enroll Student');
                   1316:         }
                   1317:         $r->print('<br /><input type="button" value="'.$btntxt.'" onclick="setSections(this.form)" />'."\n");
1.218     raeburn  1318:     } else {
                   1319:         $r->print('<h3>'.&mt('Add Roles').'</h3>');
                   1320:         my $addrolesdisplay = 0;
                   1321:         if ($context eq 'domain' || $context eq 'author') {
                   1322:             $addrolesdisplay = &new_coauthor_roles($r,$ccuname,$ccdomain);
                   1323:         }
                   1324:         if ($context eq 'domain') {
                   1325:             my $add_domainroles = &new_domain_roles($r);
                   1326:             if (!$addrolesdisplay) {
                   1327:                 $addrolesdisplay = $add_domainroles;
1.2       www      1328:             }
1.218     raeburn  1329:             $r->print(&course_level_dc($env{'request.role.domain'},'Course'));
1.301     bisitz   1330:             $r->print('<br /><input type="button" value="'.&mt('Save').'" onclick="setCourse()" />'."\n");
1.218     raeburn  1331:         } elsif ($context eq 'author') {
                   1332:             if ($addrolesdisplay) {
1.262     schafran 1333:                 $r->print('<br /><input type="button" value="'.&mt('Save').'"');
1.218     raeburn  1334:                 if ($newuser) {
1.301     bisitz   1335:                     $r->print(' onclick="auth_check()" \>'."\n");
1.218     raeburn  1336:                 } else {
1.301     bisitz   1337:                     $r->print('onclick="this.form.submit()" \>'."\n");
1.218     raeburn  1338:                 }
1.188     raeburn  1339:             } else {
1.218     raeburn  1340:                 $r->print('<br /><a href="javascript:backPage(document.cu)">'.
                   1341:                           &mt('Back to previous page').'</a>');
1.188     raeburn  1342:             }
                   1343:         } else {
1.218     raeburn  1344:             $r->print(&course_level_table(%inccourses));
1.301     bisitz   1345:             $r->print('<br /><input type="button" value="'.&mt('Save').'" onclick="setSections(this.form)" />'."\n");
1.188     raeburn  1346:         }
1.88      raeburn  1347:     }
1.188     raeburn  1348:     $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','currstate','ccuname','ccdomain']));
1.179     raeburn  1349:     $r->print('<input type="hidden" name="currstate" value="" />');
1.352     raeburn  1350:     $r->print('<input type="hidden" name="prevphase" value="'.$env{'form.phase'}.'" /></form>');
1.218     raeburn  1351:     return;
1.2       www      1352: }
1.1       www      1353: 
1.213     raeburn  1354: sub singleuser_breadcrumb {
1.318     raeburn  1355:     my ($crstype) = @_;
1.213     raeburn  1356:     my %breadcrumb_text;
                   1357:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1358:         if ($crstype eq 'Community') {
                   1359:             $breadcrumb_text{'search'} = 'Enroll a member';
                   1360:         } else {
                   1361:             $breadcrumb_text{'search'} = 'Enroll a student';
                   1362:         }
1.213     raeburn  1363:         $breadcrumb_text{'userpicked'} = 'Select a user',
                   1364:         $breadcrumb_text{'modify'} = 'Set section/dates',
                   1365:     } else {
1.229     raeburn  1366:         $breadcrumb_text{'search'} = 'Create/modify a user';
1.213     raeburn  1367:         $breadcrumb_text{'userpicked'} = 'Select a user',
                   1368:         $breadcrumb_text{'modify'} = 'Set user role',
                   1369:     }
                   1370:     return %breadcrumb_text;
                   1371: }
                   1372: 
                   1373: sub date_sections_select {
                   1374:     my ($context,$newuser,$formname,$permission) = @_;
                   1375:     my $cid = $env{'request.course.id'};
                   1376:     my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity($cid);
                   1377:     my $date_table = '<h3>'.&mt('Starting and Ending Dates').'</h3>'."\n".
                   1378:         &Apache::lonuserutils::date_setting_table(undef,undef,$context,
                   1379:                                                   undef,$formname,$permission);
                   1380:     my $rowtitle = 'Section';
                   1381:     my $secbox = '<h3>'.&mt('Section').'</h3>'."\n".
                   1382:         &Apache::lonuserutils::section_picker($cdom,$cnum,'st',$rowtitle,
                   1383:                                               $permission);
                   1384:     my $output = $date_table.$secbox;
                   1385:     return $output;
                   1386: }
                   1387: 
1.216     raeburn  1388: sub validation_javascript {
                   1389:     my ($context,$ccdomain,$pjump_def,$groupslist,$newuser,$formname,
                   1390:         $loaditem) = @_;
                   1391:     my $dc_setcourse_code = '';
                   1392:     my $nondc_setsection_code = '';
                   1393:     if ($context eq 'domain') {
                   1394:         my $dcdom = $env{'request.role.domain'};
                   1395:         $loaditem->{'onload'} = "document.cu.coursedesc.value='';";
1.227     raeburn  1396:         $dc_setcourse_code = 
                   1397:             &Apache::lonuserutils::dc_setcourse_js('cu','singleuser',$context);
1.216     raeburn  1398:     } else {
1.227     raeburn  1399:         my $checkauth; 
                   1400:         if (($newuser) || (&Apache::lonnet::allowed('mau',$ccdomain))) {
                   1401:             $checkauth = 1;
                   1402:         }
                   1403:         if ($context eq 'course') {
                   1404:             $nondc_setsection_code =
                   1405:                 &Apache::lonuserutils::setsections_javascript($formname,$groupslist,
                   1406:                                                               undef,$checkauth);
                   1407:         }
                   1408:         if ($checkauth) {
                   1409:             $nondc_setsection_code .= 
                   1410:                 &Apache::lonuserutils::verify_authen($formname,$context);
                   1411:         }
1.216     raeburn  1412:     }
                   1413:     my $js = &user_modification_js($pjump_def,$dc_setcourse_code,
                   1414:                                    $nondc_setsection_code,$groupslist);
                   1415:     my ($jsback,$elements) = &crumb_utilities();
                   1416:     $js .= "\n".
1.301     bisitz   1417:            '<script type="text/javascript">'."\n".
                   1418:            '// <![CDATA['."\n".
                   1419:            $jsback."\n".
                   1420:            '// ]]>'."\n".
                   1421:            '</script>'."\n";
1.216     raeburn  1422:     return $js;
                   1423: }
                   1424: 
1.217     raeburn  1425: sub display_existing_roles {
1.329     raeburn  1426:     my ($r,$ccuname,$ccdomain,$inccourses,$context,$roledom,$crstype) = @_;
                   1427:     my $now=time;
                   1428:     my %lt=&Apache::lonlocal::texthash(
1.217     raeburn  1429:                     'rer'  => "Existing Roles",
                   1430:                     'rev'  => "Revoke",
                   1431:                     'del'  => "Delete",
                   1432:                     'ren'  => "Re-Enable",
                   1433:                     'rol'  => "Role",
                   1434:                     'ext'  => "Extent",
                   1435:                     'sta'  => "Start",
                   1436:                     'end'  => "End",
                   1437:                                        );
1.329     raeburn  1438:     my (%rolesdump,%roletext,%sortrole,%roleclass,%rolepriv);
                   1439:     if ($context eq 'course' || $context eq 'author') {
                   1440:         my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
                   1441:         my %roleshash = 
                   1442:             &Apache::lonnet::get_my_roles($ccuname,$ccdomain,'userroles',
                   1443:                               ['active','previous','future'],\@roles,$roledom,1);
                   1444:         foreach my $key (keys(%roleshash)) {
                   1445:             my ($start,$end) = split(':',$roleshash{$key});
                   1446:             next if ($start eq '-1' || $end eq '-1');
                   1447:             my ($rnum,$rdom,$role,$sec) = split(':',$key);
                   1448:             if ($context eq 'course') {
                   1449:                 next unless (($rnum eq $env{'course.'.$env{'request.course.id'}.'.num'})
                   1450:                              && ($rdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}));
                   1451:             } elsif ($context eq 'author') {
                   1452:                 next unless (($rnum eq $env{'user.name'}) && ($rdom eq $env{'request.role.domain'}));
                   1453:             }
                   1454:             my ($newkey,$newvalue,$newrole);
                   1455:             $newkey = '/'.$rdom.'/'.$rnum;
                   1456:             if ($sec ne '') {
                   1457:                 $newkey .= '/'.$sec;
                   1458:             }
                   1459:             $newvalue = $role;
                   1460:             if ($role =~ /^cr/) {
                   1461:                 $newrole = 'cr';
                   1462:             } else {
                   1463:                 $newrole = $role;
                   1464:             }
                   1465:             $newkey .= '_'.$newrole;
                   1466:             if ($start ne '' && $end ne '') {
                   1467:                 $newvalue .= '_'.$end.'_'.$start;
1.335     raeburn  1468:             } elsif ($end ne '') {
                   1469:                 $newvalue .= '_'.$end;
1.329     raeburn  1470:             }
                   1471:             $rolesdump{$newkey} = $newvalue;
                   1472:         }
                   1473:     } else {
1.350     raeburn  1474:         my $extra = &Apache::lonnet::freeze_escape({'skipcheck' => 1});
                   1475:         %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname,'.',undef,$extra);
1.329     raeburn  1476:     }
                   1477:     # Build up table of user roles to allow revocation and re-enabling of roles.
                   1478:     my ($tmp) = keys(%rolesdump);
                   1479:     return if ($tmp =~ /^(con_lost|error)/i);
                   1480:     foreach my $area (sort { my $a1=join('_',(split('_',$a))[1,0]);
                   1481:                                 my $b1=join('_',(split('_',$b))[1,0]);
                   1482:                                 return $a1 cmp $b1;
                   1483:                             } keys(%rolesdump)) {
                   1484:         next if ($area =~ /^rolesdef/);
                   1485:         my $envkey=$area;
                   1486:         my $role = $rolesdump{$area};
                   1487:         my $thisrole=$area;
                   1488:         $area =~ s/\_\w\w$//;
                   1489:         my ($role_code,$role_end_time,$role_start_time) =
                   1490:             split(/_/,$role);
1.217     raeburn  1491: # Is this a custom role? Get role owner and title.
1.329     raeburn  1492:         my ($croleudom,$croleuname,$croletitle)=
                   1493:             ($role_code=~m{^cr/($match_domain)/($match_username)/(\w+)$});
                   1494:         my $allowed=0;
                   1495:         my $delallowed=0;
                   1496:         my $sortkey=$role_code;
                   1497:         my $class='Unknown';
                   1498:         if ($area =~ m{^/($match_domain)/($match_courseid)} ) {
                   1499:             $class='Course';
                   1500:             my ($coursedom,$coursedir) = ($1,$2);
                   1501:             my $cid = $1.'_'.$2;
                   1502:             # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
                   1503:             my %coursedata=
                   1504:                 &Apache::lonnet::coursedescription($cid);
                   1505:             if ($coursedir =~ /^$match_community$/) {
                   1506:                 $class='Community';
                   1507:             }
                   1508:             $sortkey.="\0$coursedom";
                   1509:             my $carea;
                   1510:             if (defined($coursedata{'description'})) {
                   1511:                 $carea=$coursedata{'description'}.
                   1512:                     '<br />'.&mt('Domain').': '.$coursedom.('&nbsp;'x8).
                   1513:     &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$coursedir,$coursedom);
                   1514:                 $sortkey.="\0".$coursedata{'description'};
                   1515:             } else {
                   1516:                 if ($class eq 'Community') {
                   1517:                     $carea=&mt('Unavailable community').': '.$area;
                   1518:                     $sortkey.="\0".&mt('Unavailable community').': '.$area;
1.217     raeburn  1519:                 } else {
                   1520:                     $carea=&mt('Unavailable course').': '.$area;
                   1521:                     $sortkey.="\0".&mt('Unavailable course').': '.$area;
                   1522:                 }
1.329     raeburn  1523:             }
                   1524:             $sortkey.="\0$coursedir";
                   1525:             $inccourses->{$cid}=1;
                   1526:             if ((&Apache::lonnet::allowed('c'.$role_code,$coursedom.'/'.$coursedir)) ||
                   1527:                 (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1528:                 $allowed=1;
                   1529:             }
                   1530:             unless ($allowed) {
                   1531:                 my $isowner = &is_courseowner($cid,$coursedata{'internal.courseowner'});
                   1532:                 if ($isowner) {
                   1533:                     if (($role_code eq 'co') && ($class eq 'Community')) {
                   1534:                         $allowed = 1;
                   1535:                     } elsif (($role_code eq 'cc') && ($class eq 'Course')) {
                   1536:                         $allowed = 1;
                   1537:                     }
1.217     raeburn  1538:                 }
1.329     raeburn  1539:             } 
                   1540:             if ((&Apache::lonnet::allowed('dro',$coursedom)) ||
                   1541:                 (&Apache::lonnet::allowed('dro',$ccdomain))) {
                   1542:                 $delallowed=1;
                   1543:             }
1.217     raeburn  1544: # - custom role. Needs more info, too
1.329     raeburn  1545:             if ($croletitle) {
                   1546:                 if (&Apache::lonnet::allowed('ccr',$coursedom.'/'.$coursedir)) {
                   1547:                     $allowed=1;
                   1548:                     $thisrole.='.'.$role_code;
1.217     raeburn  1549:                 }
1.329     raeburn  1550:             }
                   1551:             if ($area=~m{^/($match_domain)/($match_courseid)/(\w+)}) {
                   1552:                 $carea.='<br />Section: '.$3;
                   1553:                 $sortkey.="\0$3";
                   1554:                 if (!$allowed) {
                   1555:                     if ($env{'request.course.sec'} eq $3) {
                   1556:                         if (&Apache::lonnet::allowed('c'.$role_code,$1.'/'.$2.'/'.$3)) {
                   1557:                             $allowed = 1;
1.217     raeburn  1558:                         }
                   1559:                     }
                   1560:                 }
1.329     raeburn  1561:             }
                   1562:             $area=$carea;
                   1563:         } else {
                   1564:             $sortkey.="\0".$area;
                   1565:             # Determine if current user is able to revoke privileges
                   1566:             if ($area=~m{^/($match_domain)/}) {
                   1567:                 if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
                   1568:                    (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1569:                    $allowed=1;
1.217     raeburn  1570:                 }
1.329     raeburn  1571:                 if (((&Apache::lonnet::allowed('dro',$1))  ||
                   1572:                     (&Apache::lonnet::allowed('dro',$ccdomain))) &&
                   1573:                     ($role_code ne 'dc')) {
                   1574:                     $delallowed=1;
1.217     raeburn  1575:                 }
1.329     raeburn  1576:             } else {
                   1577:                 if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
1.217     raeburn  1578:                     $allowed=1;
                   1579:                 }
                   1580:             }
1.329     raeburn  1581:             if ($role_code eq 'ca' || $role_code eq 'au') {
                   1582:                 $class='Construction Space';
                   1583:             } elsif ($role_code eq 'su') {
                   1584:                 $class='System';
1.217     raeburn  1585:             } else {
1.329     raeburn  1586:                 $class='Domain';
1.217     raeburn  1587:             }
1.329     raeburn  1588:         }
                   1589:         if (($role_code eq 'ca') || ($role_code eq 'aa')) {
                   1590:             $area=~m{/($match_domain)/($match_username)};
                   1591:             if (&Apache::lonuserutils::authorpriv($2,$1)) {
                   1592:                 $allowed=1;
1.217     raeburn  1593:             } else {
1.329     raeburn  1594:                 $allowed=0;
1.217     raeburn  1595:             }
1.329     raeburn  1596:         }
                   1597:         my $row = '';
                   1598:         $row.= '<td>';
                   1599:         my $active=1;
                   1600:         $active=0 if (($role_end_time) && ($now>$role_end_time));
                   1601:         if (($active) && ($allowed)) {
                   1602:             $row.= '<input type="checkbox" name="rev:'.$thisrole.'" />';
                   1603:         } else {
                   1604:             if ($active) {
                   1605:                $row.='&nbsp;';
1.217     raeburn  1606:             } else {
1.329     raeburn  1607:                $row.=&mt('expired or revoked');
1.217     raeburn  1608:             }
1.329     raeburn  1609:         }
                   1610:         $row.='</td><td>';
                   1611:         if ($allowed && !$active) {
                   1612:             $row.= '<input type="checkbox" name="ren:'.$thisrole.'" />';
                   1613:         } else {
                   1614:             $row.='&nbsp;';
                   1615:         }
                   1616:         $row.='</td><td>';
                   1617:         if ($delallowed) {
                   1618:             $row.= '<input type="checkbox" name="del:'.$thisrole.'" />';
                   1619:         } else {
                   1620:             $row.='&nbsp;';
                   1621:         }
                   1622:         my $plaintext='';
                   1623:         if (!$croletitle) {
                   1624:             $plaintext=&Apache::lonnet::plaintext($role_code,$class)
                   1625:         } else {
                   1626:             $plaintext=
1.346     bisitz   1627:                 &mt('Customrole [_1][_2]defined by [_3]',
                   1628:                         '"'.$croletitle.'"',
                   1629:                         '<br />',
                   1630:                         $croleuname.':'.$croleudom);
1.329     raeburn  1631:         }
                   1632:         $row.= '</td><td>'.$plaintext.
                   1633:                '</td><td>'.$area.
                   1634:                '</td><td>'.($role_start_time?&Apache::lonlocal::locallocaltime($role_start_time)
                   1635:                                             : '&nbsp;' ).
                   1636:                '</td><td>'.($role_end_time  ?&Apache::lonlocal::locallocaltime($role_end_time)
                   1637:                                             : '&nbsp;' )
                   1638:                ."</td>";
                   1639:         $sortrole{$sortkey}=$envkey;
                   1640:         $roletext{$envkey}=$row;
                   1641:         $roleclass{$envkey}=$class;
                   1642:         $rolepriv{$envkey}=$allowed;
                   1643:     } # end of foreach        (table building loop)
                   1644: 
                   1645:     my $rolesdisplay = 0;
                   1646:     my %output = ();
                   1647:     foreach my $type ('Construction Space','Course','Community','Domain','System','Unknown') {
                   1648:         $output{$type} = '';
                   1649:         foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                   1650:             if ( ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/ ) && ($rolepriv{$sortrole{$which}}) ) {
                   1651:                  $output{$type}.=
                   1652:                       &Apache::loncommon::start_data_table_row().
                   1653:                       $roletext{$sortrole{$which}}.
                   1654:                       &Apache::loncommon::end_data_table_row();
1.217     raeburn  1655:             }
1.329     raeburn  1656:         }
                   1657:         unless($output{$type} eq '') {
                   1658:             $output{$type} = '<tr class="LC_info_row">'.
                   1659:                       "<td align='center' colspan='7'>".&mt($type)."</td></tr>".
                   1660:                       $output{$type};
                   1661:             $rolesdisplay = 1;
                   1662:         }
                   1663:     }
                   1664:     if ($rolesdisplay == 1) {
                   1665:         my $contextrole='';
                   1666:         if ($env{'request.course.id'}) {
                   1667:             if (&Apache::loncommon::course_type() eq 'Community') {
                   1668:                 $contextrole = &mt('Existing Roles in this Community');
1.290     bisitz   1669:             } else {
1.329     raeburn  1670:                 $contextrole = &mt('Existing Roles in this Course');
1.290     bisitz   1671:             }
1.329     raeburn  1672:         } elsif ($env{'request.role'} =~ /^au\./) {
                   1673:             $contextrole = &mt('Existing Co-Author Roles in your Construction Space');
                   1674:         } else {
                   1675:             $contextrole = &mt('Existing Roles in this Domain');
                   1676:         }
                   1677:         $r->print('
1.217     raeburn  1678: <h3>'.$lt{'rer'}.'</h3>'.
1.329     raeburn  1679: '<div>'.$contextrole.'</div>'.
1.217     raeburn  1680: &Apache::loncommon::start_data_table("LC_createuser").
                   1681: &Apache::loncommon::start_data_table_header_row().
                   1682: '<th>'.$lt{'rev'}.'</th><th>'.$lt{'ren'}.'</th><th>'.$lt{'del'}.
                   1683: '</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.
                   1684: '</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
                   1685: &Apache::loncommon::end_data_table_header_row());
1.329     raeburn  1686:         foreach my $type ('Construction Space','Course','Community','Domain','System','Unknown') {
                   1687:             if ($output{$type}) {
                   1688:                 $r->print($output{$type}."\n");
1.217     raeburn  1689:             }
                   1690:         }
1.329     raeburn  1691:         $r->print(&Apache::loncommon::end_data_table());
                   1692:     }
1.217     raeburn  1693:     return;
                   1694: }
                   1695: 
1.218     raeburn  1696: sub new_coauthor_roles {
                   1697:     my ($r,$ccuname,$ccdomain) = @_;
                   1698:     my $addrolesdisplay = 0;
                   1699:     #
                   1700:     # Co-Author
                   1701:     #
                   1702:     if (&Apache::lonuserutils::authorpriv($env{'user.name'},
                   1703:                                           $env{'request.role.domain'}) &&
                   1704:         ($env{'user.name'} ne $ccuname || $env{'user.domain'} ne $ccdomain)) {
                   1705:         # No sense in assigning co-author role to yourself
                   1706:         $addrolesdisplay = 1;
                   1707:         my $cuname=$env{'user.name'};
                   1708:         my $cudom=$env{'request.role.domain'};
                   1709:         my %lt=&Apache::lonlocal::texthash(
                   1710:                     'cs'   => "Construction Space",
                   1711:                     'act'  => "Activate",
                   1712:                     'rol'  => "Role",
                   1713:                     'ext'  => "Extent",
                   1714:                     'sta'  => "Start",
                   1715:                     'end'  => "End",
                   1716:                     'cau'  => "Co-Author",
                   1717:                     'caa'  => "Assistant Co-Author",
                   1718:                     'ssd'  => "Set Start Date",
                   1719:                     'sed'  => "Set End Date"
                   1720:                                        );
                   1721:         $r->print('<h4>'.$lt{'cs'}.'</h4>'."\n".
                   1722:                   &Apache::loncommon::start_data_table()."\n".
                   1723:                   &Apache::loncommon::start_data_table_header_row()."\n".
                   1724:                   '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'.
                   1725:                   '<th>'.$lt{'ext'}.'</th><th>'.$lt{'sta'}.'</th>'.
                   1726:                   '<th>'.$lt{'end'}.'</th>'."\n".
                   1727:                   &Apache::loncommon::end_data_table_header_row()."\n".
                   1728:                   &Apache::loncommon::start_data_table_row().'
                   1729:            <td>
1.291     bisitz   1730:             <input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_ca" />
1.218     raeburn  1731:            </td>
                   1732:            <td>'.$lt{'cau'}.'</td>
                   1733:            <td>'.$cudom.'_'.$cuname.'</td>
                   1734:            <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_ca" value="" />
                   1735:              <a href=
                   1736: "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>
                   1737: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_ca" value="" />
                   1738: <a href=
                   1739: "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".
                   1740:               &Apache::loncommon::end_data_table_row()."\n".
                   1741:               &Apache::loncommon::start_data_table_row()."\n".
1.291     bisitz   1742: '<td><input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_aa" /></td>
1.218     raeburn  1743: <td>'.$lt{'caa'}.'</td>
                   1744: <td>'.$cudom.'_'.$cuname.'</td>
                   1745: <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_aa" value="" />
                   1746: <a href=
                   1747: "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>
                   1748: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_aa" value="" />
                   1749: <a href=
                   1750: "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".
                   1751:              &Apache::loncommon::end_data_table_row()."\n".
                   1752:              &Apache::loncommon::end_data_table());
                   1753:     } elsif ($env{'request.role'} =~ /^au\./) {
                   1754:         if (!(&Apache::lonuserutils::authorpriv($env{'user.name'},
                   1755:                                                 $env{'request.role.domain'}))) {
                   1756:             $r->print('<span class="LC_error">'.
                   1757:                       &mt('You do not have privileges to assign co-author roles.').
                   1758:                       '</span>');
                   1759:         } elsif (($env{'user.name'} eq $ccuname) &&
                   1760:              ($env{'user.domain'} eq $ccdomain)) {
                   1761:             $r->print(&mt('Assigning yourself a co-author or assistant co-author role in your own author area in Construction Space is not permitted'));
                   1762:         }
                   1763:     }
                   1764:     return $addrolesdisplay;;
                   1765: }
                   1766: 
                   1767: sub new_domain_roles {
                   1768:     my ($r) = @_;
                   1769:     my $addrolesdisplay = 0;
                   1770:     #
                   1771:     # Domain level
                   1772:     #
                   1773:     my $num_domain_level = 0;
                   1774:     my $domaintext =
                   1775:     '<h4>'.&mt('Domain Level').'</h4>'.
                   1776:     &Apache::loncommon::start_data_table().
                   1777:     &Apache::loncommon::start_data_table_header_row().
                   1778:     '<th>'.&mt('Activate').'</th><th>'.&mt('Role').'</th><th>'.
                   1779:     &mt('Extent').'</th>'.
                   1780:     '<th>'.&mt('Start').'</th><th>'.&mt('End').'</th>'.
                   1781:     &Apache::loncommon::end_data_table_header_row();
1.312     raeburn  1782:     my @allroles = &Apache::lonuserutils::roles_by_context('domain');
1.218     raeburn  1783:     foreach my $thisdomain (sort(&Apache::lonnet::all_domains())) {
1.312     raeburn  1784:         foreach my $role (@allroles) {
                   1785:             next if ($role eq 'ad');
1.218     raeburn  1786:             if (&Apache::lonnet::allowed('c'.$role,$thisdomain)) {
                   1787:                my $plrole=&Apache::lonnet::plaintext($role);
                   1788:                my %lt=&Apache::lonlocal::texthash(
                   1789:                     'ssd'  => "Set Start Date",
                   1790:                     'sed'  => "Set End Date"
                   1791:                                        );
                   1792:                $num_domain_level ++;
                   1793:                $domaintext .=
                   1794: &Apache::loncommon::start_data_table_row().
1.291     bisitz   1795: '<td><input type="checkbox" name="act_'.$thisdomain.'_'.$role.'" /></td>
1.218     raeburn  1796: <td>'.$plrole.'</td>
                   1797: <td>'.$thisdomain.'</td>
                   1798: <td><input type="hidden" name="start_'.$thisdomain.'_'.$role.'" value="" />
                   1799: <a href=
                   1800: "javascript:pjump('."'date_start','Start Date $plrole',document.cu.start_$thisdomain\_$role.value,'start_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
                   1801: <td><input type="hidden" name="end_'.$thisdomain.'_'.$role.'" value="" />
                   1802: <a href=
                   1803: "javascript:pjump('."'date_end','End Date $plrole',document.cu.end_$thisdomain\_$role.value,'end_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'.
                   1804: &Apache::loncommon::end_data_table_row();
                   1805:             }
                   1806:         }
                   1807:     }
                   1808:     $domaintext.= &Apache::loncommon::end_data_table();
                   1809:     if ($num_domain_level > 0) {
                   1810:         $r->print($domaintext);
                   1811:         $addrolesdisplay = 1;
                   1812:     }
                   1813:     return $addrolesdisplay;
                   1814: }
                   1815: 
1.188     raeburn  1816: sub user_authentication {
1.227     raeburn  1817:     my ($ccuname,$ccdomain,$formname) = @_;
1.188     raeburn  1818:     my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
1.227     raeburn  1819:     my $outcome;
1.188     raeburn  1820:     # Check for a bad authentication type
                   1821:     if ($currentauth !~ /^(krb4|krb5|unix|internal|localauth):/) {
                   1822:         # bad authentication scheme
                   1823:         my %lt=&Apache::lonlocal::texthash(
                   1824:                        'err'   => "ERROR",
                   1825:                        'uuas'  => "This user has an unrecognized authentication scheme",
                   1826:                        'adcs'  => "Please alert a domain coordinator of this situation",
                   1827:                        'sldb'  => "Please specify login data below",
                   1828:                        'ld'    => "Login Data"
                   1829:         );
                   1830:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
1.227     raeburn  1831:             &initialize_authen_forms($ccdomain,$formname);
                   1832: 
1.190     raeburn  1833:             my $choices = &Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc);
1.188     raeburn  1834:             $outcome = <<ENDBADAUTH;
                   1835: <script type="text/javascript" language="Javascript">
1.301     bisitz   1836: // <![CDATA[
1.188     raeburn  1837: $loginscript
1.301     bisitz   1838: // ]]>
1.188     raeburn  1839: </script>
                   1840: <span class="LC_error">$lt{'err'}:
                   1841: $lt{'uuas'} ($currentauth). $lt{'sldb'}.</span>
                   1842: <h3>$lt{'ld'}</h3>
                   1843: $choices
                   1844: ENDBADAUTH
                   1845:         } else {
                   1846:             # This user is not allowed to modify the user's
                   1847:             # authentication scheme, so just notify them of the problem
                   1848:             $outcome = <<ENDBADAUTH;
                   1849: <span class="LC_error"> $lt{'err'}: 
                   1850: $lt{'uuas'} ($currentauth). $lt{'adcs'}.
                   1851: </span>
                   1852: ENDBADAUTH
                   1853:         }
                   1854:     } else { # Authentication type is valid
1.227     raeburn  1855:         &initialize_authen_forms($ccdomain,$formname,$currentauth,'modifyuser');
1.205     raeburn  1856:         my ($authformcurrent,$can_modify,@authform_others) =
1.188     raeburn  1857:             &modify_login_block($ccdomain,$currentauth);
                   1858:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
                   1859:             # Current user has login modification privileges
                   1860:             my %lt=&Apache::lonlocal::texthash (
                   1861:                            'ld'    => "Login Data",
                   1862:                            'ccld'  => "Change Current Login Data",
                   1863:                            'enld'  => "Enter New Login Data"
                   1864:                                                );
                   1865:             $outcome =
                   1866:                        '<script type="text/javascript" language="Javascript">'."\n".
1.301     bisitz   1867:                        '// <![CDATA['."\n".
1.188     raeburn  1868:                        $loginscript."\n".
1.301     bisitz   1869:                        '// ]]>'."\n".
1.188     raeburn  1870:                        '</script>'."\n".
                   1871:                        '<h3>'.$lt{'ld'}.'</h3>'.
                   1872:                        &Apache::loncommon::start_data_table().
1.205     raeburn  1873:                        &Apache::loncommon::start_data_table_row().
1.188     raeburn  1874:                        '<td>'.$authformnop;
                   1875:             if ($can_modify) {
                   1876:                 $outcome .= '</td>'."\n".
                   1877:                             &Apache::loncommon::end_data_table_row().
                   1878:                             &Apache::loncommon::start_data_table_row().
                   1879:                             '<td>'.$authformcurrent.'</td>'.
                   1880:                             &Apache::loncommon::end_data_table_row()."\n";
                   1881:             } else {
1.200     raeburn  1882:                 $outcome .= '&nbsp;('.$authformcurrent.')</td>'.
                   1883:                             &Apache::loncommon::end_data_table_row()."\n";
1.188     raeburn  1884:             }
1.205     raeburn  1885:             foreach my $item (@authform_others) { 
                   1886:                 $outcome .= &Apache::loncommon::start_data_table_row().
                   1887:                             '<td>'.$item.'</td>'.
                   1888:                             &Apache::loncommon::end_data_table_row()."\n";
1.188     raeburn  1889:             }
1.205     raeburn  1890:             $outcome .= &Apache::loncommon::end_data_table();
1.188     raeburn  1891:         } else {
                   1892:             if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
                   1893:                 my %lt=&Apache::lonlocal::texthash(
                   1894:                            'ccld'  => "Change Current Login Data",
                   1895:                            'yodo'  => "You do not have privileges to modify the authentication configuration for this user.",
                   1896:                            'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   1897:                 );
                   1898:                 $outcome .= <<ENDNOPRIV;
                   1899: <h3>$lt{'ccld'}</h3>
                   1900: $lt{'yodo'} $lt{'ifch'}: $ccdomain
1.235     raeburn  1901: <input type="hidden" name="login" value="nochange" />
1.188     raeburn  1902: ENDNOPRIV
                   1903:             }
                   1904:         }
                   1905:     }  ## End of "check for bad authentication type" logic
                   1906:     return $outcome;
                   1907: }
                   1908: 
1.187     raeburn  1909: sub modify_login_block {
                   1910:     my ($dom,$currentauth) = @_;
                   1911:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   1912:     my ($authnum,%can_assign) =
                   1913:         &Apache::loncommon::get_assignable_auth($dom);
1.205     raeburn  1914:     my ($authformcurrent,@authform_others,$show_override_msg);
1.187     raeburn  1915:     if ($currentauth=~/^krb(4|5):/) {
                   1916:         $authformcurrent=$authformkrb;
                   1917:         if ($can_assign{'int'}) {
1.205     raeburn  1918:             push(@authform_others,$authformint);
1.187     raeburn  1919:         }
                   1920:         if ($can_assign{'loc'}) {
1.205     raeburn  1921:             push(@authform_others,$authformloc);
1.187     raeburn  1922:         }
                   1923:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   1924:             $show_override_msg = 1;
                   1925:         }
                   1926:     } elsif ($currentauth=~/^internal:/) {
                   1927:         $authformcurrent=$authformint;
                   1928:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  1929:             push(@authform_others,$authformkrb);
1.187     raeburn  1930:         }
                   1931:         if ($can_assign{'loc'}) {
1.205     raeburn  1932:             push(@authform_others,$authformloc);
1.187     raeburn  1933:         }
                   1934:         if ($can_assign{'int'}) {
                   1935:             $show_override_msg = 1;
                   1936:         }
                   1937:     } elsif ($currentauth=~/^unix:/) {
                   1938:         $authformcurrent=$authformfsys;
                   1939:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  1940:             push(@authform_others,$authformkrb);
1.187     raeburn  1941:         }
                   1942:         if ($can_assign{'int'}) {
1.205     raeburn  1943:             push(@authform_others,$authformint);
1.187     raeburn  1944:         }
                   1945:         if ($can_assign{'loc'}) {
1.205     raeburn  1946:             push(@authform_others,$authformloc);
1.187     raeburn  1947:         }
                   1948:         if ($can_assign{'fsys'}) {
                   1949:             $show_override_msg = 1;
                   1950:         }
                   1951:     } elsif ($currentauth=~/^localauth:/) {
                   1952:         $authformcurrent=$authformloc;
                   1953:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  1954:             push(@authform_others,$authformkrb);
1.187     raeburn  1955:         }
                   1956:         if ($can_assign{'int'}) {
1.205     raeburn  1957:             push(@authform_others,$authformint);
1.187     raeburn  1958:         }
                   1959:         if ($can_assign{'loc'}) {
                   1960:             $show_override_msg = 1;
                   1961:         }
                   1962:     }
                   1963:     if ($show_override_msg) {
1.205     raeburn  1964:         $authformcurrent = '<table><tr><td colspan="3">'.$authformcurrent.
                   1965:                            '</td></tr>'."\n".
                   1966:                            '<tr><td>&nbsp;&nbsp;&nbsp;</td>'.
                   1967:                            '<td><b>'.&mt('Currently in use').'</b></td>'.
                   1968:                            '<td align="right"><span class="LC_cusr_emph">'.
1.187     raeburn  1969:                             &mt('will override current values').
1.205     raeburn  1970:                             '</span></td></tr></table>';
1.187     raeburn  1971:     }
1.205     raeburn  1972:     return ($authformcurrent,$show_override_msg,@authform_others); 
1.187     raeburn  1973: }
                   1974: 
1.188     raeburn  1975: sub personal_data_display {
1.252     raeburn  1976:     my ($ccuname,$ccdomain,$newuser,$context,$inst_results,$rolesarray) = @_;
1.286     raeburn  1977:     my ($output,$showforceid,%userenv,%canmodify,%canmodify_status);
1.219     raeburn  1978:     my @userinfo = ('firstname','middlename','lastname','generation',
                   1979:                     'permanentemail','id');
1.252     raeburn  1980:     my $rowcount = 0;
                   1981:     my $editable = 0;
1.286     raeburn  1982:     %canmodify_status = 
                   1983:         &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
                   1984:                                                    ['inststatus'],$rolesarray);
1.253     raeburn  1985:     if (!$newuser) {
1.188     raeburn  1986:         # Get the users information
                   1987:         %userenv = &Apache::lonnet::get('environment',
                   1988:                    ['firstname','middlename','lastname','generation',
1.286     raeburn  1989:                     'permanentemail','id','inststatus'],$ccdomain,$ccuname);
1.219     raeburn  1990:         %canmodify =
                   1991:             &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
1.252     raeburn  1992:                                                        \@userinfo,$rolesarray);
1.257     raeburn  1993:     } elsif ($context eq 'selfcreate') {
                   1994:         %canmodify = &selfcreate_canmodify($context,$ccdomain,\@userinfo,
                   1995:                                            $inst_results,$rolesarray);
1.188     raeburn  1996:     }
                   1997:     my %lt=&Apache::lonlocal::texthash(
                   1998:                 'pd'             => "Personal Data",
                   1999:                 'firstname'      => "First Name",
                   2000:                 'middlename'     => "Middle Name",
                   2001:                 'lastname'       => "Last Name",
                   2002:                 'generation'     => "Generation",
                   2003:                 'permanentemail' => "Permanent e-mail address",
1.259     bisitz   2004:                 'id'             => "Student/Employee ID",
1.286     raeburn  2005:                 'lg'             => "Login Data",
                   2006:                 'inststatus'     => "Affiliation",
1.188     raeburn  2007:     );
                   2008:     my %textboxsize = (
                   2009:                        firstname      => '15',
                   2010:                        middlename     => '15',
                   2011:                        lastname       => '15',
                   2012:                        generation     => '5',
                   2013:                        permanentemail => '25',
                   2014:                        id             => '15',
                   2015:                       );
                   2016:     my $genhelp=&Apache::loncommon::help_open_topic('Generation');
                   2017:     $output = '<h3>'.$lt{'pd'}.'</h3>'.
                   2018:               &Apache::lonhtmlcommon::start_pick_box();
                   2019:     foreach my $item (@userinfo) {
                   2020:         my $rowtitle = $lt{$item};
1.252     raeburn  2021:         my $hiderow = 0;
1.188     raeburn  2022:         if ($item eq 'generation') {
                   2023:             $rowtitle = $genhelp.$rowtitle;
                   2024:         }
1.252     raeburn  2025:         my $row = &Apache::lonhtmlcommon::row_title($rowtitle,undef,'LC_oddrow_value')."\n";
1.188     raeburn  2026:         if ($newuser) {
1.210     raeburn  2027:             if (ref($inst_results) eq 'HASH') {
                   2028:                 if ($inst_results->{$item} ne '') {
1.252     raeburn  2029:                     $row .= '<input type="hidden" name="c'.$item.'" value="'.$inst_results->{$item}.'" />'.$inst_results->{$item};
1.210     raeburn  2030:                 } else {
1.252     raeburn  2031:                     if ($context eq 'selfcreate') {
                   2032:                         if ($canmodify{$item}) { 
                   2033:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
                   2034:                             $editable ++;
                   2035:                         } else {
                   2036:                             $hiderow = 1;
                   2037:                         }
1.253     raeburn  2038:                     } else {
                   2039:                         $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2040:                     }
1.210     raeburn  2041:                 }
1.188     raeburn  2042:             } else {
1.252     raeburn  2043:                 if ($context eq 'selfcreate') {
1.287     raeburn  2044:                     if (($item eq 'permanentemail') && ($newuser eq 'email')) {
                   2045:                         $row .= $ccuname;
1.252     raeburn  2046:                     } else {
1.287     raeburn  2047:                         if ($canmodify{$item}) {
                   2048:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
                   2049:                             $editable ++;
                   2050:                         } else {
                   2051:                             $hiderow = 1;
                   2052:                         }
1.252     raeburn  2053:                     }
1.253     raeburn  2054:                 } else {
                   2055:                     $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2056:                 }
1.188     raeburn  2057:             }
                   2058:         } else {
1.219     raeburn  2059:             if ($canmodify{$item}) {
1.252     raeburn  2060:                 $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="'.$userenv{$item}.'" />';
1.188     raeburn  2061:             } else {
1.252     raeburn  2062:                 $row .= $userenv{$item};
1.188     raeburn  2063:             }
1.206     raeburn  2064:             if ($item eq 'id') {
1.219     raeburn  2065:                 $showforceid = $canmodify{$item};
                   2066:             }
1.188     raeburn  2067:         }
1.252     raeburn  2068:         $row .= &Apache::lonhtmlcommon::row_closure(1);
                   2069:         if (!$hiderow) {
                   2070:             $output .= $row;
                   2071:             $rowcount ++;
                   2072:         }
1.188     raeburn  2073:     }
1.286     raeburn  2074:     if (($canmodify_status{'inststatus'}) || ($context ne 'selfcreate')) {
                   2075:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($ccdomain);
                   2076:         if (ref($types) eq 'ARRAY') {
                   2077:             if (@{$types} > 0) {
                   2078:                 my ($hiderow,$shown);
                   2079:                 if ($canmodify_status{'inststatus'}) {
                   2080:                     $shown = &pick_inst_statuses($userenv{'inststatus'},$usertypes,$types);
                   2081:                 } else {
                   2082:                     if ($userenv{'inststatus'} eq '') {
                   2083:                         $hiderow = 1;
1.334     raeburn  2084:                     } else {
                   2085:                         my @showitems;
                   2086:                         foreach my $item ( map { &unescape($_); } split(':',$userenv{'inststatus'})) {
                   2087:                             if (exists($usertypes->{$item})) {
                   2088:                                 push(@showitems,$usertypes->{$item});
                   2089:                             } else {
                   2090:                                 push(@showitems,$item);
                   2091:                             }
                   2092:                         }
                   2093:                         if (@showitems) {
                   2094:                             $shown = join(', ',@showitems);
                   2095:                         } else {
                   2096:                             $hiderow = 1;
                   2097:                         }
1.286     raeburn  2098:                     }
                   2099:                 }
                   2100:                 if (!$hiderow) {
                   2101:                     my $row = &Apache::lonhtmlcommon::row_title(&mt('Affliations'),undef,'LC_oddrow_value')."\n".
                   2102:                               $shown.&Apache::lonhtmlcommon::row_closure(1); 
                   2103:                     if ($context eq 'selfcreate') {
                   2104:                         $rowcount ++;
                   2105:                     }
                   2106:                     $output .= $row;
                   2107:                 }
                   2108:             }
                   2109:         }
                   2110:     }
1.188     raeburn  2111:     $output .= &Apache::lonhtmlcommon::end_pick_box();
1.206     raeburn  2112:     if (wantarray) {
1.252     raeburn  2113:         if ($context eq 'selfcreate') {
                   2114:             return($output,$rowcount,$editable);
                   2115:         } else {
                   2116:             return ($output,$showforceid);
                   2117:         }
1.206     raeburn  2118:     } else {
                   2119:         return $output;
                   2120:     }
1.188     raeburn  2121: }
                   2122: 
1.286     raeburn  2123: sub pick_inst_statuses {
                   2124:     my ($curr,$usertypes,$types) = @_;
                   2125:     my ($output,$rem,@currtypes);
                   2126:     if ($curr ne '') {
                   2127:         @currtypes = map { &unescape($_); } split(/:/,$curr);
                   2128:     }
                   2129:     my $numinrow = 2;
                   2130:     if (ref($types) eq 'ARRAY') {
                   2131:         $output = '<table>';
                   2132:         my $lastcolspan; 
                   2133:         for (my $i=0; $i<@{$types}; $i++) {
                   2134:             if (defined($usertypes->{$types->[$i]})) {
                   2135:                 my $rem = $i%($numinrow);
                   2136:                 if ($rem == 0) {
                   2137:                     if ($i<@{$types}-1) {
                   2138:                         if ($i > 0) { 
                   2139:                             $output .= '</tr>';
                   2140:                         }
                   2141:                         $output .= '<tr>';
                   2142:                     }
                   2143:                 } elsif ($i==@{$types}-1) {
                   2144:                     my $colsleft = $numinrow - $rem;
                   2145:                     if ($colsleft > 1) {
                   2146:                         $lastcolspan = ' colspan="'.$colsleft.'"';
                   2147:                     }
                   2148:                 }
                   2149:                 my $check = ' ';
                   2150:                 if (grep(/^\Q$types->[$i]\E$/,@currtypes)) {
                   2151:                     $check = ' checked="checked" ';
                   2152:                 }
                   2153:                 $output .= '<td class="LC_left_item"'.$lastcolspan.'>'.
                   2154:                            '<span class="LC_nobreak"><label>'.
                   2155:                            '<input type="checkbox" name="inststatus" '.
                   2156:                            'value="'.$types->[$i].'"'.$check.'/>'.
                   2157:                            $usertypes->{$types->[$i]}.'</label></span></td>';
                   2158:             }
                   2159:         }
                   2160:         $output .= '</tr></table>';
                   2161:     }
                   2162:     return $output;
                   2163: }
                   2164: 
1.257     raeburn  2165: sub selfcreate_canmodify {
                   2166:     my ($context,$dom,$userinfo,$inst_results,$rolesarray) = @_;
                   2167:     if (ref($inst_results) eq 'HASH') {
                   2168:         my @inststatuses = &get_inststatuses($inst_results);
                   2169:         if (@inststatuses == 0) {
                   2170:             @inststatuses = ('default');
                   2171:         }
                   2172:         $rolesarray = \@inststatuses;
                   2173:     }
                   2174:     my %canmodify =
                   2175:         &Apache::lonuserutils::can_modify_userinfo($context,$dom,$userinfo,
                   2176:                                                    $rolesarray);
                   2177:     return %canmodify;
                   2178: }
                   2179: 
1.252     raeburn  2180: sub get_inststatuses {
                   2181:     my ($insthashref) = @_;
                   2182:     my @inststatuses = ();
                   2183:     if (ref($insthashref) eq 'HASH') {
                   2184:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
                   2185:             @inststatuses = @{$insthashref->{'inststatus'}};
                   2186:         }
                   2187:     }
                   2188:     return @inststatuses;
                   2189: }
                   2190: 
1.4       www      2191: # ================================================================= Phase Three
1.42      matthew  2192: sub update_user_data {
1.351     raeburn  2193:     my ($r,$context,$crstype,$brcrum) = @_; 
1.101     albertel 2194:     my $uhome=&Apache::lonnet::homeserver($env{'form.ccuname'},
                   2195:                                           $env{'form.ccdomain'});
1.27      matthew  2196:     # Error messages
1.188     raeburn  2197:     my $error     = '<span class="LC_error">'.&mt('Error').': ';
1.193     raeburn  2198:     my $end       = '</span><br /><br />';
                   2199:     my $rtnlink   = '<a href="javascript:backPage(document.userupdate,'.
1.188     raeburn  2200:                     "'$env{'form.prevphase'}','modify')".'" />'.
1.219     raeburn  2201:                     &mt('Return to previous page').'</a>'.
                   2202:                     &Apache::loncommon::end_page();
                   2203:     my $now = time;
1.40      www      2204:     my $title;
1.101     albertel 2205:     if (exists($env{'form.makeuser'})) {
1.40      www      2206: 	$title='Set Privileges for New User';
                   2207:     } else {
                   2208:         $title='Modify User Privileges';
                   2209:     }
1.213     raeburn  2210:     my $newuser = 0;
1.160     raeburn  2211:     my ($jsback,$elements) = &crumb_utilities();
                   2212:     my $jscript = '<script type="text/javascript">'."\n".
1.301     bisitz   2213:                   '// <![CDATA['."\n".
                   2214:                   $jsback."\n".
                   2215:                   '// ]]>'."\n".
                   2216:                   '</script>'."\n";
1.318     raeburn  2217:     my %breadcrumb_text = &singleuser_breadcrumb($crstype);
1.351     raeburn  2218:     push (@{$brcrum},
                   2219:              {href => "javascript:backPage(document.userupdate)",
                   2220:               text => $breadcrumb_text{'search'},
                   2221:               faq  => 282,
                   2222:               bug  => 'Instructor Interface',}
                   2223:              );
                   2224:     if ($env{'form.prevphase'} eq 'userpicked') {
                   2225:         push(@{$brcrum},
                   2226:                {href => "javascript:backPage(document.userupdate,'get_user_info','select')",
                   2227:                 text => $breadcrumb_text{'userpicked'},
                   2228:                 faq  => 282,
                   2229:                 bug  => 'Instructor Interface',});
1.233     raeburn  2230:     }
1.224     raeburn  2231:     my $helpitem = 'Course_Change_Privileges';
                   2232:     if ($env{'form.action'} eq 'singlestudent') {
                   2233:         $helpitem = 'Course_Add_Student';
                   2234:     }
1.351     raeburn  2235:     push(@{$brcrum}, 
                   2236:             {href => "javascript:backPage(document.userupdate,'$env{'form.prevphase'}','modify')",
                   2237:              text => $breadcrumb_text{'modify'},
                   2238:              faq  => 282,
                   2239:              bug  => 'Instructor Interface',},
                   2240:             {href => "/adm/createuser",
                   2241:              text => "Result",
                   2242:              faq  => 282,
                   2243:              bug  => 'Instructor Interface',
                   2244:              help => $helpitem});
                   2245:     my $args = {bread_crumbs          => $brcrum,
                   2246:                 bread_crumbs_component => 'User Management'};
                   2247:     if ($env{'form.popup'}) {
                   2248:         $args->{'no_nav_bar'} = 1;
                   2249:     }
                   2250:     $r->print(&Apache::loncommon::start_page($title,$jscript,$args));
1.188     raeburn  2251:     $r->print(&update_result_form($uhome));
1.27      matthew  2252:     # Check Inputs
1.101     albertel 2253:     if (! $env{'form.ccuname'} ) {
1.193     raeburn  2254: 	$r->print($error.&mt('No login name specified').'.'.$end.$rtnlink);
1.27      matthew  2255: 	return;
                   2256:     }
1.138     albertel 2257:     if (  $env{'form.ccuname'} ne 
                   2258: 	  &LONCAPA::clean_username($env{'form.ccuname'}) ) {
1.281     bisitz   2259: 	$r->print($error.&mt('Invalid login name.').'  '.
                   2260: 		  &mt('Only letters, numbers, periods, dashes, @, and underscores are valid.').
1.193     raeburn  2261: 		  $end.$rtnlink);
1.27      matthew  2262: 	return;
                   2263:     }
1.101     albertel 2264:     if (! $env{'form.ccdomain'}       ) {
1.193     raeburn  2265: 	$r->print($error.&mt('No domain specified').'.'.$end.$rtnlink);
1.27      matthew  2266: 	return;
                   2267:     }
1.138     albertel 2268:     if (  $env{'form.ccdomain'} ne
                   2269: 	  &LONCAPA::clean_domain($env{'form.ccdomain'}) ) {
1.281     bisitz   2270: 	$r->print($error.&mt('Invalid domain name.').'  '.
                   2271: 		  &mt('Only letters, numbers, periods, dashes, and underscores are valid.').
1.193     raeburn  2272: 		  $end.$rtnlink);
1.27      matthew  2273: 	return;
                   2274:     }
1.219     raeburn  2275:     if ($uhome eq 'no_host') {
                   2276:         $newuser = 1;
                   2277:     }
1.101     albertel 2278:     if (! exists($env{'form.makeuser'})) {
1.29      matthew  2279:         # Modifying an existing user, so check the validity of the name
                   2280:         if ($uhome eq 'no_host') {
1.73      sakharuk 2281:             $r->print($error.&mt('Unable to determine home server for ').
1.101     albertel 2282:                       $env{'form.ccuname'}.&mt(' in domain ').
                   2283:                       $env{'form.ccdomain'}.'.');
1.29      matthew  2284:             return;
                   2285:         }
                   2286:     }
1.27      matthew  2287:     # Determine authentication method and password for the user being modified
                   2288:     my $amode='';
                   2289:     my $genpwd='';
1.101     albertel 2290:     if ($env{'form.login'} eq 'krb') {
1.41      albertel 2291: 	$amode='krb';
1.101     albertel 2292: 	$amode.=$env{'form.krbver'};
                   2293: 	$genpwd=$env{'form.krbarg'};
                   2294:     } elsif ($env{'form.login'} eq 'int') {
1.27      matthew  2295: 	$amode='internal';
1.101     albertel 2296: 	$genpwd=$env{'form.intarg'};
                   2297:     } elsif ($env{'form.login'} eq 'fsys') {
1.27      matthew  2298: 	$amode='unix';
1.101     albertel 2299: 	$genpwd=$env{'form.fsysarg'};
                   2300:     } elsif ($env{'form.login'} eq 'loc') {
1.27      matthew  2301: 	$amode='localauth';
1.101     albertel 2302: 	$genpwd=$env{'form.locarg'};
1.27      matthew  2303: 	$genpwd=" " if (!$genpwd);
1.101     albertel 2304:     } elsif (($env{'form.login'} eq 'nochange') ||
                   2305:              ($env{'form.login'} eq ''        )) { 
1.34      matthew  2306:         # There is no need to tell the user we did not change what they
                   2307:         # did not ask us to change.
1.35      matthew  2308:         # If they are creating a new user but have not specified login
                   2309:         # information this will be caught below.
1.30      matthew  2310:     } else {
1.193     raeburn  2311: 	    $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);    
1.30      matthew  2312: 	    return;
1.27      matthew  2313:     }
1.164     albertel 2314: 
1.188     raeburn  2315:     $r->print('<h3>'.&mt('User [_1] in domain [_2]',
                   2316: 			 $env{'form.ccuname'}, $env{'form.ccdomain'}).'</h3>');
1.344     bisitz   2317:     $r->print('<p class="LC_info">'.&mt('Please be patient').'</p>');
                   2318: 
1.193     raeburn  2319:     my (%alerts,%rulematch,%inst_results,%curr_rules);
1.334     raeburn  2320:     my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
1.267     raeburn  2321:     my @usertools = ('aboutme','blog','portfolio');
1.299     raeburn  2322:     my @requestcourses = ('official','unofficial','community');
1.286     raeburn  2323:     my ($othertitle,$usertypes,$types) = 
                   2324:         &Apache::loncommon::sorted_inst_types($env{'form.ccdomain'});
1.334     raeburn  2325:     my %canmodify_status =
                   2326:         &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},
                   2327:                                                    ['inststatus']);
1.101     albertel 2328:     if ($env{'form.makeuser'}) {
1.164     albertel 2329: 	$r->print('<h3>'.&mt('Creating new account.').'</h3>');
1.27      matthew  2330:         # Check for the authentication mode and password
                   2331:         if (! $amode || ! $genpwd) {
1.193     raeburn  2332: 	    $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);    
1.27      matthew  2333: 	    return;
1.18      albertel 2334: 	}
1.29      matthew  2335:         # Determine desired host
1.101     albertel 2336:         my $desiredhost = $env{'form.hserver'};
1.29      matthew  2337:         if (lc($desiredhost) eq 'default') {
                   2338:             $desiredhost = undef;
                   2339:         } else {
1.147     albertel 2340:             my %home_servers = 
                   2341: 		&Apache::lonnet::get_servers($env{'form.ccdomain'},'library');
1.29      matthew  2342:             if (! exists($home_servers{$desiredhost})) {
1.193     raeburn  2343:                 $r->print($error.&mt('Invalid home server specified').$end.$rtnlink);
                   2344:                 return;
                   2345:             }
                   2346:         }
                   2347:         # Check ID format
                   2348:         my %checkhash;
                   2349:         my %checks = ('id' => 1);
                   2350:         %{$checkhash{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}} = (
1.219     raeburn  2351:             'newuser' => $newuser, 
1.196     raeburn  2352:             'id' => $env{'form.cid'},
1.193     raeburn  2353:         );
1.196     raeburn  2354:         if ($env{'form.cid'} ne '') {
                   2355:             &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
                   2356:                                           \%rulematch,\%inst_results,\%curr_rules);
                   2357:             if (ref($alerts{'id'}) eq 'HASH') {
                   2358:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
                   2359:                     my $domdesc =
                   2360:                         &Apache::lonnet::domain($env{'form.ccdomain'},'description');
                   2361:                     if ($alerts{'id'}{$env{'form.ccdomain'}}{$env{'form.cid'}}) {
                   2362:                         my $userchkmsg;
                   2363:                         if (ref($curr_rules{$env{'form.ccdomain'}}) eq 'HASH') {
                   2364:                             $userchkmsg  = 
                   2365:                                 &Apache::loncommon::instrule_disallow_msg('id',
                   2366:                                                                     $domdesc,1).
                   2367:                                 &Apache::loncommon::user_rule_formats($env{'form.ccdomain'},
                   2368:                                     $domdesc,$curr_rules{$env{'form.ccdomain'}}{'id'},'id');
                   2369:                         }
                   2370:                         $r->print($error.&mt('Invalid ID format').$end.
                   2371:                                   $userchkmsg.$rtnlink);
                   2372:                         return;
                   2373:                     }
                   2374:                 }
1.29      matthew  2375:             }
                   2376:         }
1.27      matthew  2377: 	# Call modifyuser
                   2378: 	my $result = &Apache::lonnet::modifyuser
1.193     raeburn  2379: 	    ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cid'},
1.188     raeburn  2380:              $amode,$genpwd,$env{'form.cfirstname'},
                   2381:              $env{'form.cmiddlename'},$env{'form.clastname'},
                   2382:              $env{'form.cgeneration'},undef,$desiredhost,
                   2383:              $env{'form.cpermanentemail'});
1.77      www      2384: 	$r->print(&mt('Generating user').': '.$result);
1.219     raeburn  2385:         $uhome = &Apache::lonnet::homeserver($env{'form.ccuname'},
1.101     albertel 2386:                                                $env{'form.ccdomain'});
1.334     raeburn  2387:         my (%changeHash,%newcustom,%changed,%changedinfo);
1.267     raeburn  2388:         if ($uhome ne 'no_host') {
1.334     raeburn  2389:             if ($context eq 'domain') {
                   2390:                 if ($env{'form.customquota'} == 1) {
                   2391:                     if ($env{'form.portfolioquota'} eq '') {
                   2392:                         $newcustom{'quota'} = 0;
                   2393:                     } else {
                   2394:                         $newcustom{'quota'} = $env{'form.portfolioquota'};
                   2395:                         $newcustom{'quota'} =~ s/[^\d\.]//g;
                   2396:                     }
                   2397:                     $changed{'quota'} = &quota_admin($newcustom{'quota'},\%changeHash);
                   2398:                 }
                   2399:                 foreach my $item (@usertools) {
                   2400:                     if ($env{'form.custom'.$item} == 1) {
                   2401:                         $newcustom{$item} = $env{'form.tools_'.$item};
                   2402:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   2403:                                                      \%changeHash,'tools');
                   2404:                     }
1.267     raeburn  2405:                 }
1.334     raeburn  2406:                 foreach my $item (@requestcourses) {
1.341     raeburn  2407:                     if ($env{'form.custom'.$item} == 1) {
                   2408:                         $newcustom{$item} = $env{'form.crsreq_'.$item};
                   2409:                         if ($env{'form.crsreq_'.$item} eq 'autolimit') {
                   2410:                             $newcustom{$item} .= '=';
                   2411:                             unless ($env{'form.crsreq_'.$item.'_limit'} =~ /\D/) {
                   2412:                                 $newcustom{$item} .= $env{'form.crsreq_'.$item.'_limit'};
                   2413:                             }
1.334     raeburn  2414:                         }
1.341     raeburn  2415:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   2416:                                                       \%changeHash,'requestcourses');
1.334     raeburn  2417:                     }
1.275     raeburn  2418:                 }
                   2419:             }
1.334     raeburn  2420:             if ($canmodify_status{'inststatus'}) {
                   2421:                 if (exists($env{'form.inststatus'})) {
                   2422:                     my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   2423:                     if (@inststatuses > 0) {
                   2424:                         $changeHash{'inststatus'} = join(',',@inststatuses);
                   2425:                         $changed{'inststatus'} = $changeHash{'inststatus'};
1.306     raeburn  2426:                     }
                   2427:                 }
1.232     raeburn  2428:             }
1.334     raeburn  2429:             if (keys(%changed)) {
                   2430:                 foreach my $item (@userinfo) {
                   2431:                     $changeHash{$item}  = $env{'form.c'.$item};
1.286     raeburn  2432:                 }
1.267     raeburn  2433:                 my $chgresult =
                   2434:                      &Apache::lonnet::put('environment',\%changeHash,
                   2435:                                           $env{'form.ccdomain'},$env{'form.ccuname'});
                   2436:             } 
1.232     raeburn  2437:         }
1.219     raeburn  2438:         $r->print('<br />'.&mt('Home server').': '.$uhome.' '.
                   2439:                   &Apache::lonnet::hostname($uhome));
1.101     albertel 2440:     } elsif (($env{'form.login'} ne 'nochange') &&
                   2441:              ($env{'form.login'} ne ''        )) {
1.27      matthew  2442: 	# Modify user privileges
                   2443:         if (! $amode || ! $genpwd) {
1.193     raeburn  2444: 	    $r->print($error.'Invalid login mode or password'.$end.$rtnlink);    
1.27      matthew  2445: 	    return;
1.20      harris41 2446: 	}
1.27      matthew  2447: 	# Only allow authentification modification if the person has authority
1.101     albertel 2448: 	if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.20      harris41 2449: 	    $r->print('Modifying authentication: '.
1.31      matthew  2450:                       &Apache::lonnet::modifyuserauth(
1.101     albertel 2451: 		       $env{'form.ccdomain'},$env{'form.ccuname'},
1.21      harris41 2452:                        $amode,$genpwd));
1.102     albertel 2453:             $r->print('<br />'.&mt('Home server').': '.&Apache::lonnet::homeserver
1.101     albertel 2454: 		  ($env{'form.ccuname'},$env{'form.ccdomain'}));
1.4       www      2455: 	} else {
1.27      matthew  2456: 	    # Okay, this is a non-fatal error.
1.193     raeburn  2457: 	    $r->print($error.&mt('You do not have the authority to modify this users authentification information').'.'.$end);    
1.27      matthew  2458: 	}
1.28      matthew  2459:     }
1.344     bisitz   2460: 
                   2461:     $r->rflush(); # Finish display of header before time consuming actions start
                   2462: 
1.28      matthew  2463:     ##
1.343     raeburn  2464:     my (@userroles,%userupdate,$cnum,$cdom,%namechanged);
1.213     raeburn  2465:     if ($context eq 'course') {
                   2466:         ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity();
1.318     raeburn  2467:         $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
1.213     raeburn  2468:     }
1.101     albertel 2469:     if (! $env{'form.makeuser'} ) {
1.28      matthew  2470:         # Check for need to change
                   2471:         my %userenv = &Apache::lonnet::get
1.134     raeburn  2472:             ('environment',['firstname','middlename','lastname','generation',
1.267     raeburn  2473:              'id','permanentemail','portfolioquota','inststatus','tools.aboutme',
1.279     raeburn  2474:              'tools.blog','tools.portfolio','requestcourses.official',
1.300     raeburn  2475:              'requestcourses.unofficial','requestcourses.community',
                   2476:              'reqcrsotherdom.official','reqcrsotherdom.unofficial',
                   2477:              'reqcrsotherdom.community'],
1.160     raeburn  2478:               $env{'form.ccdomain'},$env{'form.ccuname'});
1.28      matthew  2479:         my ($tmp) = keys(%userenv);
                   2480:         if ($tmp =~ /^(con_lost|error)/i) { 
                   2481:             %userenv = ();
                   2482:         }
1.206     raeburn  2483:         my $no_forceid_alert;
                   2484:         # Check to see if user information can be changed
                   2485:         my %domconfig =
                   2486:             &Apache::lonnet::get_dom('configuration',['usermodification'],
                   2487:                                      $env{'form.ccdomain'});
1.213     raeburn  2488:         my @statuses = ('active','future');
                   2489:         my %roles = &Apache::lonnet::get_my_roles($env{'form.ccuname'},$env{'form.ccdomain'},'userroles',\@statuses,undef,$env{'request.role.domain'});
                   2490:         my ($auname,$audom);
1.220     raeburn  2491:         if ($context eq 'author') {
1.206     raeburn  2492:             $auname = $env{'user.name'};
                   2493:             $audom = $env{'user.domain'};     
                   2494:         }
                   2495:         foreach my $item (keys(%roles)) {
1.220     raeburn  2496:             my ($rolenum,$roledom,$role) = split(/:/,$item,-1);
1.206     raeburn  2497:             if ($context eq 'course') {
                   2498:                 if ($cnum ne '' && $cdom ne '') {
                   2499:                     if ($rolenum eq $cnum && $roledom eq $cdom) {
                   2500:                         if (!grep(/^\Q$role\E$/,@userroles)) {
                   2501:                             push(@userroles,$role);
                   2502:                         }
                   2503:                     }
                   2504:                 }
                   2505:             } elsif ($context eq 'author') {
                   2506:                 if ($rolenum eq $auname && $roledom eq $audom) {
                   2507:                     if (!grep(/^\Q$role\E$/,@userroles)) { 
                   2508:                         push(@userroles,$role);
                   2509:                     }
                   2510:                 }
                   2511:             }
                   2512:         }
1.220     raeburn  2513:         if ($env{'form.action'} eq 'singlestudent') {
                   2514:             if (!grep(/^st$/,@userroles)) {
                   2515:                 push(@userroles,'st');
                   2516:             }
                   2517:         } else {
                   2518:             # Check for course or co-author roles being activated or re-enabled
                   2519:             if ($context eq 'author' || $context eq 'course') {
                   2520:                 foreach my $key (keys(%env)) {
                   2521:                     if ($context eq 'author') {
                   2522:                         if ($key=~/^form\.act_\Q$audom\E_\Q$auname\E_([^_]+)/) {
                   2523:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2524:                                 push(@userroles,$1);
                   2525:                             }
                   2526:                         } elsif ($key =~/^form\.ren\:\Q$audom\E\/\Q$auname\E_([^_]+)/) {
                   2527:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2528:                                 push(@userroles,$1);
                   2529:                             }
1.206     raeburn  2530:                         }
1.220     raeburn  2531:                     } elsif ($context eq 'course') {
                   2532:                         if ($key=~/^form\.act_\Q$cdom\E_\Q$cnum\E_([^_]+)/) {
                   2533:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2534:                                 push(@userroles,$1);
                   2535:                             }
                   2536:                         } elsif ($key =~/^form\.ren\:\Q$cdom\E\/\Q$cnum\E(\/?\w*)_([^_]+)/) {
                   2537:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2538:                                 push(@userroles,$1);
                   2539:                             }
1.206     raeburn  2540:                         }
                   2541:                     }
                   2542:                 }
                   2543:             }
                   2544:         }
                   2545:         #Check to see if we can change personal data for the user 
                   2546:         my (@mod_disallowed,@longroles);
                   2547:         foreach my $role (@userroles) {
                   2548:             if ($role eq 'cr') {
                   2549:                 push(@longroles,'Custom');
                   2550:             } else {
1.318     raeburn  2551:                 push(@longroles,&Apache::lonnet::plaintext($role,$crstype)); 
1.206     raeburn  2552:             }
                   2553:         }
1.219     raeburn  2554:         my %canmodify = &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},\@userinfo,\@userroles);
                   2555:         foreach my $item (@userinfo) {
1.28      matthew  2556:             # Strip leading and trailing whitespace
1.203     raeburn  2557:             $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g;
1.219     raeburn  2558:             if (!$canmodify{$item}) {
1.207     raeburn  2559:                 if (defined($env{'form.c'.$item})) {
                   2560:                     if ($env{'form.c'.$item} ne $userenv{$item}) {
                   2561:                         push(@mod_disallowed,$item);
                   2562:                     }
1.206     raeburn  2563:                 }
                   2564:                 $env{'form.c'.$item} = $userenv{$item};
                   2565:             }
1.28      matthew  2566:         }
1.259     bisitz   2567:         # Check to see if we can change the Student/Employee ID
1.196     raeburn  2568:         my $forceid = $env{'form.forceid'};
                   2569:         my $recurseid = $env{'form.recurseid'};
                   2570:         my (%alerts,%rulematch,%idinst_results,%curr_rules,%got_rules);
1.203     raeburn  2571:         my %uidhash = &Apache::lonnet::idrget($env{'form.ccdomain'},
                   2572:                                             $env{'form.ccuname'});
                   2573:         if (($uidhash{$env{'form.ccuname'}}) && 
                   2574:             ($uidhash{$env{'form.ccuname'}}!~/error\:/) && 
                   2575:             (!$forceid)) {
                   2576:             if ($env{'form.cid'} ne $uidhash{$env{'form.ccuname'}}) {
                   2577:                 $env{'form.cid'} = $userenv{'id'};
1.293     bisitz   2578:                 $no_forceid_alert = &mt('New student/employee ID does not match existing ID for this user.')
1.259     bisitz   2579:                                    .'<br />'
                   2580:                                    .&mt("Change is not permitted without checking the 'Force ID change' checkbox on the previous page.")
                   2581:                                    .'<br />'."\n";
1.203     raeburn  2582:             }
                   2583:         }
                   2584:         if ($env{'form.cid'} ne $userenv{'id'}) {
1.196     raeburn  2585:             my $checkhash;
                   2586:             my $checks = { 'id' => 1 };
                   2587:             $checkhash->{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}} = 
                   2588:                    { 'newuser' => $newuser,
                   2589:                      'id'  => $env{'form.cid'}, 
                   2590:                    };
                   2591:             &Apache::loncommon::user_rule_check($checkhash,$checks,
                   2592:                 \%alerts,\%rulematch,\%idinst_results,\%curr_rules,\%got_rules);
                   2593:             if (ref($alerts{'id'}) eq 'HASH') {
                   2594:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
1.203     raeburn  2595:                    $env{'form.cid'} = $userenv{'id'};
1.196     raeburn  2596:                 }
                   2597:             }
                   2598:         }
1.286     raeburn  2599:         my ($quotachanged,$oldportfolioquota,$newportfolioquota,$oldinststatus,
1.334     raeburn  2600:             $newinststatus,$oldisdefault,$newisdefault,%oldsettings,
1.339     raeburn  2601:             %oldsettingstext,%newsettings,%newsettingstext,@disporder,
                   2602:             $olddefquota,$oldsettingstatus,$newdefquota,$newsettingstatus);
1.334     raeburn  2603:         @disporder = ('inststatus');
                   2604:         if ($env{'request.role.domain'} eq $env{'form.ccdomain'}) {
                   2605:             push(@disporder,'requestcourses');
                   2606:         } else {
                   2607:             push(@disporder,'reqcrsotherdom');
                   2608:         }
                   2609:         push(@disporder,('quota','tools'));
1.338     raeburn  2610:         $oldinststatus = $userenv{'inststatus'};
1.339     raeburn  2611:         ($olddefquota,$oldsettingstatus) = 
1.334     raeburn  2612:             &Apache::loncommon::default_quota($env{'form.ccdomain'},$oldinststatus);
1.339     raeburn  2613:         ($newdefquota,$newsettingstatus) = ($olddefquota,$oldsettingstatus);
1.334     raeburn  2614:         my %canshow;
1.220     raeburn  2615:         if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
1.334     raeburn  2616:             $canshow{'quota'} = 1;
1.220     raeburn  2617:         }
1.267     raeburn  2618:         if (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
1.334     raeburn  2619:             $canshow{'tools'} = 1;
1.267     raeburn  2620:         }
1.275     raeburn  2621:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
1.334     raeburn  2622:             $canshow{'requestcourses'} = 1;
1.300     raeburn  2623:         } elsif (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.334     raeburn  2624:             $canshow{'reqcrsotherdom'} = 1;
1.275     raeburn  2625:         }
1.286     raeburn  2626:         if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.334     raeburn  2627:             $canshow{'inststatus'} = 1;
1.286     raeburn  2628:         }
1.267     raeburn  2629:         my (%changeHash,%changed);
1.286     raeburn  2630:         if ($oldinststatus eq '') {
1.334     raeburn  2631:             $oldsettings{'inststatus'} = $othertitle; 
1.286     raeburn  2632:         } else {
                   2633:             if (ref($usertypes) eq 'HASH') {
1.334     raeburn  2634:                 $oldsettings{'inststatus'} = join(', ',map{ $usertypes->{ &unescape($_) }; } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  2635:             } else {
1.334     raeburn  2636:                 $oldsettings{'inststatus'} = join(', ',map{ &unescape($_); } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  2637:             }
                   2638:         }
                   2639:         $changeHash{'inststatus'} = $userenv{'inststatus'};
1.334     raeburn  2640:         if ($canmodify_status{'inststatus'}) {
                   2641:             $canshow{'inststatus'} = 1;
1.286     raeburn  2642:             if (exists($env{'form.inststatus'})) {
                   2643:                 my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   2644:                 if (@inststatuses > 0) {
                   2645:                     $newinststatus = join(':',map { &escape($_); } @inststatuses);
                   2646:                     $changeHash{'inststatus'} = $newinststatus;
                   2647:                     if ($newinststatus ne $oldinststatus) {
                   2648:                         $changed{'inststatus'} = $newinststatus;
1.339     raeburn  2649:                         ($newdefquota,$newsettingstatus) =
                   2650:                             &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus);
1.286     raeburn  2651:                     }
                   2652:                     if (ref($usertypes) eq 'HASH') {
1.334     raeburn  2653:                         $newsettings{'inststatus'} = join(', ',map{ $usertypes->{$_}; } (@inststatuses)); 
1.286     raeburn  2654:                     } else {
1.337     raeburn  2655:                         $newsettings{'inststatus'} = join(', ',@inststatuses);
1.286     raeburn  2656:                     }
1.334     raeburn  2657:                 }
                   2658:             } else {
                   2659:                 $newinststatus = '';
                   2660:                 $changeHash{'inststatus'} = $newinststatus;
                   2661:                 $newsettings{'inststatus'} = $othertitle;
                   2662:                 if ($newinststatus ne $oldinststatus) {
                   2663:                     $changed{'inststatus'} = $changeHash{'inststatus'};
1.339     raeburn  2664:                     ($newdefquota,$newsettingstatus) =
                   2665:                         &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus);
1.286     raeburn  2666:                 }
                   2667:             }
1.334     raeburn  2668:         } elsif ($context ne 'selfcreate') {
                   2669:             $canshow{'inststatus'} = 1;
1.337     raeburn  2670:             $newsettings{'inststatus'} = $oldsettings{'inststatus'};
1.286     raeburn  2671:         }
1.204     raeburn  2672:         $changeHash{'portfolioquota'} = $userenv{'portfolioquota'};
1.334     raeburn  2673:         if ($context eq 'domain') {
                   2674:             if ($userenv{'portfolioquota'} ne '') {
                   2675:                 $oldportfolioquota = $userenv{'portfolioquota'};
                   2676:                 if ($env{'form.customquota'} == 1) {
                   2677:                     if ($env{'form.portfolioquota'} eq '') {
                   2678:                         $newportfolioquota = 0;
                   2679:                     } else {
                   2680:                         $newportfolioquota = $env{'form.portfolioquota'};
                   2681:                         $newportfolioquota =~ s/[^\d\.]//g;
                   2682:                     }
                   2683:                     if ($newportfolioquota != $oldportfolioquota) {
                   2684:                         $changed{'quota'} = &quota_admin($newportfolioquota,\%changeHash);
                   2685:                     }
1.149     raeburn  2686:                 } else {
1.334     raeburn  2687:                     $changed{'quota'} = &quota_admin('',\%changeHash);
1.339     raeburn  2688:                     $newportfolioquota = $newdefquota;
1.334     raeburn  2689:                     $newisdefault = 1;
1.149     raeburn  2690:                 }
1.334     raeburn  2691:             } else {
                   2692:                 $oldisdefault = 1;
1.339     raeburn  2693:                 $oldportfolioquota = $olddefquota;
1.334     raeburn  2694:                 if ($env{'form.customquota'} == 1) {
                   2695:                     if ($env{'form.portfolioquota'} eq '') {
                   2696:                         $newportfolioquota = 0;
                   2697:                     } else {
                   2698:                         $newportfolioquota = $env{'form.portfolioquota'};
                   2699:                         $newportfolioquota =~ s/[^\d\.]//g;
                   2700:                     }
1.267     raeburn  2701:                     $changed{'quota'} = &quota_admin($newportfolioquota,\%changeHash);
1.334     raeburn  2702:                 } else {
1.339     raeburn  2703:                     $newportfolioquota = $newdefquota;
1.334     raeburn  2704:                     $newisdefault = 1;
1.134     raeburn  2705:                 }
                   2706:             }
1.334     raeburn  2707:             if ($oldisdefault) {
1.339     raeburn  2708:                 $oldsettingstext{'quota'} = &get_defaultquota_text($oldsettingstatus);
1.334     raeburn  2709:             }
                   2710:             if ($newisdefault) {
1.339     raeburn  2711:                 $newsettingstext{'quota'} = &get_defaultquota_text($newsettingstatus);
1.334     raeburn  2712:             }
                   2713:             &tool_changes('tools',\@usertools,\%oldsettings,\%oldsettingstext,\%userenv,
                   2714:                           \%changeHash,\%changed,\%newsettings,\%newsettingstext);
                   2715:             if ($env{'form.ccdomain'} eq $env{'request.role.domain'}) {
                   2716:                 &tool_changes('requestcourses',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   2717:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  2718:             } else {
1.334     raeburn  2719:                 &tool_changes('reqcrsotherdom',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   2720:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  2721:             }
                   2722:         }
1.334     raeburn  2723:         foreach my $item (@userinfo) {
                   2724:             if ($env{'form.c'.$item} ne $userenv{$item}) {
                   2725:                 $namechanged{$item} = 1;
                   2726:             }
1.204     raeburn  2727:         }
1.334     raeburn  2728:         $oldsettings{'quota'} = $oldportfolioquota.' Mb';
                   2729:         $newsettings{'quota'} = $newportfolioquota.' Mb';
                   2730:         if ((keys(%namechanged) > 0) || (keys(%changed) > 0)) {
1.267     raeburn  2731:             my ($chgresult,$namechgresult);
                   2732:             if (keys(%changed) > 0) {
                   2733:                 $chgresult = 
1.204     raeburn  2734:                     &Apache::lonnet::put('environment',\%changeHash,
                   2735:                                   $env{'form.ccdomain'},$env{'form.ccuname'});
1.267     raeburn  2736:                 if ($chgresult eq 'ok') {
                   2737:                     if (($env{'user.name'} eq $env{'form.ccuname'}) &&
                   2738:                         ($env{'user.domain'} eq $env{'form.ccdomain'})) {
1.270     raeburn  2739:                         my %newenvhash;
                   2740:                         foreach my $key (keys(%changed)) {
1.299     raeburn  2741:                             if (($key eq 'official') || ($key eq 'unofficial')
                   2742:                                 || ($key eq 'community')) {
1.279     raeburn  2743:                                 $newenvhash{'environment.requestcourses.'.$key} =
                   2744:                                     $changeHash{'requestcourses.'.$key};
                   2745:                                 if ($changeHash{'requestcourses.'.$key} ne '') {
1.332     raeburn  2746:                                     $newenvhash{'environment.canrequest.'.$key} = 1;
1.279     raeburn  2747:                                 } else {
                   2748:                                     $newenvhash{'environment.canrequest.'.$key} =
                   2749:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   2750:                                             $key,'reload','requestcourses');
                   2751:                                 }
1.275     raeburn  2752:                             } elsif ($key ne 'quota') {
1.270     raeburn  2753:                                 $newenvhash{'environment.tools.'.$key} = 
                   2754:                                     $changeHash{'tools.'.$key};
1.279     raeburn  2755:                                 if ($changeHash{'tools.'.$key} ne '') {
                   2756:                                     $newenvhash{'environment.availabletools.'.$key} =
                   2757:                                         $changeHash{'tools.'.$key};
                   2758:                                 } else {
                   2759:                                     $newenvhash{'environment.availabletools.'.$key} =
                   2760:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},                                            $key,'reload','tools');
                   2761:                                 }
1.270     raeburn  2762:                             }
                   2763:                         }
1.271     raeburn  2764:                         if (keys(%newenvhash)) {
                   2765:                             &Apache::lonnet::appenv(\%newenvhash);
                   2766:                         }
1.267     raeburn  2767:                     }
                   2768:                 }
1.204     raeburn  2769:             }
1.334     raeburn  2770:             if (keys(%namechanged) > 0) {
1.337     raeburn  2771:                 foreach my $field (@userinfo) {
                   2772:                     $changeHash{$field}  = $env{'form.c'.$field};
                   2773:                 }
                   2774: # Make the change
1.204     raeburn  2775:                 $namechgresult =
                   2776:                     &Apache::lonnet::modifyuser($env{'form.ccdomain'},
                   2777:                         $env{'form.ccuname'},$changeHash{'id'},undef,undef,
                   2778:                         $changeHash{'firstname'},$changeHash{'middlename'},
                   2779:                         $changeHash{'lastname'},$changeHash{'generation'},
1.337     raeburn  2780:                         $changeHash{'id'},undef,$changeHash{'permanentemail'},undef,\@userinfo);
1.220     raeburn  2781:                 %userupdate = (
                   2782:                                lastname   => $env{'form.clastname'},
                   2783:                                middlename => $env{'form.cmiddlename'},
                   2784:                                firstname  => $env{'form.cfirstname'},
                   2785:                                generation => $env{'form.cgeneration'},
                   2786:                                id         => $env{'form.cid'},
                   2787:                              );
1.204     raeburn  2788:             }
1.334     raeburn  2789:             if (((keys(%namechanged) > 0) && $namechgresult eq 'ok') || 
1.267     raeburn  2790:                 ((keys(%changed) > 0) && $chgresult eq 'ok')) {
1.28      matthew  2791:             # Tell the user we changed the name
1.334     raeburn  2792:                 &display_userinfo($r,1,\@disporder,\%canshow,\@requestcourses,
                   2793:                                   \@usertools,\%userenv,\%changed,\%namechanged,
                   2794:                                   \%oldsettings, \%oldsettingstext,\%newsettings,
                   2795:                                   \%newsettingstext);
1.203     raeburn  2796:                 if ($env{'form.cid'} ne $userenv{'id'}) {
                   2797:                     &Apache::lonnet::idput($env{'form.ccdomain'},
                   2798:                          ($env{'form.ccuname'} => $env{'form.cid'}));
                   2799:                     if (($recurseid) &&
                   2800:                         (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}))) {
                   2801:                         my $idresult = 
                   2802:                             &Apache::lonuserutils::propagate_id_change(
                   2803:                                 $env{'form.ccuname'},$env{'form.ccdomain'},
                   2804:                                 \%userupdate);
                   2805:                         $r->print('<br />'.$idresult.'<br />');
                   2806:                     }
1.196     raeburn  2807:                 }
1.149     raeburn  2808:                 if (($env{'form.ccdomain'} eq $env{'user.domain'}) && 
                   2809:                     ($env{'form.ccuname'} eq $env{'user.name'})) {
                   2810:                     my %newenvhash;
                   2811:                     foreach my $key (keys(%changeHash)) {
                   2812:                         $newenvhash{'environment.'.$key} = $changeHash{$key};
                   2813:                     }
1.238     raeburn  2814:                     &Apache::lonnet::appenv(\%newenvhash);
1.149     raeburn  2815:                 }
1.28      matthew  2816:             } else { # error occurred
1.188     raeburn  2817:                 $r->print('<span class="LC_error">'.&mt('Unable to successfully change environment for').' '.
                   2818:                       $env{'form.ccuname'}.' '.&mt('in domain').' '.
1.206     raeburn  2819:                       $env{'form.ccdomain'}.'</span><br />');
1.28      matthew  2820:             }
1.334     raeburn  2821:         } else { # End of if ($env ... ) logic
1.275     raeburn  2822:             # They did not want to change the users name, quota, tool availability,
                   2823:             # or ability to request creation of courses, 
1.267     raeburn  2824:             # but we can still tell them what the name and quota and availabilities are  
1.334     raeburn  2825:             &display_userinfo($r,undef,\@disporder,\%canshow,\@requestcourses,
                   2826:                               \@usertools,\%userenv,\%changed,\%namechanged,\%oldsettings,
                   2827:                               \%oldsettingstext,\%newsettings,\%newsettingstext);
1.28      matthew  2828:         }
1.206     raeburn  2829:         if (@mod_disallowed) {
                   2830:             my ($rolestr,$contextname);
                   2831:             if (@longroles > 0) {
                   2832:                 $rolestr = join(', ',@longroles);
                   2833:             } else {
                   2834:                 $rolestr = &mt('No roles');
                   2835:             }
                   2836:             if ($context eq 'course') {
                   2837:                 $contextname = &mt('course');
                   2838:             } elsif ($context eq 'author') {
                   2839:                 $contextname = &mt('co-author');
                   2840:             }
                   2841:             $r->print(&mt('The following fields were not updated: ').'<ul>');
                   2842:             my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
                   2843:             foreach my $field (@mod_disallowed) {
                   2844:                 $r->print('<li>'.$fieldtitles{$field}.'</li>'."\n"); 
                   2845:             }
1.207     raeburn  2846:             $r->print('</ul>');
                   2847:             if (@mod_disallowed == 1) {
                   2848:                 $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));
                   2849:             } else {
                   2850:                 $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));
                   2851:             }
1.292     bisitz   2852:             my $helplink = 'javascript:helpMenu('."'display'".')';
                   2853:             $r->print('<span class="LC_cusr_emph">'.$rolestr.'</span><br />'
                   2854:                      .&mt('Please contact your [_1]helpdesk[_2] for more information.'
                   2855:                          ,'<a href="'.$helplink.'">','</a>')
                   2856:                       .'<br />');
1.206     raeburn  2857:         }
1.259     bisitz   2858:         $r->print('<span class="LC_warning">'
                   2859:                   .$no_forceid_alert
                   2860:                   .&Apache::lonuserutils::print_namespacing_alerts($env{'form.ccdomain'},\%alerts,\%curr_rules)
                   2861:                   .'</span>');
1.4       www      2862:     }
1.220     raeburn  2863:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  2864:         &enroll_single_student($r,$uhome,$amode,$genpwd,$now,$newuser,$context,$crstype);
                   2865:         $r->print('<p><a href="javascript:backPage(document.userupdate)">');
                   2866:         if ($crstype eq 'Community') {
                   2867:             $r->print(&mt('Enroll Another Member'));
                   2868:         } else {
                   2869:             $r->print(&mt('Enroll Another Student'));
                   2870:         }
                   2871:         $r->print('</a></p>');
1.220     raeburn  2872:     } else {
1.239     raeburn  2873:         my @rolechanges = &update_roles($r,$context);
1.334     raeburn  2874:         if (keys(%namechanged) > 0) {
1.220     raeburn  2875:             if ($context eq 'course') {
                   2876:                 if (@userroles > 0) {
1.225     raeburn  2877:                     if ((@rolechanges == 0) || 
                   2878:                         (!(grep(/^st$/,@rolechanges)))) {
                   2879:                         if (grep(/^st$/,@userroles)) {
                   2880:                             my $classlistupdated =
                   2881:                                 &Apache::lonuserutils::update_classlist($cdom,
1.220     raeburn  2882:                                               $cnum,$env{'form.ccdomain'},
                   2883:                                        $env{'form.ccuname'},\%userupdate);
1.225     raeburn  2884:                         }
1.220     raeburn  2885:                     }
                   2886:                 }
                   2887:             }
                   2888:         }
1.226     raeburn  2889:         my $userinfo = &Apache::loncommon::plainname($env{'form.ccuname'},
1.233     raeburn  2890:                                                      $env{'form.ccdomain'});
                   2891:         if ($env{'form.popup'}) {
                   2892:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
                   2893:         } else {
1.246     bisitz   2894:             $r->print('<p><a href="javascript:backPage(document.userupdate,'."'$env{'form.prevphase'}','modify'".')">'
                   2895:                      .&mt('Modify this user: [_1]','<span class="LC_cusr_emph">'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.' ('.$userinfo.')</span>').'</a>'
                   2896:                      .('&nbsp;'x5).'<a href="javascript:backPage(document.userupdate)">'
                   2897:                      .&mt('Create/Modify Another User').'</a></p>');
1.233     raeburn  2898:         }
1.220     raeburn  2899:     }
                   2900: }
                   2901: 
1.334     raeburn  2902: sub display_userinfo {
                   2903:     my ($r,$changed,$order,$canshow,$requestcourses,$usertools,$userenv,
                   2904:         $changedhash,$namechangedhash,$oldsetting,$oldsettingtext,
                   2905:         $newsetting,$newsettingtext) = @_;
                   2906:     return unless (ref($order) eq 'ARRAY' &&
                   2907:                    ref($canshow) eq 'HASH' && 
                   2908:                    ref($requestcourses) eq 'ARRAY' && 
                   2909:                    ref($usertools) eq 'ARRAY' && 
                   2910:                    ref($userenv) eq 'HASH' &&
                   2911:                    ref($changedhash) eq 'HASH' &&
                   2912:                    ref($oldsetting) eq 'HASH' &&
                   2913:                    ref($oldsettingtext) eq 'HASH' &&
                   2914:                    ref($newsetting) eq 'HASH' &&
                   2915:                    ref($newsettingtext) eq 'HASH');
                   2916:     my %lt=&Apache::lonlocal::texthash(
                   2917:          'ui'             => 'User Information (unchanged)',
                   2918:          'uic'            => 'User Information Changed',
                   2919:          'firstname'      => 'First Name',
                   2920:          'middlename'     => 'Middle Name',
                   2921:          'lastname'       => 'Last Name',
                   2922:          'generation'     => 'Generation',
                   2923:          'id'             => 'Student/Employee ID',
                   2924:          'permanentemail' => 'Permanent e-mail address',
                   2925:          'quota'          => 'Disk space allocated to portfolio files',
                   2926:          'blog'           => 'Blog Availability',
                   2927:          'aboutme'        => 'Personal Information Page Availability',
                   2928:          'portfolio'      => 'Portfolio Availability',
                   2929:          'official'       => 'Can Request Official Courses',
                   2930:          'unofficial'     => 'Can Request Unofficial Courses',
                   2931:          'community'      => 'Can Request Communities',
                   2932:          'inststatus'     => "Affiliation",
                   2933:          'prvs'           => 'Previous Value:',
                   2934:          'chto'           => 'Changed To:'
                   2935:     );
                   2936:     my $title = $lt{'ui'}; 
                   2937:     if ($changed) {
                   2938:         $title = $lt{'uic'};
                   2939:     }
                   2940:     $r->print('<h4>'.$title.'</h4>'.
                   2941:               &Apache::loncommon::start_data_table().
                   2942:               &Apache::loncommon::start_data_table_header_row());
                   2943:     if ($changed) {
                   2944:         $r->print("<th>&nbsp;</th>\n");
                   2945:     }
                   2946:     my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
                   2947:     foreach my $item (@userinfo) {
                   2948:         $r->print("<th>$lt{$item}</th>\n");
                   2949:     }
                   2950:     foreach my $entry (@{$order}) {
                   2951:         if ($canshow->{$entry}) {
                   2952:             if (($entry eq 'requestcourses') || ($entry eq 'reqcrsotherdom')) {
                   2953:                 foreach my $item (@{$requestcourses}) {
                   2954:                     $r->print("<th>$lt{$item}</th>\n");
                   2955:                 }
                   2956:             } elsif ($entry eq 'tools') {
                   2957:                 foreach my $item (@{$usertools}) {
                   2958:                     $r->print("<th>$lt{$item}</th>\n");
                   2959:                 }
                   2960:             } else {
                   2961:                 $r->print("<th>$lt{$entry}</th>\n");
                   2962:             }
                   2963:         }
                   2964:     }
                   2965:     $r->print(&Apache::loncommon::end_data_table_header_row().
                   2966:              &Apache::loncommon::start_data_table_row());
                   2967:     if ($changed) {
                   2968:         $r->print('<td><b>'.$lt{'prvs'}.'</b></td>'."\n");
                   2969:     }
                   2970:     foreach my $item (@userinfo) {
                   2971:         $r->print('<td>'.$userenv->{$item}.' </td>'."\n");
                   2972:     }
                   2973:     foreach my $entry (@{$order}) {
                   2974:         if ($canshow->{$entry}) {
                   2975:             if (($entry eq 'requestcourses') || ($entry eq 'reqcrsotherdom')) {
                   2976:                 foreach my $item (@{$requestcourses}) {
                   2977:                     $r->print("<td>$oldsetting->{$item} $oldsettingtext->{$item}</td>\n");
                   2978:                 }
                   2979:             } elsif ($entry eq 'tools') {
                   2980:                 foreach my $item (@{$usertools}) {
                   2981:                     $r->print("<td>$oldsetting->{$item} $oldsettingtext->{$item}</td>\n");
                   2982:                 }
                   2983:             } else {
                   2984:                 $r->print("<td>$oldsetting->{$entry} $oldsettingtext->{$entry} </td>\n");
                   2985:             }
                   2986:         }
                   2987:     }
                   2988:     $r->print(&Apache::loncommon::end_data_table_row());
                   2989:     if ($changed) {
                   2990:         $r->print(&Apache::loncommon::start_data_table_row().
                   2991:                   '<td><span class="LC_nobreak"><b>'.$lt{'chto'}.'</b></span></td>'."\n");
                   2992:         foreach my $item (@userinfo) {
                   2993:             my $value = $env{'form.c'.$item};
                   2994:             if ($namechangedhash->{$item}) {
                   2995:                 $value = '<span class="LC_cusr_emph">'.$value.'</span>';
                   2996:             }
                   2997:             $r->print("<td>$value </td>\n");
                   2998:         }
                   2999:         foreach my $entry (@{$order}) {
                   3000:             if ($canshow->{$entry}) {
                   3001:                 if (($entry eq 'requestcourses') || ($entry eq 'reqcrsotherdom')) {
                   3002:                     foreach my $item (@{$requestcourses}) {
                   3003:                         my $value = $newsetting->{$item}.' '.$newsettingtext->{$item};
                   3004:                         if ($changedhash->{$item}) {
                   3005:                             $value = '<span class="LC_cusr_emph">'.$value.'</span>';
                   3006:                         }
                   3007:                         $r->print("<td>$value </td>\n");
                   3008:                     }
                   3009:                 } elsif ($entry eq 'tools') {
                   3010:                     foreach my $item (@{$usertools}) {
                   3011:                         my $value = $newsetting->{$item}.' '.$newsettingtext->{$item};
                   3012:                         if ($changedhash->{$item}) {
                   3013:                             $value = '<span class="LC_cusr_emph">'.$value.'</span>';
                   3014:                         }
                   3015:                         $r->print("<td>$value </td>\n");
                   3016:                     }
                   3017:                 } else {
                   3018:                     my $value = $newsetting->{$entry}.' '.$newsettingtext->{$entry};
                   3019:                     if ($changedhash->{$entry}) {
                   3020:                         $value = '<span class="LC_cusr_emph">'.$value.'</span>';
                   3021:                     }
                   3022:                     $r->print("<td>$value </td>\n");
                   3023:                 }
                   3024:             }
                   3025:         }
                   3026:         $r->print(&Apache::loncommon::end_data_table_row());
                   3027:     }
                   3028:     $r->print(&Apache::loncommon::end_data_table().'<br />');
                   3029:     return;
                   3030: }
                   3031: 
1.275     raeburn  3032: sub tool_changes {
                   3033:     my ($context,$usertools,$oldaccess,$oldaccesstext,$userenv,$changeHash,
                   3034:         $changed,$newaccess,$newaccesstext) = @_;
                   3035:     if (!((ref($usertools) eq 'ARRAY') && (ref($oldaccess) eq 'HASH') &&
                   3036:           (ref($oldaccesstext) eq 'HASH') && (ref($userenv) eq 'HASH') &&
                   3037:           (ref($changeHash) eq 'HASH') && (ref($changed) eq 'HASH') &&
                   3038:           (ref($newaccess) eq 'HASH') && (ref($newaccesstext) eq 'HASH'))) {
                   3039:         return;
                   3040:     }
1.300     raeburn  3041:     if ($context eq 'reqcrsotherdom') {
1.309     raeburn  3042:         my @options = ('approval','validate','autolimit');
1.306     raeburn  3043:         my $optregex = join('|',@options);
                   3044:         my %reqdisplay = &courserequest_display();
1.300     raeburn  3045:         my $cdom = $env{'request.role.domain'};
                   3046:         foreach my $tool (@{$usertools}) {
1.314     raeburn  3047:             $oldaccesstext->{$tool} = &mt('No');
                   3048:             $newaccesstext->{$tool} = $oldaccesstext->{$tool};
1.300     raeburn  3049:             $changeHash->{$context.'.'.$tool} = $userenv->{$context.'.'.$tool};
1.314     raeburn  3050:             my $newop;
                   3051:             if ($env{'form.'.$context.'_'.$tool}) {
                   3052:                 $newop = $env{'form.'.$context.'_'.$tool};
                   3053:                 if ($newop eq 'autolimit') {
                   3054:                     my $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
                   3055:                     $limit =~ s/\D+//g;
                   3056:                     $newop .= '='.$limit;
                   3057:                 }
                   3058:             }
1.300     raeburn  3059:             if ($userenv->{$context.'.'.$tool} eq '') {
1.314     raeburn  3060:                 if ($newop) {
                   3061:                     $changed->{$tool}=&tool_admin($tool,$cdom.':'.$newop,
1.300     raeburn  3062:                                                   $changeHash,$context);
                   3063:                     if ($changed->{$tool}) {
1.314     raeburn  3064:                         $newaccesstext->{$tool} = &mt('Yes');
1.300     raeburn  3065:                     } else {
                   3066:                         $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3067:                     }
                   3068:                 }
                   3069:             } else {
                   3070:                 my @curr = split(',',$userenv->{$context.'.'.$tool});
                   3071:                 my @new;
                   3072:                 my $changedoms;
1.314     raeburn  3073:                 foreach my $req (@curr) {
                   3074:                     if ($req =~ /^\Q$cdom\E\:($optregex\=?\d*)$/) {
                   3075:                         $oldaccesstext->{$tool} = &mt('Yes');
                   3076:                         my $oldop = $1;
                   3077:                         if ($oldop ne $newop) {
                   3078:                             $changedoms = 1;
                   3079:                             foreach my $item (@curr) {
                   3080:                                 my ($reqdom,$option) = split(':',$item);
                   3081:                                 unless ($reqdom eq $cdom) {
                   3082:                                     push(@new,$item);
                   3083:                                 }
                   3084:                             }
                   3085:                             if ($newop) {
                   3086:                                 push(@new,$cdom.':'.$newop);
1.300     raeburn  3087:                             }
1.314     raeburn  3088:                             @new = sort(@new);
1.300     raeburn  3089:                         }
1.314     raeburn  3090:                         last;
1.300     raeburn  3091:                     }
1.314     raeburn  3092:                 }
                   3093:                 if ((!$changedoms) && ($newop)) {
1.300     raeburn  3094:                     $changedoms = 1;
1.306     raeburn  3095:                     @new = sort(@curr,$cdom.':'.$newop);
1.300     raeburn  3096:                 }
                   3097:                 if ($changedoms) {
1.314     raeburn  3098:                     my $newdomstr;
1.300     raeburn  3099:                     if (@new) {
                   3100:                         $newdomstr = join(',',@new);
                   3101:                     }
                   3102:                     $changed->{$tool}=&tool_admin($tool,$newdomstr,$changeHash,
                   3103:                                                   $context);
                   3104:                     if ($changed->{$tool}) {
                   3105:                         if ($env{'form.'.$context.'_'.$tool}) {
1.306     raeburn  3106:                             if ($env{'form.'.$context.'_'.$tool} eq 'autolimit') {
1.314     raeburn  3107:                                 my $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
                   3108:                                 $limit =~ s/\D+//g;
                   3109:                                 if ($limit) {
                   3110:                                     $newaccesstext->{$tool} = &mt('Yes, up to limit of [quant,_1,request] per user.',$limit);
                   3111:                                 } else {
1.306     raeburn  3112:                                     $newaccesstext->{$tool} = &mt('Yes, processed automatically');
                   3113:                                 }
1.314     raeburn  3114:                             } else {
1.306     raeburn  3115:                                 $newaccesstext->{$tool} = $reqdisplay{$env{'form.'.$context.'_'.$tool}};
                   3116:                             }
1.300     raeburn  3117:                         } else {
1.306     raeburn  3118:                             $newaccesstext->{$tool} = &mt('No');
1.300     raeburn  3119:                         }
                   3120:                     }
                   3121:                 }
                   3122:             }
                   3123:         }
                   3124:         return;
                   3125:     }
1.275     raeburn  3126:     foreach my $tool (@{$usertools}) {
1.306     raeburn  3127:         my $newval;
                   3128:         if ($context eq 'requestcourses') {
                   3129:             $newval = $env{'form.crsreq_'.$tool};
                   3130:             if ($newval eq 'autolimit') {
                   3131:                 $newval .= '='.$env{'form.crsreq_'.$tool.'_limit'};
                   3132:             }
1.314     raeburn  3133:         } else {
1.306     raeburn  3134:             $newval = $env{'form.'.$context.'_'.$tool};
                   3135:         }
1.275     raeburn  3136:         if ($userenv->{$context.'.'.$tool} ne '') {
                   3137:             $oldaccess->{$tool} = &mt('custom');
                   3138:             if ($userenv->{$context.'.'.$tool}) {
                   3139:                 $oldaccesstext->{$tool} = &mt("availability set to 'on'");
                   3140:             } else {
                   3141:                 $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   3142:             }
1.279     raeburn  3143:             $changeHash->{$context.'.'.$tool} = $userenv->{$context.'.'.$tool};
1.275     raeburn  3144:             if ($env{'form.custom'.$tool} == 1) {
1.306     raeburn  3145:                 if ($newval ne $userenv->{$context.'.'.$tool}) {
                   3146:                     $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3147:                                                     $context);
1.275     raeburn  3148:                     if ($changed->{$tool}) {
                   3149:                         $newaccess->{$tool} = &mt('custom');
1.306     raeburn  3150:                         if ($newval) {
1.275     raeburn  3151:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3152:                         } else {
                   3153:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3154:                         }
                   3155:                     } else {
                   3156:                         $newaccess->{$tool} = $oldaccess->{$tool};
                   3157:                         if ($userenv->{$context.'.'.$tool}) {
                   3158:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3159:                         } else {
                   3160:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3161:                         }
                   3162:                     }
                   3163:                 } else {
                   3164:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3165:                     $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3166:                 }
                   3167:             } else {
                   3168:                 $changed->{$tool} = &tool_admin($tool,'',$changeHash,$context);
                   3169:                 if ($changed->{$tool}) {
                   3170:                     $newaccess->{$tool} = &mt('default');
                   3171:                 } else {
                   3172:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3173:                     if ($userenv->{$context.'.'.$tool}) {
1.300     raeburn  3174:                         $newaccesstext->{$tool} = &mt("availability set to 'on'");
1.275     raeburn  3175:                     } else {
1.300     raeburn  3176:                         $newaccesstext->{$tool} = &mt("availability set to 'off'");
1.275     raeburn  3177:                     }
                   3178:                 }
                   3179:             }
                   3180:         } else {
                   3181:             $oldaccess->{$tool} = &mt('default');
                   3182:             if ($env{'form.custom'.$tool} == 1) {
1.306     raeburn  3183:                 $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3184:                                                 $context);
1.275     raeburn  3185:                 if ($changed->{$tool}) {
                   3186:                     $newaccess->{$tool} = &mt('custom');
1.306     raeburn  3187:                     if ($newval) {
1.275     raeburn  3188:                         $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3189:                     } else {
                   3190:                         $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3191:                     }
                   3192:                 } else {
                   3193:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3194:                 }
                   3195:             } else {
                   3196:                 $newaccess->{$tool} = $oldaccess->{$tool};
                   3197:             }
                   3198:         }
                   3199:     }
                   3200:     return;
                   3201: }
                   3202: 
1.220     raeburn  3203: sub update_roles {
1.239     raeburn  3204:     my ($r,$context) = @_;
1.4       www      3205:     my $now=time;
1.225     raeburn  3206:     my @rolechanges;
1.220     raeburn  3207:     my %disallowed;
1.73      sakharuk 3208:     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
1.135     raeburn  3209:     foreach my $key (keys (%env)) {
                   3210: 	next if (! $env{$key});
1.190     raeburn  3211:         next if ($key eq 'form.action');
1.27      matthew  3212: 	# Revoke roles
1.135     raeburn  3213: 	if ($key=~/^form\.rev/) {
                   3214: 	    if ($key=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
1.64      www      3215: # Revoke standard role
1.170     albertel 3216: 		my ($scope,$role) = ($1,$2);
                   3217: 		my $result =
                   3218: 		    &Apache::lonnet::revokerole($env{'form.ccdomain'},
                   3219: 						$env{'form.ccuname'},
1.239     raeburn  3220: 						$scope,$role,'','',$context);
1.170     albertel 3221: 	        $r->print(&mt('Revoking [_1] in [_2]: [_3]',
                   3222: 			      $role,$scope,'<b>'.$result.'</b>').'<br />');
                   3223: 		if ($role eq 'st') {
1.202     raeburn  3224: 		    my $result = 
1.198     raeburn  3225:                         &Apache::lonuserutils::classlist_drop($scope,
                   3226:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  3227: 			    $now);
1.170     albertel 3228: 		    $r->print($result);
1.53      www      3229: 		}
1.225     raeburn  3230:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3231:                     push(@rolechanges,$role);
                   3232:                 }
1.196     raeburn  3233: 	    }
1.195     raeburn  3234: 	    if ($key=~m{^form\.rev\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}s) {
1.64      www      3235: # Revoke custom role
1.113     raeburn  3236: 		$r->print(&mt('Revoking custom role:').
1.139     albertel 3237:                       ' '.$4.' by '.$3.':'.$2.' in '.$1.': <b>'.
1.101     albertel 3238:                       &Apache::lonnet::revokecustomrole($env{'form.ccdomain'},
1.239     raeburn  3239: 				  $env{'form.ccuname'},$1,$2,$3,$4,'','',$context).
1.102     albertel 3240: 		'</b><br />');
1.225     raeburn  3241:                 if (!grep(/^cr$/,@rolechanges)) {
                   3242:                     push(@rolechanges,'cr');
                   3243:                 }
1.64      www      3244: 	    }
1.135     raeburn  3245: 	} elsif ($key=~/^form\.del/) {
                   3246: 	    if ($key=~/^form\.del\:([^\_]+)\_([^\_\.]+)$/) {
1.116     raeburn  3247: # Delete standard role
1.170     albertel 3248: 		my ($scope,$role) = ($1,$2);
                   3249: 		my $result =
                   3250: 		    &Apache::lonnet::assignrole($env{'form.ccdomain'},
                   3251: 						$env{'form.ccuname'},
1.239     raeburn  3252: 						$scope,$role,$now,0,1,'',
                   3253:                                                 $context);
1.170     albertel 3254: 	        $r->print(&mt('Deleting [_1] in [_2]: [_3]',$role,$scope,
                   3255: 			      '<b>'.$result.'</b>').'<br />');
                   3256: 		if ($role eq 'st') {
1.202     raeburn  3257: 		    my $result = 
1.198     raeburn  3258:                         &Apache::lonuserutils::classlist_drop($scope,
                   3259:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  3260: 			    $now);
1.170     albertel 3261: 		    $r->print($result);
1.81      albertel 3262: 		}
1.225     raeburn  3263:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3264:                     push(@rolechanges,$role);
                   3265:                 }
1.116     raeburn  3266:             }
1.139     albertel 3267: 	    if ($key=~m{^form\.del\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  3268:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   3269: # Delete custom role
1.294     bisitz   3270:                 $r->print(&mt('Deleting custom role [_1] by [_2] in [_3]',
                   3271:                       $rolename,$rnam.':'.$rdom,$url).': <b>'.
1.116     raeburn  3272:                       &Apache::lonnet::assigncustomrole($env{'form.ccdomain'},
                   3273:                          $env{'form.ccuname'},$url,$rdom,$rnam,$rolename,$now,
1.240     raeburn  3274:                          0,1,$context).'</b><br />');
1.225     raeburn  3275:                 if (!grep(/^cr$/,@rolechanges)) {
                   3276:                     push(@rolechanges,'cr');
                   3277:                 }
1.116     raeburn  3278:             }
1.135     raeburn  3279: 	} elsif ($key=~/^form\.ren/) {
1.101     albertel 3280:             my $udom = $env{'form.ccdomain'};
                   3281:             my $uname = $env{'form.ccuname'};
1.116     raeburn  3282: # Re-enable standard role
1.135     raeburn  3283: 	    if ($key=~/^form\.ren\:([^\_]+)\_([^\_\.]+)$/) {
1.89      raeburn  3284:                 my $url = $1;
                   3285:                 my $role = $2;
                   3286:                 my $logmsg;
                   3287:                 my $output;
                   3288:                 if ($role eq 'st') {
1.141     albertel 3289:                     if ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
1.129     albertel 3290:                         my $result = &Apache::loncommon::commit_studentrole(\$logmsg,$udom,$uname,$url,$role,$now,0,$1,$2,$3);
1.220     raeburn  3291:                         if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course') || ($result eq 'refused')) {
1.223     raeburn  3292:                             if ($result eq 'refused' && $logmsg) {
                   3293:                                 $output = $logmsg;
                   3294:                             } else { 
                   3295:                                 $output = "Error: $result\n";
                   3296:                             }
1.89      raeburn  3297:                         } else {
                   3298:                             $output = &mt('Assigning').' '.$role.' in '.$url.
                   3299:                                       &mt('starting').' '.localtime($now).
                   3300:                                       ': <br />'.$logmsg.'<br />'.
                   3301:                                       &mt('Add to classlist').': <b>ok</b><br />';
                   3302:                         }
                   3303:                     }
                   3304:                 } else {
1.101     albertel 3305: 		    my $result=&Apache::lonnet::assignrole($env{'form.ccdomain'},
1.239     raeburn  3306:                                $env{'form.ccuname'},$url,$role,0,$now,'','',
                   3307:                                $context);
1.266     bisitz   3308: 		    $output = &mt('Re-enabling [_1] in [_2]: [_3]',
                   3309: 			      $role,$url,'<b>'.$result.'</b>').'<br />';
1.27      matthew  3310: 		}
1.89      raeburn  3311:                 $r->print($output);
1.225     raeburn  3312:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3313:                     push(@rolechanges,$role);
                   3314:                 }
1.113     raeburn  3315: 	    }
1.116     raeburn  3316: # Re-enable custom role
1.139     albertel 3317: 	    if ($key=~m{^form\.ren\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  3318:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   3319:                 my $result = &Apache::lonnet::assigncustomrole(
                   3320:                                $env{'form.ccdomain'}, $env{'form.ccuname'},
1.240     raeburn  3321:                                $url,$rdom,$rnam,$rolename,0,$now,undef,$context);
1.294     bisitz   3322:                 $r->print(&mt('Re-enabling custom role [_1] by [_2] in [_3]: [_4]',
                   3323:                           $rolename,$rnam.':'.$rdom,$url,'<b>'.$result.'</b>').'<br />');
1.225     raeburn  3324:                 if (!grep(/^cr$/,@rolechanges)) {
                   3325:                     push(@rolechanges,'cr');
                   3326:                 }
1.116     raeburn  3327:             }
1.135     raeburn  3328: 	} elsif ($key=~/^form\.act/) {
1.101     albertel 3329:             my $udom = $env{'form.ccdomain'};
                   3330:             my $uname = $env{'form.ccuname'};
1.141     albertel 3331: 	    if ($key=~/^form\.act\_($match_domain)\_($match_courseid)\_cr_cr_($match_domain)_($match_username)_([^\_]+)$/) {
1.65      www      3332:                 # Activate a custom role
1.83      albertel 3333: 		my ($one,$two,$three,$four,$five)=($1,$2,$3,$4,$5);
                   3334: 		my $url='/'.$one.'/'.$two;
                   3335: 		my $full=$one.'_'.$two.'_cr_cr_'.$three.'_'.$four.'_'.$five;
1.65      www      3336: 
1.101     albertel 3337:                 my $start = ( $env{'form.start_'.$full} ?
                   3338:                               $env{'form.start_'.$full} :
1.88      raeburn  3339:                               $now );
1.101     albertel 3340:                 my $end   = ( $env{'form.end_'.$full} ?
                   3341:                               $env{'form.end_'.$full} :
1.88      raeburn  3342:                               0 );
                   3343:                                                                                      
                   3344:                 # split multiple sections
                   3345:                 my %sections = ();
1.101     albertel 3346:                 my $num_sections = &build_roles($env{'form.sec_'.$full},\%sections,$5);
1.88      raeburn  3347:                 if ($num_sections == 0) {
1.240     raeburn  3348:                     $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end,$context));
1.88      raeburn  3349:                 } else {
1.114     albertel 3350: 		    my %curr_groups =
1.117     raeburn  3351: 			&Apache::longroup::coursegroups($one,$two);
1.113     raeburn  3352:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                   3353:                         if (($sec eq 'none') || ($sec eq 'all') || 
                   3354:                             exists($curr_groups{$sec})) {
                   3355:                             $disallowed{$sec} = $url;
                   3356:                             next;
                   3357:                         }
                   3358:                         my $securl = $url.'/'.$sec;
1.240     raeburn  3359: 		        $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end,$context));
1.88      raeburn  3360:                     }
                   3361:                 }
1.225     raeburn  3362:                 if (!grep(/^cr$/,@rolechanges)) {
                   3363:                     push(@rolechanges,'cr');
                   3364:                 }
1.142     raeburn  3365: 	    } elsif ($key=~/^form\.act\_($match_domain)\_($match_name)\_([^\_]+)$/) {
1.27      matthew  3366: 		# Activate roles for sections with 3 id numbers
                   3367: 		# set start, end times, and the url for the class
1.83      albertel 3368: 		my ($one,$two,$three)=($1,$2,$3);
1.101     albertel 3369: 		my $start = ( $env{'form.start_'.$one.'_'.$two.'_'.$three} ? 
                   3370: 			      $env{'form.start_'.$one.'_'.$two.'_'.$three} : 
1.27      matthew  3371: 			      $now );
1.101     albertel 3372: 		my $end   = ( $env{'form.end_'.$one.'_'.$two.'_'.$three} ? 
                   3373: 			      $env{'form.end_'.$one.'_'.$two.'_'.$three} :
1.27      matthew  3374: 			      0 );
1.83      albertel 3375: 		my $url='/'.$one.'/'.$two;
1.88      raeburn  3376:                 my $type = 'three';
                   3377:                 # split multiple sections
                   3378:                 my %sections = ();
1.101     albertel 3379:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two.'_'.$three},\%sections,$three);
1.88      raeburn  3380:                 if ($num_sections == 0) {
1.240     raeburn  3381:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context));
1.88      raeburn  3382:                 } else {
1.114     albertel 3383:                     my %curr_groups = 
1.117     raeburn  3384: 			&Apache::longroup::coursegroups($one,$two);
1.88      raeburn  3385:                     my $emptysec = 0;
                   3386:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                   3387:                         $sec =~ s/\W//g;
1.113     raeburn  3388:                         if ($sec ne '') {
                   3389:                             if (($sec eq 'none') || ($sec eq 'all') || 
                   3390:                                 exists($curr_groups{$sec})) {
                   3391:                                 $disallowed{$sec} = $url;
                   3392:                                 next;
                   3393:                             }
1.88      raeburn  3394:                             my $securl = $url.'/'.$sec;
1.240     raeburn  3395:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec,$context));
1.88      raeburn  3396:                         } else {
                   3397:                             $emptysec = 1;
                   3398:                         }
                   3399:                     }
                   3400:                     if ($emptysec) {
1.240     raeburn  3401:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context));
1.88      raeburn  3402:                     }
1.225     raeburn  3403:                 }
                   3404:                 if (!grep(/^\Q$three\E$/,@rolechanges)) {
                   3405:                     push(@rolechanges,$three);
                   3406:                 }
1.135     raeburn  3407: 	    } elsif ($key=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
1.27      matthew  3408: 		# Activate roles for sections with two id numbers
                   3409: 		# set start, end times, and the url for the class
1.101     albertel 3410: 		my $start = ( $env{'form.start_'.$1.'_'.$2} ? 
                   3411: 			      $env{'form.start_'.$1.'_'.$2} : 
1.27      matthew  3412: 			      $now );
1.101     albertel 3413: 		my $end   = ( $env{'form.end_'.$1.'_'.$2} ? 
                   3414: 			      $env{'form.end_'.$1.'_'.$2} :
1.27      matthew  3415: 			      0 );
1.225     raeburn  3416:                 my $one = $1;
                   3417:                 my $two = $2;
                   3418: 		my $url='/'.$one.'/';
1.88      raeburn  3419:                 # split multiple sections
                   3420:                 my %sections = ();
1.225     raeburn  3421:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two},\%sections,$two);
1.88      raeburn  3422:                 if ($num_sections == 0) {
1.240     raeburn  3423:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  3424:                 } else {
                   3425:                     my $emptysec = 0;
                   3426:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                   3427:                         if ($sec ne '') {
                   3428:                             my $securl = $url.'/'.$sec;
1.240     raeburn  3429:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$two,$start,$end,$one,undef,$sec,$context));
1.88      raeburn  3430:                         } else {
                   3431:                             $emptysec = 1;
                   3432:                         }
                   3433:                     }
                   3434:                     if ($emptysec) {
1.240     raeburn  3435:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  3436:                     }
                   3437:                 }
1.225     raeburn  3438:                 if (!grep(/^\Q$two\E$/,@rolechanges)) {
                   3439:                     push(@rolechanges,$two);
                   3440:                 }
1.64      www      3441: 	    } else {
1.190     raeburn  3442: 		$r->print('<p><span class="LC_error">'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$key.'</tt></span></p><br />');
1.64      www      3443:             }
1.113     raeburn  3444:             foreach my $key (sort(keys(%disallowed))) {
1.274     bisitz   3445:                 $r->print('<p class="LC_warning">');
1.113     raeburn  3446:                 if (($key eq 'none') || ($key eq 'all')) {  
1.274     bisitz   3447:                     $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  3448:                 } else {
1.274     bisitz   3449:                     $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  3450:                 }
1.274     bisitz   3451:                 $r->print('</p><p>'
                   3452:                          .&mt('Please [_1]go back[_2] and choose a different section name.'
                   3453:                              ,'<a href="javascript:history.go(-1)'
                   3454:                              ,'</a>')
                   3455:                          .'</p><br />'
                   3456:                 );
1.113     raeburn  3457:             }
                   3458: 	}
1.101     albertel 3459:     } # End of foreach (keys(%env))
1.75      www      3460: # Flush the course logs so reverse user roles immediately updated
1.349     raeburn  3461:     $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
1.225     raeburn  3462:     if (@rolechanges == 0) {
1.193     raeburn  3463:         $r->print(&mt('No roles to modify'));
                   3464:     }
1.225     raeburn  3465:     return @rolechanges;
1.220     raeburn  3466: }
                   3467: 
                   3468: sub enroll_single_student {
1.318     raeburn  3469:     my ($r,$uhome,$amode,$genpwd,$now,$newuser,$context,$crstype) = @_;
                   3470:     $r->print('<h3>');
                   3471:     if ($crstype eq 'Community') {
                   3472:         $r->print(&mt('Enrolling Member'));
                   3473:     } else {
                   3474:         $r->print(&mt('Enrolling Student'));
                   3475:     }
                   3476:     $r->print('</h3>');
1.220     raeburn  3477: 
                   3478:     # Remove non alphanumeric values from section
                   3479:     $env{'form.sections'}=~s/\W//g;
                   3480: 
                   3481:     # Clean out any old student roles the user has in this class.
                   3482:     &Apache::lonuserutils::modifystudent($env{'form.ccdomain'},
                   3483:          $env{'form.ccuname'},$env{'request.course.id'},undef,$uhome);
                   3484:     my ($startdate,$enddate) = &Apache::lonuserutils::get_dates_from_form();
                   3485:     my $enroll_result =
                   3486:         &Apache::lonnet::modify_student_enrollment($env{'form.ccdomain'},
                   3487:             $env{'form.ccuname'},$env{'form.cid'},$env{'form.cfirstname'},
                   3488:             $env{'form.cmiddlename'},$env{'form.clastname'},
                   3489:             $env{'form.generation'},$env{'form.sections'},$enddate,
1.239     raeburn  3490:             $startdate,'manual',undef,$env{'request.course.id'},'',$context);
1.220     raeburn  3491:     if ($enroll_result =~ /^ok/) {
                   3492:         $r->print(&mt('<b>[_1]</b> enrolled',$env{'form.ccuname'}.':'.$env{'form.ccdomain'}));
                   3493:         if ($env{'form.sections'} ne '') {
                   3494:             $r->print(' '.&mt('in section [_1]',$env{'form.sections'}));
                   3495:         }
                   3496:         my ($showstart,$showend);
                   3497:         if ($startdate <= $now) {
                   3498:             $showstart = &mt('Access starts immediately');
                   3499:         } else {
                   3500:             $showstart = &mt('Access starts: ').&Apache::lonlocal::locallocaltime($startdate);
                   3501:         }
                   3502:         if ($enddate == 0) {
                   3503:             $showend = &mt('ends: no ending date');
                   3504:         } else {
                   3505:             $showend = &mt('ends: ').&Apache::lonlocal::locallocaltime($enddate);
                   3506:         }
                   3507:         $r->print('.<br />'.$showstart.'; '.$showend);
                   3508:         if ($startdate <= $now && !$newuser) {
1.318     raeburn  3509:             $r->print('<p> ');
                   3510:             if ($crstype eq 'Community') {
                   3511:                 $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.'));
                   3512:             } else {
                   3513:                 $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.'));
                   3514:            }
                   3515:            $r->print('</p>');
1.220     raeburn  3516:         }
                   3517:     } else {
                   3518:         $r->print(&mt('unable to enroll').": ".$enroll_result);
                   3519:     }
                   3520:     return;
1.188     raeburn  3521: }
                   3522: 
1.204     raeburn  3523: sub get_defaultquota_text {
                   3524:     my ($settingstatus) = @_;
                   3525:     my $defquotatext; 
                   3526:     if ($settingstatus eq '') {
                   3527:         $defquotatext = &mt('(default)');
                   3528:     } else {
                   3529:         my ($usertypes,$order) =
                   3530:             &Apache::lonnet::retrieve_inst_usertypes($env{'form.ccdomain'});
                   3531:         if ($usertypes->{$settingstatus} eq '') {
                   3532:             $defquotatext = &mt('(default)');
                   3533:         } else {
                   3534:             $defquotatext = &mt('(default for [_1])',$usertypes->{$settingstatus});
                   3535:         }
                   3536:     }
                   3537:     return $defquotatext;
                   3538: }
                   3539: 
1.188     raeburn  3540: sub update_result_form {
                   3541:     my ($uhome) = @_;
                   3542:     my $outcome = 
                   3543:     '<form name="userupdate" method="post" />'."\n";
1.160     raeburn  3544:     foreach my $item ('srchby','srchin','srchtype','srchterm','srchdomain','ccuname','ccdomain') {
1.188     raeburn  3545:         $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  3546:     }
1.207     raeburn  3547:     if ($env{'form.origname'} ne '') {
                   3548:         $outcome .= '<input type="hidden" name="origname" value="'.$env{'form.origname'}.'" />'."\n";
                   3549:     }
1.160     raeburn  3550:     foreach my $item ('sortby','seluname','seludom') {
                   3551:         if (exists($env{'form.'.$item})) {
1.188     raeburn  3552:             $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  3553:         }
                   3554:     }
1.188     raeburn  3555:     if ($uhome eq 'no_host') {
                   3556:         $outcome .= '<input type="hidden" name="forcenewuser" value="1" />'."\n";
                   3557:     }
                   3558:     $outcome .= '<input type="hidden" name="phase" value="" />'."\n".
                   3559:                 '<input type ="hidden" name="currstate" value="" />'."\n".
1.220     raeburn  3560:                 '<input type ="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
1.188     raeburn  3561:                 '</form>';
                   3562:     return $outcome;
1.4       www      3563: }
                   3564: 
1.149     raeburn  3565: sub quota_admin {
                   3566:     my ($setquota,$changeHash) = @_;
                   3567:     my $quotachanged;
                   3568:     if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
                   3569:         # Current user has quota modification privileges
1.267     raeburn  3570:         if (ref($changeHash) eq 'HASH') {
                   3571:             $quotachanged = 1;
                   3572:             $changeHash->{'portfolioquota'} = $setquota;
                   3573:         }
1.149     raeburn  3574:     }
                   3575:     return $quotachanged;
                   3576: }
                   3577: 
1.267     raeburn  3578: sub tool_admin {
1.275     raeburn  3579:     my ($tool,$settool,$changeHash,$context) = @_;
                   3580:     my $canchange = 0; 
1.279     raeburn  3581:     if ($context eq 'requestcourses') {
1.275     raeburn  3582:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
                   3583:             $canchange = 1;
                   3584:         }
1.300     raeburn  3585:     } elsif ($context eq 'reqcrsotherdom') {
                   3586:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
                   3587:             $canchange = 1;
                   3588:         }
1.275     raeburn  3589:     } elsif (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
                   3590:         # Current user has quota modification privileges
                   3591:         $canchange = 1;
                   3592:     }
1.267     raeburn  3593:     my $toolchanged;
1.275     raeburn  3594:     if ($canchange) {
1.267     raeburn  3595:         if (ref($changeHash) eq 'HASH') {
                   3596:             $toolchanged = 1;
1.275     raeburn  3597:             $changeHash->{$context.'.'.$tool} = $settool;
1.267     raeburn  3598:         }
                   3599:     }
                   3600:     return $toolchanged;
                   3601: }
                   3602: 
1.88      raeburn  3603: sub build_roles {
1.89      raeburn  3604:     my ($sectionstr,$sections,$role) = @_;
1.88      raeburn  3605:     my $num_sections = 0;
                   3606:     if ($sectionstr=~ /,/) {
                   3607:         my @secnums = split/,/,$sectionstr;
1.89      raeburn  3608:         if ($role eq 'st') {
                   3609:             $secnums[0] =~ s/\W//g;
                   3610:             $$sections{$secnums[0]} = 1;
                   3611:             $num_sections = 1;
                   3612:         } else {
                   3613:             foreach my $sec (@secnums) {
                   3614:                 $sec =~ ~s/\W//g;
1.150     banghart 3615:                 if (!($sec eq "")) {
1.89      raeburn  3616:                     if (exists($$sections{$sec})) {
                   3617:                         $$sections{$sec} ++;
                   3618:                     } else {
                   3619:                         $$sections{$sec} = 1;
                   3620:                         $num_sections ++;
                   3621:                     }
1.88      raeburn  3622:                 }
                   3623:             }
                   3624:         }
                   3625:     } else {
                   3626:         $sectionstr=~s/\W//g;
                   3627:         unless ($sectionstr eq '') {
                   3628:             $$sections{$sectionstr} = 1;
                   3629:             $num_sections ++;
                   3630:         }
                   3631:     }
1.129     albertel 3632: 
1.88      raeburn  3633:     return $num_sections;
                   3634: }
                   3635: 
1.58      www      3636: # ========================================================== Custom Role Editor
                   3637: 
                   3638: sub custom_role_editor {
1.351     raeburn  3639:     my ($r,$brcrum) = @_;
1.324     raeburn  3640:     my $action = $env{'form.customroleaction'};
                   3641:     my $rolename; 
                   3642:     if ($action eq 'new') {
                   3643:         $rolename=$env{'form.newrolename'};
                   3644:     } else {
                   3645:         $rolename=$env{'form.rolename'};
1.59      www      3646:     }
                   3647: 
1.324     raeburn  3648:     my ($crstype,$context);
                   3649:     if ($env{'request.course.id'}) {
                   3650:         $crstype = &Apache::loncommon::course_type();
                   3651:         $context = 'course';
                   3652:     } else {
                   3653:         $context = 'domain';
                   3654:         $crstype = $env{'form.templatecrstype'};
                   3655:     }
1.351     raeburn  3656: 
                   3657:     $rolename=~s/[^A-Za-z0-9]//gs;
                   3658:     if (!$rolename || $env{'form.phase'} eq 'pickrole') {
                   3659: 	&print_username_entry_form($r,undef,undef,undef,undef,$crstype,$brcrum);
                   3660:         return;
                   3661:     }
                   3662: 
1.153     banghart 3663: # ------------------------------------------------------- What can be assigned?
                   3664:     my %full=();
                   3665:     my %courselevel=();
                   3666:     my %courselevelcurrent=();
1.61      www      3667:     my $syspriv='';
                   3668:     my $dompriv='';
                   3669:     my $coursepriv='';
1.153     banghart 3670:     my $body_top;
1.59      www      3671:     my ($rdummy,$roledef)=
                   3672: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
1.60      www      3673: # ------------------------------------------------------- Does this role exist?
1.153     banghart 3674:     $body_top .= '<h2>';
1.59      www      3675:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.153     banghart 3676: 	$body_top .= &mt('Existing Role').' "';
1.61      www      3677: # ------------------------------------------------- Get current role privileges
                   3678: 	($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
1.324     raeburn  3679:         if ($crstype eq 'Community') {
                   3680:             $syspriv =~ s/bre\&S//;   
                   3681:         }
1.59      www      3682:     } else {
1.153     banghart 3683: 	$body_top .= &mt('New Role').' "';
1.59      www      3684: 	$roledef='';
                   3685:     }
1.153     banghart 3686:     $body_top .= $rolename.'"</h2>';
1.135     raeburn  3687:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   3688: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 3689:         if (!$restrict) { $restrict='F'; }
1.60      www      3690:         $courselevel{$priv}=$restrict;
1.61      www      3691:         if ($coursepriv=~/\:$priv/) {
                   3692: 	    $courselevelcurrent{$priv}=1;
                   3693: 	}
1.60      www      3694: 	$full{$priv}=1;
                   3695:     }
                   3696:     my %domainlevel=();
1.61      www      3697:     my %domainlevelcurrent=();
1.135     raeburn  3698:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   3699: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 3700:         if (!$restrict) { $restrict='F'; }
1.60      www      3701:         $domainlevel{$priv}=$restrict;
1.61      www      3702:         if ($dompriv=~/\:$priv/) {
                   3703: 	    $domainlevelcurrent{$priv}=1;
                   3704: 	}
1.60      www      3705: 	$full{$priv}=1;
                   3706:     }
1.61      www      3707:     my %systemlevel=();
                   3708:     my %systemlevelcurrent=();
1.135     raeburn  3709:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
                   3710: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 3711:         if (!$restrict) { $restrict='F'; }
1.61      www      3712:         $systemlevel{$priv}=$restrict;
                   3713:         if ($syspriv=~/\:$priv/) {
                   3714: 	    $systemlevelcurrent{$priv}=1;
                   3715: 	}
                   3716: 	$full{$priv}=1;
                   3717:     }
1.160     raeburn  3718:     my ($jsback,$elements) = &crumb_utilities();
1.154     banghart 3719:     my $button_code = "\n";
1.153     banghart 3720:     my $head_script = "\n";
1.301     bisitz   3721:     $head_script .= '<script type="text/javascript">'."\n"
                   3722:                    .'// <![CDATA['."\n";
1.324     raeburn  3723:     my @template_roles = ("in","ta","ep");
                   3724:     if ($context eq 'domain') {
                   3725:         push(@template_roles,"ad");
1.318     raeburn  3726:     }
1.324     raeburn  3727:     push(@template_roles,"st");
1.318     raeburn  3728:     if ($crstype eq 'Community') {
                   3729:         unshift(@template_roles,'co');
                   3730:     } else {
                   3731:         unshift(@template_roles,'cc');
                   3732:     }
1.154     banghart 3733:     foreach my $role (@template_roles) {
1.324     raeburn  3734:         $head_script .= &make_script_template($role,$crstype);
1.318     raeburn  3735:         $button_code .= &make_button_code($role,$crstype).' ';
1.154     banghart 3736:     }
1.324     raeburn  3737:     my $context_code;
                   3738:     if ($context eq 'domain') {
                   3739:         my $checkedCommunity = '';
                   3740:         my $checkedCourse = ' checked="checked"';
                   3741:         if ($env{'form.templatecrstype'} eq 'Community') {
                   3742:             $checkedCommunity = $checkedCourse;
                   3743:             $checkedCourse = '';
                   3744:         }
                   3745:         $context_code = '<label>'.
                   3746:                         '<input type="radio" name="templatecrstype" value="Course"'.$checkedCourse.' onclick="this.form.submit();">'.
                   3747:                         &mt('Course').
                   3748:                         '</label>'.('&nbsp;' x2).
                   3749:                         '<label>'.
                   3750:                         '<input type="radio" name="templatecrstype" value="Community"'.$checkedCommunity.' onclick="this.form.submit();">'.
                   3751:                         &mt('Community').
                   3752:                         '</label>'.
                   3753:                         '</fieldset>'.
                   3754:                         '<input type="hidden" name="customroleaction" value="'.
                   3755:                         $action.'" />';
                   3756:         if ($env{'form.customroleaction'} eq 'new') {
                   3757:             $context_code .= '<input type="hidden" name="newrolename" value="'.
                   3758:                              $rolename.'" />';
                   3759:         } else {
                   3760:             $context_code .= '<input type="hidden" name="rolename" value="'.
                   3761:                              $rolename.'" />';
                   3762:         }
                   3763:         $context_code .= '<input type="hidden" name="action" value="custom" />'.
                   3764:                          '<input type="hidden" name="phase" value="selected_custom_edit" />';
                   3765:     }
                   3766: 
1.301     bisitz   3767:     $head_script .= "\n".$jsback."\n"
                   3768:                    .'// ]]>'."\n"
                   3769:                    .'</script>'."\n";
1.351     raeburn  3770:     push (@{$brcrum},
                   3771:               {href => "javascript:backPage(document.form1,'pickrole','')",
                   3772:                text => "Pick custom role",
                   3773:                faq  => 282,bug=>'Instructor Interface',},
                   3774:               {href => "javascript:backPage(document.form1,'','')",
                   3775:                text => "Edit custom role",
                   3776:                faq  => 282,
                   3777:                bug  => 'Instructor Interface',
                   3778:                help => 'Course_Editing_Custom_Roles'}
                   3779:               );
                   3780:     my $args = { bread_crumbs          => $brcrum,
                   3781:                  bread_crumbs_component => 'User Management'};
                   3782:  
                   3783:     $r->print(&Apache::loncommon::start_page('Custom Role Editor',
                   3784:                                              $head_script,$args).
                   3785:               $body_top);
1.73      sakharuk 3786:     my %lt=&Apache::lonlocal::texthash(
                   3787: 		    'prv'  => "Privilege",
1.131     raeburn  3788: 		    'crl'  => "Course Level",
1.73      sakharuk 3789:                     'dml'  => "Domain Level",
1.150     banghart 3790:                     'ssl'  => "System Level");
1.264     bisitz   3791: 
1.324     raeburn  3792:     $r->print('<div class="LC_left_float">'
1.264     bisitz   3793:              .'<form action=""><fieldset>'
                   3794:              .'<legend>'.&mt('Select a Template').'</legend>'
                   3795:              .$button_code
1.324     raeburn  3796:              .'</fieldset></form></div>');
                   3797:     if ($context_code) {
                   3798:         $r->print('<div class="LC_left_float">'
                   3799:                  .'<form action="/adm/createuser" method="post"><fieldset>'
                   3800:                  .'<legend>'.&mt('Context').'</legend>'
                   3801:                  .$context_code
                   3802:                  .'</form>'
                   3803:                  .'</div>'
                   3804:         );
                   3805:     }
                   3806:     $r->print('<br clear="all" />');
1.264     bisitz   3807: 
1.61      www      3808:     $r->print(<<ENDCCF);
1.160     raeburn  3809: <form name="form1" method="post">
1.61      www      3810: <input type="hidden" name="phase" value="set_custom_roles" />
                   3811: <input type="hidden" name="rolename" value="$rolename" />
                   3812: ENDCCF
1.135     raeburn  3813:     $r->print(&Apache::loncommon::start_data_table().
                   3814:               &Apache::loncommon::start_data_table_header_row(). 
                   3815: '<th>'.$lt{'prv'}.'</th><th>'.$lt{'crl'}.'</th><th>'.$lt{'dml'}.
                   3816: '</th><th>'.$lt{'ssl'}.'</th>'.
                   3817:               &Apache::loncommon::end_data_table_header_row());
1.324     raeburn  3818:     foreach my $priv (sort(keys(%full))) {
1.318     raeburn  3819:         my $privtext = &Apache::lonnet::plaintext($priv,$crstype);
1.135     raeburn  3820:         $r->print(&Apache::loncommon::start_data_table_row().
                   3821: 	          '<td>'.$privtext.'</td><td>'.
1.288     bisitz   3822:     ($courselevel{$priv}?'<input type="checkbox" name="'.$priv.'_c"'.
                   3823:     ($courselevelcurrent{$priv}?' checked="checked"':'').' />':'&nbsp;').
1.61      www      3824:     '</td><td>'.
1.288     bisitz   3825:     ($domainlevel{$priv}?'<input type="checkbox" name="'.$priv.'_d"'.
                   3826:     ($domainlevelcurrent{$priv}?' checked="checked"':'').' />':'&nbsp;').
1.324     raeburn  3827:     '</td><td>');
                   3828:         if ($priv eq 'bre' && $crstype eq 'Community') {
                   3829:             $r->print('&nbsp;');  
                   3830:         } else {
                   3831:             $r->print($systemlevel{$priv}?'<input type="checkbox" name="'.$priv.'_s"'.
                   3832:                       ($systemlevelcurrent{$priv}?' checked="checked"':'').' />':'&nbsp;');
                   3833:         }
                   3834:         $r->print('</td>'.
                   3835:                   &Apache::loncommon::end_data_table_row());
1.60      www      3836:     }
1.135     raeburn  3837:     $r->print(&Apache::loncommon::end_data_table().
1.190     raeburn  3838:    '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
1.160     raeburn  3839:    '<input type="hidden" name="startrolename" value="'.$env{'form.rolename'}.
1.179     raeburn  3840:    '" />'."\n".'<input type="hidden" name="currstate" value="" />'."\n".   
1.160     raeburn  3841:    '<input type="reset" value="'.&mt("Reset").'" />'."\n".
1.351     raeburn  3842:    '<input type="submit" value="'.&mt('Save').'" /></form>');
1.61      www      3843: }
1.153     banghart 3844: # --------------------------------------------------------
                   3845: sub make_script_template {
1.324     raeburn  3846:     my ($role,$crstype) = @_;
1.153     banghart 3847:     my %full_c=();
                   3848:     my %full_d=();
                   3849:     my %full_s=();
                   3850:     my $return_script;
                   3851:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   3852:         my ($priv,$restrict)=split(/\&/,$item);
                   3853:         $full_c{$priv}=1;
                   3854:     }
                   3855:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   3856:         my ($priv,$restrict)=split(/\&/,$item);
                   3857:         $full_d{$priv}=1;
                   3858:     }
1.154     banghart 3859:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
1.324     raeburn  3860:         next if (($crstype eq 'Community') && ($item eq 'bre&S'));
1.153     banghart 3861:         my ($priv,$restrict)=split(/\&/,$item);
                   3862:         $full_s{$priv}=1;
                   3863:     }
                   3864:     $return_script .= 'function set_'.$role.'() {'."\n";
                   3865:     my @temp = split(/:/,$Apache::lonnet::pr{$role.':c'});
                   3866:     my %role_c;
1.155     banghart 3867:     foreach my $priv (@temp) {
1.153     banghart 3868:         my ($priv_item, $dummy) = split(/\&/,$priv);
                   3869:         $role_c{$priv_item} = 1;
                   3870:     }
1.269     raeburn  3871:     my %role_d;
                   3872:     @temp = split(/:/,$Apache::lonnet::pr{$role.':d'});
                   3873:     foreach my $priv(@temp) {
                   3874:         my ($priv_item, $dummy) = split(/\&/,$priv);
                   3875:         $role_d{$priv_item} = 1;
                   3876:     }
                   3877:     my %role_s;
                   3878:     @temp = split(/:/,$Apache::lonnet::pr{$role.':s'});
                   3879:     foreach my $priv(@temp) {
                   3880:         my ($priv_item, $dummy) = split(/\&/,$priv);
                   3881:         $role_s{$priv_item} = 1;
                   3882:     }
1.153     banghart 3883:     foreach my $priv_item (keys(%full_c)) {
                   3884:         my ($priv, $dummy) = split(/\&/,$priv_item);
1.269     raeburn  3885:         if ((exists($role_c{$priv})) || (exists($role_d{$priv})) || 
                   3886:             (exists($role_s{$priv}))) {
1.153     banghart 3887:             $return_script .= "document.form1.$priv"."_c.checked = true;\n";
                   3888:         } else {
                   3889:             $return_script .= "document.form1.$priv"."_c.checked = false;\n";
                   3890:         }
                   3891:     }
1.154     banghart 3892:     foreach my $priv_item (keys(%full_d)) {
                   3893:         my ($priv, $dummy) = split(/\&/,$priv_item);
1.269     raeburn  3894:         if ((exists($role_d{$priv})) || (exists($role_s{$priv}))) {
1.154     banghart 3895:             $return_script .= "document.form1.$priv"."_d.checked = true;\n";
                   3896:         } else {
                   3897:             $return_script .= "document.form1.$priv"."_d.checked = false;\n";
                   3898:         }
                   3899:     }
                   3900:     foreach my $priv_item (keys(%full_s)) {
1.153     banghart 3901:         my ($priv, $dummy) = split(/\&/,$priv_item);
1.154     banghart 3902:         if (exists($role_s{$priv})) {
                   3903:             $return_script .= "document.form1.$priv"."_s.checked = true;\n";
                   3904:         } else {
                   3905:             $return_script .= "document.form1.$priv"."_s.checked = false;\n";
                   3906:         }
1.153     banghart 3907:     }
                   3908:     $return_script .= '}'."\n";
1.154     banghart 3909:     return ($return_script);
                   3910: }
                   3911: # ----------------------------------------------------------
                   3912: sub make_button_code {
1.318     raeburn  3913:     my ($role,$crstype) = @_;
                   3914:     my $label = &Apache::lonnet::plaintext($role,$crstype);
1.301     bisitz   3915:     my $button_code = '<input type="button" onclick="set_'.$role.'()" value="'.$label.'" />';
1.154     banghart 3916:     return ($button_code);
1.153     banghart 3917: }
1.61      www      3918: # ---------------------------------------------------------- Call to definerole
                   3919: sub set_custom_role {
1.351     raeburn  3920:     my ($r,$context,$brcrum) = @_;
1.101     albertel 3921:     my $rolename=$env{'form.rolename'};
1.63      www      3922:     $rolename=~s/[^A-Za-z0-9]//gs;
1.150     banghart 3923:     if (!$rolename) {
1.351     raeburn  3924: 	&custom_role_editor($r,$brcrum);
1.61      www      3925:         return;
                   3926:     }
1.160     raeburn  3927:     my ($jsback,$elements) = &crumb_utilities();
1.301     bisitz   3928:     my $jscript = '<script type="text/javascript">'
                   3929:                  .'// <![CDATA['."\n"
                   3930:                  .$jsback."\n"
                   3931:                  .'// ]]>'."\n"
                   3932:                  .'</script>'."\n";
1.352     raeburn  3933:     push(@{$brcrum},
                   3934:         {href => "javascript:backPage(document.customresult,'pickrole','')",
                   3935:          text => "Pick custom role",
                   3936:          faq  => 282,
                   3937:          bug  => 'Instructor Interface',},
                   3938:         {href => "javascript:backPage(document.customresult,'selected_custom_edit','')",
                   3939:          text => "Edit custom role",
                   3940:          faq  => 282,
                   3941:          bug  => 'Instructor Interface',},
                   3942:         {href => "javascript:backPage(document.customresult,'set_custom_roles','')",
                   3943:          text => "Result",
                   3944:          faq  => 282,
                   3945:          bug  => 'Instructor Interface',
                   3946:          help => 'Course_Editing_Custom_Roles'},
                   3947:         );
                   3948:     my $args = { bread_crumbs           => $brcrum,
1.351     raeburn  3949:                  bread_crumbs_component => 'User Management'}; 
                   3950:     $r->print(&Apache::loncommon::start_page('Save Custom Role',$jscript,$args));
1.160     raeburn  3951: 
1.61      www      3952:     my ($rdummy,$roledef)=
1.110     albertel 3953: 	&Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   3954: 
1.61      www      3955: # ------------------------------------------------------- Does this role exist?
1.188     raeburn  3956:     $r->print('<h3>');
1.61      www      3957:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.73      sakharuk 3958: 	$r->print(&mt('Existing Role').' "');
1.61      www      3959:     } else {
1.73      sakharuk 3960: 	$r->print(&mt('New Role').' "');
1.61      www      3961: 	$roledef='';
                   3962:     }
1.188     raeburn  3963:     $r->print($rolename.'"</h3>');
1.61      www      3964: # ------------------------------------------------------- What can be assigned?
                   3965:     my $sysrole='';
                   3966:     my $domrole='';
                   3967:     my $courole='';
                   3968: 
1.135     raeburn  3969:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   3970: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 3971:         if (!$restrict) { $restrict=''; }
                   3972:         if ($env{'form.'.$priv.'_c'}) {
1.135     raeburn  3973: 	    $courole.=':'.$item;
1.61      www      3974: 	}
                   3975:     }
                   3976: 
1.135     raeburn  3977:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   3978: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 3979:         if (!$restrict) { $restrict=''; }
                   3980:         if ($env{'form.'.$priv.'_d'}) {
1.135     raeburn  3981: 	    $domrole.=':'.$item;
1.61      www      3982: 	}
                   3983:     }
                   3984: 
1.135     raeburn  3985:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
                   3986: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 3987:         if (!$restrict) { $restrict=''; }
                   3988:         if ($env{'form.'.$priv.'_s'}) {
1.135     raeburn  3989: 	    $sysrole.=':'.$item;
1.61      www      3990: 	}
                   3991:     }
1.63      www      3992:     $r->print('<br />Defining Role: '.
1.61      www      3993: 	   &Apache::lonnet::definerole($rolename,$sysrole,$domrole,$courole));
1.101     albertel 3994:     if ($env{'request.course.id'}) {
                   3995:         my $url='/'.$env{'request.course.id'};
1.63      www      3996:         $url=~s/\_/\//g;
1.73      sakharuk 3997: 	$r->print('<br />'.&mt('Assigning Role to Self').': '.
1.101     albertel 3998: 	      &Apache::lonnet::assigncustomrole($env{'user.domain'},
                   3999: 						$env{'user.name'},
1.63      www      4000: 						$url,
1.101     albertel 4001: 						$env{'user.domain'},
                   4002: 						$env{'user.name'},
1.240     raeburn  4003: 						$rolename,undef,undef,undef,$context));
1.63      www      4004:     }
1.190     raeburn  4005:     $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  4006:     $r->print(&Apache::lonhtmlcommon::echo_form_input([]).'</form>');
1.58      www      4007: }
                   4008: 
1.2       www      4009: # ================================================================ Main Handler
                   4010: sub handler {
                   4011:     my $r = shift;
                   4012:     if ($r->header_only) {
1.68      www      4013:        &Apache::loncommon::content_type($r,'text/html');
1.2       www      4014:        $r->send_http_header;
                   4015:        return OK;
                   4016:     }
1.318     raeburn  4017:     my ($context,$crstype);
1.190     raeburn  4018:     if ($env{'request.course.id'}) {
                   4019:         $context = 'course';
1.318     raeburn  4020:         $crstype = &Apache::loncommon::course_type();
1.190     raeburn  4021:     } elsif ($env{'request.role'} =~ /^au\./) {
1.206     raeburn  4022:         $context = 'author';
1.190     raeburn  4023:     } else {
                   4024:         $context = 'domain';
                   4025:     }
                   4026:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.233     raeburn  4027:         ['action','state','callingform','roletype','showrole','bulkaction','popup','phase',
                   4028:          'username','domain','srchterm','srchdomain','srchin','srchby','srchtype']);
1.190     raeburn  4029:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.351     raeburn  4030:     my $args;
                   4031:     my $brcrum = [];
                   4032:     my $bread_crumbs_component = 'User Management';
1.202     raeburn  4033:     if ($env{'form.action'} ne 'dateselect') {
1.351     raeburn  4034:         $brcrum = [{href=>"/adm/createuser",
                   4035:                     text=>"User Management",
                   4036:                     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'}
                   4037:                   ];
1.202     raeburn  4038:     }
1.289     droeschl 4039:     #SD Following files not added to help, because the corresponding .tex-files seem to
                   4040:     #be missing: Course_Approve_Selfenroll,Course_User_Logs,
1.209     raeburn  4041:     my ($permission,$allowed) = 
1.318     raeburn  4042:         &Apache::lonuserutils::get_permission($context,$crstype);
1.190     raeburn  4043:     if (!$allowed) {
                   4044:         $env{'user.error.msg'}=
                   4045:             "/adm/createuser:cst:0:0:Cannot create/modify user data ".
                   4046:                                  "or view user status.";
                   4047:         return HTTP_NOT_ACCEPTABLE;
                   4048:     }
                   4049: 
                   4050:     &Apache::loncommon::content_type($r,'text/html');
                   4051:     $r->send_http_header;
                   4052: 
                   4053:     # Main switch on form.action and form.state, as appropriate
                   4054:     if (! exists($env{'form.action'})) {
1.351     raeburn  4055:         $args = {bread_crumbs => $brcrum,
                   4056:                  bread_crumbs_component => $bread_crumbs_component}; 
                   4057:         $r->print(&header(undef,$args));
1.318     raeburn  4058:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  4059:     } elsif ($env{'form.action'} eq 'upload' && $permission->{'cusr'}) {
1.351     raeburn  4060:         push(@{$brcrum},
                   4061:               { href => '/adm/createuser?action=upload&state=',
                   4062:                 text => 'Upload Users List',
                   4063:                 help => 'Course_Create_Class_List',
                   4064:               });
                   4065:         $bread_crumbs_component = 'Upload Users List';
                   4066:         $args = {bread_crumbs           => $brcrum,
                   4067:                  bread_crumbs_component => $bread_crumbs_component};
                   4068:         $r->print(&header(undef,$args));
1.190     raeburn  4069:         $r->print('<form name="studentform" method="post" '.
                   4070:                   'enctype="multipart/form-data" '.
                   4071:                   ' action="/adm/createuser">'."\n");
                   4072:         if (! exists($env{'form.state'})) {
                   4073:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   4074:         } elsif ($env{'form.state'} eq 'got_file') {
1.221     raeburn  4075:             &Apache::lonuserutils::print_upload_manager_form($r,$context,
1.318     raeburn  4076:                                                              $permission,$crstype);
1.190     raeburn  4077:         } elsif ($env{'form.state'} eq 'enrolling') {
                   4078:             if ($env{'form.datatoken'}) {
1.221     raeburn  4079:                 &Apache::lonuserutils::upfile_drop_add($r,$context,$permission);
1.190     raeburn  4080:             }
                   4081:         } else {
                   4082:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   4083:         }
1.213     raeburn  4084:     } elsif ((($env{'form.action'} eq 'singleuser') || ($env{'form.action'}
                   4085:              eq 'singlestudent')) && ($permission->{'cusr'})) {
1.190     raeburn  4086:         my $phase = $env{'form.phase'};
                   4087:         my @search = ('srchterm','srchby','srchin','srchtype','srchdomain');
1.192     albertel 4088: 	&Apache::loncreateuser::restore_prev_selections();
                   4089: 	my $srch;
                   4090: 	foreach my $item (@search) {
                   4091: 	    $srch->{$item} = $env{'form.'.$item};
                   4092: 	}
1.207     raeburn  4093:         if (($phase eq 'get_user_info') || ($phase eq 'userpicked') ||
                   4094:             ($phase eq 'createnewuser')) {
                   4095:             if ($env{'form.phase'} eq 'createnewuser') {
                   4096:                 my $response;
                   4097:                 if ($env{'form.srchterm'} !~ /^$match_username$/) {
                   4098:                     my $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @');
1.221     raeburn  4099:                     $env{'form.phase'} = '';
1.351     raeburn  4100:                     &print_username_entry_form($r,$context,$response,$srch,undef,$crstype,$brcrum);
1.207     raeburn  4101:                 } else {
                   4102:                     my $ccuname =&LONCAPA::clean_username($srch->{'srchterm'});
                   4103:                     my $ccdomain=&LONCAPA::clean_domain($srch->{'srchdomain'});
                   4104:                     &print_user_modification_page($r,$ccuname,$ccdomain,
1.221     raeburn  4105:                                                   $srch,$response,$context,
1.351     raeburn  4106:                                                   $permission,$crstype,$brcrum);
1.207     raeburn  4107:                 }
                   4108:             } elsif ($env{'form.phase'} eq 'get_user_info') {
1.190     raeburn  4109:                 my ($currstate,$response,$forcenewuser,$results) = 
1.221     raeburn  4110:                     &user_search_result($context,$srch);
1.190     raeburn  4111:                 if ($env{'form.currstate'} eq 'modify') {
                   4112:                     $currstate = $env{'form.currstate'};
                   4113:                 }
                   4114:                 if ($currstate eq 'select') {
                   4115:                     &print_user_selection_page($r,$response,$srch,$results,
1.351     raeburn  4116:                                                \@search,$context,undef,$crstype,
                   4117:                                                $brcrum);
1.190     raeburn  4118:                 } elsif ($currstate eq 'modify') {
                   4119:                     my ($ccuname,$ccdomain);
                   4120:                     if (($srch->{'srchby'} eq 'uname') && 
                   4121:                         ($srch->{'srchtype'} eq 'exact')) {
                   4122:                         $ccuname = $srch->{'srchterm'};
                   4123:                         $ccdomain= $srch->{'srchdomain'};
                   4124:                     } else {
                   4125:                         my @matchedunames = keys(%{$results});
                   4126:                         ($ccuname,$ccdomain) = split(/:/,$matchedunames[0]);
                   4127:                     }
                   4128:                     $ccuname =&LONCAPA::clean_username($ccuname);
                   4129:                     $ccdomain=&LONCAPA::clean_domain($ccdomain);
                   4130:                     if ($env{'form.forcenewuser'}) {
                   4131:                         $response = '';
                   4132:                     }
                   4133:                     &print_user_modification_page($r,$ccuname,$ccdomain,
1.221     raeburn  4134:                                                   $srch,$response,$context,
1.351     raeburn  4135:                                                   $permission,$crstype,$brcrum);
1.190     raeburn  4136:                 } elsif ($currstate eq 'query') {
1.351     raeburn  4137:                     &print_user_query_page($r,'createuser',$brcrum);
1.190     raeburn  4138:                 } else {
1.229     raeburn  4139:                     $env{'form.phase'} = '';
1.207     raeburn  4140:                     &print_username_entry_form($r,$context,$response,$srch,
1.351     raeburn  4141:                                                $forcenewuser,$crstype,$brcrum);
1.190     raeburn  4142:                 }
                   4143:             } elsif ($env{'form.phase'} eq 'userpicked') {
                   4144:                 my $ccuname = &LONCAPA::clean_username($env{'form.seluname'});
                   4145:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.seludom'});
1.196     raeburn  4146:                 &print_user_modification_page($r,$ccuname,$ccdomain,$srch,'',
1.351     raeburn  4147:                                               $context,$permission,$crstype,
                   4148:                                               $brcrum);
1.190     raeburn  4149:             }
                   4150:         } elsif ($env{'form.phase'} eq 'update_user_data') {
1.351     raeburn  4151:             &update_user_data($r,$context,$crstype,$brcrum);
1.190     raeburn  4152:         } else {
1.351     raeburn  4153:             &print_username_entry_form($r,$context,undef,$srch,undef,$crstype,
                   4154:                                        $brcrum);
1.190     raeburn  4155:         }
                   4156:     } elsif ($env{'form.action'} eq 'custom' && $permission->{'custom'}) {
                   4157:         if ($env{'form.phase'} eq 'set_custom_roles') {
1.351     raeburn  4158:             &set_custom_role($r,$context,$brcrum);
1.190     raeburn  4159:         } else {
1.351     raeburn  4160:             &custom_role_editor($r,$brcrum);
1.190     raeburn  4161:         }
1.207     raeburn  4162:     } elsif (($env{'form.action'} eq 'listusers') && 
                   4163:              ($permission->{'view'} || $permission->{'cusr'})) {
1.202     raeburn  4164:         if ($env{'form.phase'} eq 'bulkchange') {
1.351     raeburn  4165:             push(@{$brcrum},
                   4166:                     {href => '/adm/createuser?action=listusers',
                   4167:                      text => "List Users"},
                   4168:                     {href => "/adm/createuser",
                   4169:                      text => "Result",
                   4170:                      help => 'Course_View_Class_List'});
                   4171:             $bread_crumbs_component = 'Update Users';
                   4172:             $args = {bread_crumbs           => $brcrum,
                   4173:                      bread_crumbs_component => $bread_crumbs_component};
                   4174:             $r->print(&header(undef,$args));
1.202     raeburn  4175:             my $setting = $env{'form.roletype'};
                   4176:             my $choice = $env{'form.bulkaction'};
                   4177:             if ($permission->{'cusr'}) {
1.336     raeburn  4178:                 &Apache::lonuserutils::update_user_list($r,$context,$setting,$choice,$crstype);
1.221     raeburn  4179:             } else {
                   4180:                 $r->print(&mt('You are not authorized to make bulk changes to user roles'));
1.223     raeburn  4181:                 $r->print('<p><a href="/adm/createuser?action=listusers">'.&mt('Display User Lists').'</a>');
1.202     raeburn  4182:             }
                   4183:         } else {
1.351     raeburn  4184:             push(@{$brcrum},
                   4185:                     {href => '/adm/createuser?action=listusers',
                   4186:                      text => "List Users",
                   4187:                      help => 'Course_View_Class_List'});
                   4188:             $bread_crumbs_component = 'List Users';
                   4189:             $args = {bread_crumbs           => $brcrum,
                   4190:                      bread_crumbs_component => $bread_crumbs_component};
1.202     raeburn  4191:             my ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles);
                   4192:             my $formname = 'studentform';
1.321     raeburn  4193:             if (($context eq 'domain') && (($env{'form.roletype'} eq 'course') ||
                   4194:                 ($env{'form.roletype'} eq 'community'))) {
                   4195:                 if ($env{'form.roletype'} eq 'course') {
                   4196:                     ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles) = 
                   4197:                         &Apache::lonuserutils::courses_selector($env{'request.role.domain'},
                   4198:                                                                 $formname);
                   4199:                 } elsif ($env{'form.roletype'} eq 'community') {
                   4200:                     $cb_jscript = 
                   4201:                         &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
                   4202:                     my %elements = (
                   4203:                                       coursepick => 'radio',
                   4204:                                       coursetotal => 'text',
                   4205:                                       courselist => 'text',
                   4206:                                    );
                   4207:                     $jscript = &Apache::lonhtmlcommon::set_form_elements(\%elements);
                   4208:                 }
1.202     raeburn  4209:                 $jscript .= &verify_user_display();
                   4210:                 my $js = &add_script($jscript).$cb_jscript;
                   4211:                 my $loadcode = 
                   4212:                     &Apache::lonuserutils::course_selector_loadcode($formname);
                   4213:                 if ($loadcode ne '') {
1.351     raeburn  4214:                     $args->{add_entries} = {onload => $loadcode};
1.202     raeburn  4215:                 }
1.351     raeburn  4216:                 $r->print(&header($js,$args));
1.191     raeburn  4217:             } else {
1.351     raeburn  4218:                 $r->print(&header(&add_script(&verify_user_display()),$args));
1.191     raeburn  4219:             }
1.202     raeburn  4220:             &Apache::lonuserutils::print_userlist($r,undef,$permission,$context,
                   4221:                          $formname,$totcodes,$codetitles,$idlist,$idlist_titles);
1.191     raeburn  4222:         }
1.213     raeburn  4223:     } elsif ($env{'form.action'} eq 'drop' && $permission->{'cusr'}) {
1.318     raeburn  4224:         my $brtext;
                   4225:         if ($crstype eq 'Community') {
                   4226:             $brtext = 'Drop Members';
                   4227:         } else {
                   4228:             $brtext = 'Drop Students';
                   4229:         }
1.351     raeburn  4230:         push(@{$brcrum},
                   4231:                 {href => '/adm/createuser?action=drop',
                   4232:                  text => $brtext,
                   4233:                  help => 'Course_Drop_Student'});
                   4234:         if ($env{'form.state'} eq 'done') {
                   4235:             push(@{$brcrum},
                   4236:                      {href=>'/adm/createuser?action=drop',
                   4237:                       text=>"Result"});
                   4238:         }
                   4239:         $bread_crumbs_component = $brtext;
                   4240:         $args = {bread_crumbs           => $brcrum,
                   4241:                  bread_crumbs_component => $bread_crumbs_component}; 
                   4242:         $r->print(&header(undef,$args));
1.213     raeburn  4243:         if (!exists($env{'form.state'})) {
1.318     raeburn  4244:             &Apache::lonuserutils::print_drop_menu($r,$context,$permission,$crstype);
1.213     raeburn  4245:         } elsif ($env{'form.state'} eq 'done') {
                   4246:             &Apache::lonuserutils::update_user_list($r,$context,undef,
                   4247:                                                     $env{'form.action'});
                   4248:         }
1.202     raeburn  4249:     } elsif ($env{'form.action'} eq 'dateselect') {
                   4250:         if ($permission->{'cusr'}) {
1.351     raeburn  4251:             $r->print(&header(undef,{'no_nav_bar' => 1}).
1.221     raeburn  4252:                       &Apache::lonuserutils::date_section_selector($context,
1.351     raeburn  4253:                                                                    $permission,$crstype));
1.202     raeburn  4254:         } else {
1.351     raeburn  4255:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   4256:                      '<span class="LC_error">'.&mt('You do not have permission to modify dates or sections for users').'</span>'); 
1.202     raeburn  4257:         }
1.237     raeburn  4258:     } elsif ($env{'form.action'} eq 'selfenroll') {
1.351     raeburn  4259:         push(@{$brcrum},
                   4260:                 {href => '/adm/createuser?action=selfenroll',
                   4261:                  text => "Configure Self-enrollment",
                   4262:                  help => 'Course_Self_Enrollment'});
1.237     raeburn  4263:         if (!exists($env{'form.state'})) {
1.351     raeburn  4264:             $args = { bread_crumbs           => $brcrum,
                   4265:                       bread_crumbs_component => 'Configure Self-enrollment'};
                   4266:             $r->print(&header(undef,$args));
1.241     raeburn  4267:             $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
1.237     raeburn  4268:             &print_selfenroll_menu($r,$context,$permission);
                   4269:         } elsif ($env{'form.state'} eq 'done') {
1.351     raeburn  4270:             push (@{$brcrum},
                   4271:                       {href=>'/adm/createuser?action=selfenroll',
                   4272:                        text=>"Result"});
                   4273:             $args = { bread_crumbs           => $brcrum,
                   4274:                       bread_crumbs_component => 'Self-enrollment result'};
                   4275:             $r->print(&header(undef,$args));
1.241     raeburn  4276:             $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
                   4277:             &update_selfenroll_config($r,$context,$permission);
1.237     raeburn  4278:         }
1.277     raeburn  4279:     } elsif ($env{'form.action'} eq 'selfenrollqueue') {
1.351     raeburn  4280:         push(@{$brcrum},
                   4281:                  {href => '/adm/createuser?action=selfenrollqueue',
                   4282:                   text => 'Enrollment requests',
                   4283:                   help => 'Course_Self_Enrollment'});
                   4284:         $bread_crumbs_component = 'Enrollment requests';
                   4285:         if ($env{'form.state'} eq 'done') {
                   4286:             push(@{$brcrum},
                   4287:                      {href => '/adm/createuser?action=selfenrollqueue',
                   4288:                       text => 'Result',
                   4289:                       help => 'Course_Self_Enrollment'});
                   4290:             $bread_crumbs_component = 'Enrollment result';
                   4291:         }
                   4292:         $args = { bread_crumbs           => $brcrum,
                   4293:                   bread_crumbs_component => $bread_crumbs_component};
                   4294:         $r->print(&header(undef,$args));
1.277     raeburn  4295:         my $cid = $env{'request.course.id'};
                   4296:         my $cdom = $env{'course.'.$cid.'.domain'};
                   4297:         my $cnum = $env{'course.'.$cid.'.num'};
1.307     raeburn  4298:         my $coursedesc = $env{'course.'.$cid.'.description'};
1.277     raeburn  4299:         if (!exists($env{'form.state'})) {
                   4300:             $r->print('<h3>'.&mt('Pending enrollment requests').'</h3>'."\n");
1.307     raeburn  4301:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests($context,
                   4302:                                                                        $cdom,$cnum));
1.277     raeburn  4303:         } elsif ($env{'form.state'} eq 'done') {
                   4304:             $r->print('<h3>'.&mt('Enrollment request processing').'</h3>'."\n");
1.307     raeburn  4305:             $r->print(&Apache::loncoursequeueadmin::update_request_queue($context,
                   4306:                           $cdom,$cnum,$coursedesc));
1.277     raeburn  4307:         }
1.239     raeburn  4308:     } elsif ($env{'form.action'} eq 'changelogs') {
1.351     raeburn  4309:         push (@{$brcrum},
                   4310:                  {href => '/adm/createuser?action=changelogs',
                   4311:                   text => 'User Management Logs',
                   4312:                   help => 'Course_User_Logs'});
                   4313:         $bread_crumbs_component = 'User Changes';
                   4314:         $args = { bread_crumbs           => $brcrum,
                   4315:                   bread_crumbs_component => $bread_crumbs_component};
                   4316:         $r->print(&header(undef,$args));
                   4317:         &print_userchangelogs_display($r,$context,$permission);
1.190     raeburn  4318:     } else {
1.351     raeburn  4319:         $bread_crumbs_component = 'User Management';
                   4320:         $args = { bread_crumbs           => $brcrum,
                   4321:                   bread_crumbs_component => $bread_crumbs_component};
                   4322:         $r->print(&header(undef,$args));
1.318     raeburn  4323:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  4324:     }
1.351     raeburn  4325:     $r->print(&Apache::loncommon::end_page());
1.190     raeburn  4326:     return OK;
                   4327: }
                   4328: 
                   4329: sub header {
1.351     raeburn  4330:     my ($jscript,$args) = @_;
1.190     raeburn  4331:     my $start_page;
1.351     raeburn  4332:     if (ref($args) eq 'HASH') {
                   4333:         $start_page=&Apache::loncommon::start_page('User Management',$jscript,$args);
1.190     raeburn  4334:     } else {
1.351     raeburn  4335:         $start_page=&Apache::loncommon::start_page('User Management',$jscript);
1.190     raeburn  4336:     }
                   4337:     return $start_page;
                   4338: }
1.2       www      4339: 
1.191     raeburn  4340: sub add_script {
                   4341:     my ($js) = @_;
1.301     bisitz   4342:     return '<script type="text/javascript">'."\n"
                   4343:           .'// <![CDATA['."\n"
                   4344:           .$js."\n"
                   4345:           .'// ]]>'."\n"
                   4346:           .'</script>'."\n";
1.191     raeburn  4347: }
                   4348: 
1.202     raeburn  4349: sub verify_user_display {
                   4350:     my $output = <<"END";
                   4351: 
                   4352: function display_update() {
                   4353:     document.studentform.action.value = 'listusers';
                   4354:     document.studentform.phase.value = 'display';
                   4355:     document.studentform.submit();
                   4356: }
                   4357: 
                   4358: END
                   4359:     return $output;
                   4360: 
                   4361: }
                   4362: 
1.190     raeburn  4363: ###############################################################
                   4364: ###############################################################
                   4365: #  Menu Phase One
                   4366: sub print_main_menu {
1.318     raeburn  4367:     my ($permission,$context,$crstype) = @_;
                   4368:     my $linkcontext = $context;
                   4369:     my $stuterm = lc(&Apache::lonnet::plaintext('st',$crstype));
                   4370:     if (($context eq 'course') && ($crstype eq 'Community')) {
                   4371:         $linkcontext = lc($crstype);
                   4372:         $stuterm = 'Members';
                   4373:     }
1.208     raeburn  4374:     my %links = (
1.298     droeschl 4375:                 domain => {
                   4376:                             upload     => 'Upload a File of Users',
                   4377:                             singleuser => 'Add/Modify a User',
                   4378:                             listusers  => 'Manage Users',
                   4379:                             },
                   4380:                 author => {
                   4381:                             upload     => 'Upload a File of Co-authors',
                   4382:                             singleuser => 'Add/Modify a Co-author',
                   4383:                             listusers  => 'Manage Co-authors',
                   4384:                             },
                   4385:                 course => {
                   4386:                             upload     => 'Upload a File of Course Users',
                   4387:                             singleuser => 'Add/Modify a Course User',
1.354     www      4388:                             listusers  => 'List and Modify Multiple Course Users',
1.298     droeschl 4389:                             },
1.318     raeburn  4390:                 community => {
                   4391:                             upload     => 'Upload a File of Community Users',
                   4392:                             singleuser => 'Add/Modify a Community User',
1.354     www      4393:                             listusers  => 'List and Modify Multiple Community Users',
1.318     raeburn  4394:                            },
                   4395:                 );
                   4396:      my %linktitles = (
                   4397:                 domain => {
                   4398:                             singleuser => 'Add a user to the domain, and/or a course or community in the domain.',
                   4399:                             listusers  => 'Show and manage users in this domain.',
                   4400:                             },
                   4401:                 author => {
                   4402:                             singleuser => 'Add a user with a co- or assistant author role.',
                   4403:                             listusers  => 'Show and manage co- or assistant authors.',
                   4404:                             },
                   4405:                 course => {
                   4406:                             singleuser => 'Add a user with a certain role to this course.',
                   4407:                             listusers  => 'Show and manage users in this course.',
                   4408:                             },
                   4409:                 community => {
                   4410:                             singleuser => 'Add a user with a certain role to this community.',
                   4411:                             listusers  => 'Show and manage users in this community.',
                   4412:                            },
1.298     droeschl 4413:                 );
                   4414:   my @menu = ( {categorytitle => 'Single Users', 
                   4415:          items =>
                   4416:          [
                   4417:             {
1.318     raeburn  4418:              linktext => $links{$linkcontext}{'singleuser'},
1.298     droeschl 4419:              icon => 'edit-redo.png',
                   4420:              #help => 'Course_Change_Privileges',
                   4421:              url => '/adm/createuser?action=singleuser',
                   4422:              permission => $permission->{'cusr'},
1.318     raeburn  4423:              linktitle => $linktitles{$linkcontext}{'singleuser'},
1.298     droeschl 4424:             },
                   4425:          ]},
                   4426: 
                   4427:          {categorytitle => 'Multiple Users',
                   4428:          items => 
                   4429:          [
                   4430:             {
1.318     raeburn  4431:              linktext => $links{$linkcontext}{'upload'},
1.340     wenzelju 4432:              icon => 'uplusr.png',
1.298     droeschl 4433:              #help => 'Course_Create_Class_List',
                   4434:              url => '/adm/createuser?action=upload',
                   4435:              permission => $permission->{'cusr'},
                   4436:              linktitle => 'Upload a CSV or a text file containing users.',
                   4437:             },
                   4438:             {
1.318     raeburn  4439:              linktext => $links{$linkcontext}{'listusers'},
1.340     wenzelju 4440:              icon => 'mngcu.png',
1.298     droeschl 4441:              #help => 'Course_View_Class_List',
                   4442:              url => '/adm/createuser?action=listusers',
                   4443:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.318     raeburn  4444:              linktitle => $linktitles{$linkcontext}{'listusers'}, 
1.298     droeschl 4445:             },
                   4446: 
                   4447:          ]},
                   4448: 
                   4449:          {categorytitle => 'Administration',
                   4450:          items => [ ]},
                   4451:        );
                   4452:             
1.265     mielkec  4453:     if ($context eq 'domain'){
1.298     droeschl 4454:         
                   4455:         push(@{ $menu[2]->{items} }, #Category: Administration
                   4456:             {
                   4457:              linktext => 'Custom Roles',
                   4458:              icon => 'emblem-photos.png',
                   4459:              #help => 'Course_Editing_Custom_Roles',
                   4460:              url => '/adm/createuser?action=custom',
                   4461:              permission => $permission->{'custom'},
                   4462:              linktitle => 'Configure a custom role.',
                   4463:             },
                   4464:         );
                   4465:         
1.265     mielkec  4466:     }elsif ($context eq 'course'){
1.298     droeschl 4467:         my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity();
1.318     raeburn  4468: 
                   4469:         my %linktext = (
                   4470:                          'Course'    => {
                   4471:                                           single => 'Add/Modify a Student', 
                   4472:                                           drop   => 'Drop Students',
                   4473:                                           groups => 'Course Groups',
                   4474:                                         },
                   4475:                          'Community' => {
                   4476:                                           single => 'Add/Modify a Member', 
                   4477:                                           drop   => 'Drop Members',
                   4478:                                           groups => 'Community Groups',
                   4479:                                         },
                   4480:                        );
                   4481: 
                   4482:         my %linktitle = (
                   4483:             'Course' => {
                   4484:                   single => 'Add a user with the role of student to this course',
                   4485:                   drop   => 'Remove a student from this course.',
                   4486:                   groups => 'Manage course groups',
                   4487:                         },
                   4488:             'Community' => {
                   4489:                   single => 'Add a user with the role of member to this community',
                   4490:                   drop   => 'Remove a member from this community.',
                   4491:                   groups => 'Manage community groups',
                   4492:                            },
                   4493:         );
                   4494: 
1.298     droeschl 4495:         push(@{ $menu[0]->{items} }, #Category: Single Users
                   4496:             {   
1.318     raeburn  4497:              linktext => $linktext{$crstype}{'single'},
1.298     droeschl 4498:              #help => 'Course_Add_Student',
                   4499:              icon => 'list-add.png',
                   4500:              url => '/adm/createuser?action=singlestudent',
                   4501:              permission => $permission->{'cusr'},
1.318     raeburn  4502:              linktitle => $linktitle{$crstype}{'single'},
1.298     droeschl 4503:             },
                   4504:         );
                   4505:         
                   4506:         push(@{ $menu[1]->{items} }, #Category: Multiple Users 
                   4507:             {
1.318     raeburn  4508:              linktext => $linktext{$crstype}{'drop'},
1.298     droeschl 4509:              icon => 'edit-undo.png',
                   4510:              #help => 'Course_Drop_Student',
                   4511:              url => '/adm/createuser?action=drop',
                   4512:              permission => $permission->{'cusr'},
1.318     raeburn  4513:              linktitle => $linktitle{$crstype}{'drop'},
1.298     droeschl 4514:             },
                   4515:         );
                   4516:         push(@{ $menu[2]->{items} }, #Category: Administration
                   4517:             {    
                   4518:              linktext => 'Custom Roles',
                   4519:              icon => 'emblem-photos.png',
                   4520:              #help => 'Course_Editing_Custom_Roles',
                   4521:              url => '/adm/createuser?action=custom',
                   4522:              permission => $permission->{'custom'},
                   4523:              linktitle => 'Configure a custom role.',
                   4524:             },
                   4525:             {
1.318     raeburn  4526:              linktext => $linktext{$crstype}{'groups'},
1.333     wenzelju 4527:              icon => 'grps.png',
1.298     droeschl 4528:              #help => 'Course_Manage_Group',
                   4529:              url => '/adm/coursegroups?refpage=cusr',
                   4530:              permission => $permission->{'grp_manage'},
1.318     raeburn  4531:              linktitle => $linktitle{$crstype}{'groups'},
1.298     droeschl 4532:             },
                   4533:             {
1.328     wenzelju 4534:              linktext => 'Change Log',
1.298     droeschl 4535:              icon => 'document-properties.png',
                   4536:              #help => 'Course_User_Logs',
                   4537:              url => '/adm/createuser?action=changelogs',
                   4538:              permission => $permission->{'cusr'},
                   4539:              linktitle => 'View change log.',
                   4540:             },
                   4541:         );
1.277     raeburn  4542:         if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'}) {
1.298     droeschl 4543:             push(@{ $menu[2]->{items} },
                   4544:                     {   
                   4545:                      linktext => 'Enrollment Requests',
                   4546:                      icon => 'selfenrl-queue.png',
                   4547:                      #help => 'Course_Approve_Selfenroll',
                   4548:                      url => '/adm/createuser?action=selfenrollqueue',
                   4549:                      permission => $permission->{'cusr'},
                   4550:                      linktitle =>'Approve or reject enrollment requests.',
                   4551:                     },
                   4552:             );
1.277     raeburn  4553:         }
1.298     droeschl 4554:         
1.265     mielkec  4555:         if (!exists($permission->{'cusr_section'})){
1.320     raeburn  4556:             if ($crstype ne 'Community') {
                   4557:                 push(@{ $menu[2]->{items} },
                   4558:                     {
                   4559:                      linktext => 'Automated Enrollment',
                   4560:                      icon => 'roles.png',
                   4561:                      #help => 'Course_Automated_Enrollment',
                   4562:                      permission => (&Apache::lonnet::auto_run($cnum,$cdom)
                   4563:                                          && $permission->{'cusr'}),
                   4564:                      url  => '/adm/populate',
                   4565:                      linktitle => 'Automated enrollment manager.',
                   4566:                     }
                   4567:                 );
                   4568:             }
                   4569:             push(@{ $menu[2]->{items} }, 
1.298     droeschl 4570:                 {
                   4571:                  linktext => 'User Self-Enrollment',
1.342     wenzelju 4572:                  icon => 'self_enroll.png',
1.298     droeschl 4573:                  #help => 'Course_Self_Enrollment',
                   4574:                  url => '/adm/createuser?action=selfenroll',
                   4575:                  permission => $permission->{'cusr'},
1.317     bisitz   4576:                  linktitle => 'Configure user self-enrollment.',
1.298     droeschl 4577:                 },
                   4578:             );
                   4579:         }
1.265     mielkec  4580:     };
                   4581: return Apache::lonhtmlcommon::generate_menu(@menu);
1.250     raeburn  4582: #               { text => 'View Log-in History',
                   4583: #                 help => 'Course_User_Logins',
                   4584: #                 action => 'logins',
                   4585: #                 permission => $permission->{'cusr'},
                   4586: #               });
1.190     raeburn  4587: }
                   4588: 
1.189     albertel 4589: sub restore_prev_selections {
                   4590:     my %saveable_parameters = ('srchby'   => 'scalar',
                   4591: 			       'srchin'   => 'scalar',
                   4592: 			       'srchtype' => 'scalar',
                   4593: 			       );
                   4594:     &Apache::loncommon::store_settings('user','user_picker',
                   4595: 				       \%saveable_parameters);
                   4596:     &Apache::loncommon::restore_settings('user','user_picker',
                   4597: 					 \%saveable_parameters);
                   4598: }
                   4599: 
1.237     raeburn  4600: sub print_selfenroll_menu {
                   4601:     my ($r,$context,$permission) = @_;
1.322     raeburn  4602:     my $crstype = &Apache::loncommon::course_type();
1.237     raeburn  4603:     my $formname = 'enrollstudent';
                   4604:     my $nolink = 1;
                   4605:     my ($row,$lt) = &get_selfenroll_titles();
                   4606:     my $groupslist = &Apache::lonuserutils::get_groupslist();
                   4607:     my $setsec_js = 
                   4608:         &Apache::lonuserutils::setsections_javascript($formname,$groupslist);
1.249     raeburn  4609:     my %alerts = &Apache::lonlocal::texthash(
                   4610:         acto => 'Activation of self-enrollment was selected for the following domain(s)',
                   4611:         butn => 'but no user types have been checked.',
                   4612:         wilf => "Please uncheck 'activate' or check at least one type.",
                   4613:     );
                   4614:     my $selfenroll_js = <<"ENDSCRIPT";
                   4615: function update_types(caller,num) {
                   4616:     var delidx = getIndexByName('selfenroll_delete');
                   4617:     var actidx = getIndexByName('selfenroll_activate');
                   4618:     if (caller == 'selfenroll_all') {
                   4619:         var selall;
                   4620:         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   4621:             if (document.$formname.selfenroll_all[i].checked) {
                   4622:                 selall = document.$formname.selfenroll_all[i].value;
                   4623:             }
                   4624:         }
                   4625:         if (selall == 1) {
                   4626:             if (delidx != -1) {
                   4627:                 if (document.$formname.selfenroll_delete.length) {
                   4628:                     for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   4629:                         document.$formname.selfenroll_delete[j].checked = true;
                   4630:                     }
                   4631:                 } else {
                   4632:                     document.$formname.elements[delidx].checked = true;
                   4633:                 }
                   4634:             }
                   4635:             if (actidx != -1) {
                   4636:                 if (document.$formname.selfenroll_activate.length) {
                   4637:                     for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   4638:                         document.$formname.selfenroll_activate[j].checked = false;
                   4639:                     }
                   4640:                 } else {
                   4641:                     document.$formname.elements[actidx].checked = false;
                   4642:                 }
                   4643:             }
                   4644:             document.$formname.selfenroll_newdom.selectedIndex = 0; 
                   4645:         }
                   4646:     }
                   4647:     if (caller == 'selfenroll_activate') {
                   4648:         if (document.$formname.selfenroll_activate.length) {
                   4649:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   4650:                 if (document.$formname.selfenroll_activate[j].value == num) {
                   4651:                     if (document.$formname.selfenroll_activate[j].checked) {
                   4652:                         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   4653:                             if (document.$formname.selfenroll_all[i].value == '1') {
                   4654:                                 document.$formname.selfenroll_all[i].checked = false;
                   4655:                             }
                   4656:                             if (document.$formname.selfenroll_all[i].value == '0') {
                   4657:                                 document.$formname.selfenroll_all[i].checked = true;
                   4658:                             }
                   4659:                         }
                   4660:                     }
                   4661:                 }
                   4662:             }
                   4663:         } else {
                   4664:             for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   4665:                 if (document.$formname.selfenroll_all[i].value == '1') {
                   4666:                     document.$formname.selfenroll_all[i].checked = false;
                   4667:                 }
                   4668:                 if (document.$formname.selfenroll_all[i].value == '0') {
                   4669:                     document.$formname.selfenroll_all[i].checked = true;
                   4670:                 }
                   4671:             }
                   4672:         }
                   4673:     }
                   4674:     if (caller == 'selfenroll_delete') {
                   4675:         if (document.$formname.selfenroll_delete.length) {
                   4676:             for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   4677:                 if (document.$formname.selfenroll_delete[j].value == num) {
                   4678:                     if (document.$formname.selfenroll_delete[j].checked) {
                   4679:                         var delindex = getIndexByName('selfenroll_types_'+num);
                   4680:                         if (delindex != -1) { 
                   4681:                             if (document.$formname.elements[delindex].length) {
                   4682:                                 for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   4683:                                     document.$formname.elements[delindex][k].checked = false;
                   4684:                                 }
                   4685:                             } else {
                   4686:                                 document.$formname.elements[delindex].checked = false;
                   4687:                             }
                   4688:                         }
                   4689:                     }
                   4690:                 }
                   4691:             }
                   4692:         } else {
                   4693:             if (document.$formname.selfenroll_delete.checked) {
                   4694:                 var delindex = getIndexByName('selfenroll_types_'+num);
                   4695:                 if (delindex != -1) {
                   4696:                     if (document.$formname.elements[delindex].length) {
                   4697:                         for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   4698:                             document.$formname.elements[delindex][k].checked = false;
                   4699:                         }
                   4700:                     } else {
                   4701:                         document.$formname.elements[delindex].checked = false;
                   4702:                     }
                   4703:                 }
                   4704:             }
                   4705:         }
                   4706:     }
                   4707:     return;
                   4708: }
                   4709: 
                   4710: function validate_types(form) {
                   4711:     var needaction = new Array();
                   4712:     var countfail = 0;
                   4713:     var actidx = getIndexByName('selfenroll_activate');
                   4714:     if (actidx != -1) {
                   4715:         if (document.$formname.selfenroll_activate.length) {
                   4716:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   4717:                 var num = document.$formname.selfenroll_activate[j].value;
                   4718:                 if (document.$formname.selfenroll_activate[j].checked) {
                   4719:                     countfail = check_types(num,countfail,needaction)
                   4720:                 }
                   4721:             }
                   4722:         } else {
                   4723:             if (document.$formname.selfenroll_activate.checked) {
                   4724:                 var num = document.enrollstudent.selfenroll_activate.value;
                   4725:                 countfail = check_types(num,countfail,needaction)
                   4726:             }
                   4727:         }
                   4728:     }
                   4729:     if (countfail > 0) {
                   4730:         var msg = "$alerts{'acto'}\\n";
                   4731:         var loopend = needaction.length -1;
                   4732:         if (loopend > 0) {
                   4733:             for (var m=0; m<loopend; m++) {
                   4734:                 msg += needaction[m]+", ";
                   4735:             }
                   4736:         }
                   4737:         msg += needaction[loopend]+"\\n$alerts{'butn'}\\n$alerts{'wilf'}";
                   4738:         alert(msg);
                   4739:         return; 
                   4740:     }
                   4741:     setSections(form);
                   4742: }
                   4743: 
                   4744: function check_types(num,countfail,needaction) {
                   4745:     var typeidx = getIndexByName('selfenroll_types_'+num);
                   4746:     var count = 0;
                   4747:     if (typeidx != -1) {
                   4748:         if (document.$formname.elements[typeidx].length) {
                   4749:             for (var k=0; k<document.$formname.elements[typeidx].length; k++) {
                   4750:                 if (document.$formname.elements[typeidx][k].checked) {
                   4751:                     count ++;
                   4752:                 }
                   4753:             }
                   4754:         } else {
                   4755:             if (document.$formname.elements[typeidx].checked) {
                   4756:                 count ++;
                   4757:             }
                   4758:         }
                   4759:         if (count == 0) {
                   4760:             var domidx = getIndexByName('selfenroll_dom_'+num);
                   4761:             if (domidx != -1) {
                   4762:                 var domname = document.$formname.elements[domidx].value;
                   4763:                 needaction[countfail] = domname;
                   4764:                 countfail ++;
                   4765:             }
                   4766:         }
                   4767:     }
                   4768:     return countfail;
                   4769: }
                   4770: 
                   4771: function getIndexByName(item) {
                   4772:     for (var i=0;i<document.$formname.elements.length;i++) {
                   4773:         if (document.$formname.elements[i].name == item) {
                   4774:             return i;
                   4775:         }
                   4776:     }
                   4777:     return -1;
                   4778: }
                   4779: ENDSCRIPT
1.256     raeburn  4780:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4781:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   4782: 
1.237     raeburn  4783:     my $output = '<script type="text/javascript">'."\n".
1.301     bisitz   4784:                  '// <![CDATA['."\n".
1.249     raeburn  4785:                  $setsec_js."\n".$selfenroll_js."\n".
1.301     bisitz   4786:                  '// ]]>'."\n".
1.237     raeburn  4787:                  '</script>'."\n".
1.256     raeburn  4788:                  '<h3>'.$lt->{'selfenroll'}.'</h3>'."\n";
                   4789:     my ($visible,$cansetvis,$vismsgs,$visactions) = &visible_in_cat($cdom,$cnum);
                   4790:     if (ref($visactions) eq 'HASH') {
                   4791:         if ($visible) {
1.283     bisitz   4792:             $output .= '<p class="LC_info">'.$visactions->{'vis'}.'</p>';
1.256     raeburn  4793:         } else {
1.283     bisitz   4794:             $output .= '<p class="LC_warning">'.$visactions->{'miss'}.'</p>'
                   4795:                       .$visactions->{'yous'}.
1.256     raeburn  4796:                        '<p>'.$visactions->{'gen'}.'<br />'.$visactions->{'coca'};
                   4797:             if (ref($vismsgs) eq 'ARRAY') {
                   4798:                 $output .= '<br />'.$visactions->{'make'}.'<ul>';
                   4799:                 foreach my $item (@{$vismsgs}) {
                   4800:                     $output .= '<li>'.$visactions->{$item}.'</li>';
                   4801:                 }
                   4802:                 $output .= '</ul>';
                   4803:             }
                   4804:             $output .= '</p>';
                   4805:         }
                   4806:     }
                   4807:     $output .= '<form name="'.$formname.'" method="post" action="/adm/createuser">'."\n".
                   4808:                &Apache::lonhtmlcommon::start_pick_box();
1.237     raeburn  4809:     if (ref($row) eq 'ARRAY') {
                   4810:         foreach my $item (@{$row}) {
                   4811:             my $title = $item; 
                   4812:             if (ref($lt) eq 'HASH') {
                   4813:                 $title = $lt->{$item};
                   4814:             }
1.297     bisitz   4815:             $output .= &Apache::lonhtmlcommon::row_title($title);
1.237     raeburn  4816:             if ($item eq 'types') {
                   4817:                 my $curr_types = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_types'};
1.241     raeburn  4818:                 my $showdomdesc = 1;
                   4819:                 my $includeempty = 1;
                   4820:                 my $num = 0;
                   4821:                 $output .= &Apache::loncommon::start_data_table().
                   4822:                            &Apache::loncommon::start_data_table_row()
                   4823:                            .'<td colspan="2"><span class="LC_nobreak"><label>'
                   4824:                            .&mt('Any user in any domain:')
                   4825:                            .'&nbsp;<input type="radio" name="selfenroll_all" value="1" ';
                   4826:                 if ($curr_types eq '*') {
                   4827:                     $output .= ' checked="checked" '; 
                   4828:                 }
1.249     raeburn  4829:                 $output .= 'onchange="javascript:update_types('.
                   4830:                            "'selfenroll_all'".');" />'.&mt('Yes').'</label>'.
                   4831:                            '&nbsp;&nbsp;<input type="radio" name="selfenroll_all" value="0" ';
1.241     raeburn  4832:                 if ($curr_types ne '*') {
                   4833:                     $output .= ' checked="checked" ';
                   4834:                 }
1.249     raeburn  4835:                 $output .= ' onchange="javascript:update_types('.
                   4836:                            "'selfenroll_all'".');"/>'.&mt('No').'</label></td>'.
                   4837:                            &Apache::loncommon::end_data_table_row().
                   4838:                            &Apache::loncommon::end_data_table().
                   4839:                            &mt('Or').'<br />'.
                   4840:                            &Apache::loncommon::start_data_table();
1.241     raeburn  4841:                 my %currdoms;
1.249     raeburn  4842:                 if ($curr_types eq '') {
1.241     raeburn  4843:                     $output .= &new_selfenroll_dom_row($cdom,'0');
                   4844:                 } elsif ($curr_types ne '*') {
                   4845:                     my @entries = split(/;/,$curr_types);
                   4846:                     if (@entries > 0) {
                   4847:                         foreach my $entry (@entries) {
                   4848:                             my ($currdom,$typestr) = split(/:/,$entry);
                   4849:                             $currdoms{$currdom} = 1;
                   4850:                             my $domdesc = &Apache::lonnet::domain($currdom);
1.249     raeburn  4851:                             my @currinsttypes = split(',',$typestr);
1.241     raeburn  4852:                             $output .= &Apache::loncommon::start_data_table_row()
                   4853:                                        .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'<b>'
                   4854:                                        .'&nbsp;'.$domdesc.' ('.$currdom.')'
                   4855:                                        .'</b><input type="hidden" name="selfenroll_dom_'.$num
                   4856:                                        .'" value="'.$currdom.'" /></span><br />'
                   4857:                                        .'<span class="LC_nobreak"><label><input type="checkbox" '
1.249     raeburn  4858:                                        .'name="selfenroll_delete" value="'.$num.'" onchange="javascript:update_types('."'selfenroll_delete','$num'".');" />'
1.241     raeburn  4859:                                        .&mt('Delete').'</label></span></td>';
1.249     raeburn  4860:                             $output .= '<td valign="top">&nbsp;&nbsp;'.&mt('User types:').'<br />'
1.241     raeburn  4861:                                        .&selfenroll_inst_types($num,$currdom,\@currinsttypes).'</td>'
                   4862:                                        .&Apache::loncommon::end_data_table_row();
                   4863:                             $num ++;
                   4864:                         }
                   4865:                     }
                   4866:                 }
1.249     raeburn  4867:                 my $add_domtitle = &mt('Users in additional domain:');
1.241     raeburn  4868:                 if ($curr_types eq '*') { 
1.249     raeburn  4869:                     $add_domtitle = &mt('Users in specific domain:');
1.241     raeburn  4870:                 } elsif ($curr_types eq '') {
1.249     raeburn  4871:                     $add_domtitle = &mt('Users in other domain:');
1.241     raeburn  4872:                 }
                   4873:                 $output .= &Apache::loncommon::start_data_table_row()
                   4874:                            .'<td colspan="2"><span class="LC_nobreak">'.$add_domtitle.'</span><br />'
                   4875:                            .&Apache::loncommon::select_dom_form('','selfenroll_newdom',
                   4876:                                                                 $includeempty,$showdomdesc)
                   4877:                            .'<input type="hidden" name="selfenroll_types_total" value="'.$num.'" />'
                   4878:                            .'</td>'.&Apache::loncommon::end_data_table_row()
                   4879:                            .&Apache::loncommon::end_data_table();
1.237     raeburn  4880:             } elsif ($item eq 'registered') {
                   4881:                 my ($regon,$regoff);
                   4882:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_registered'}) {
                   4883:                     $regon = ' checked="checked" ';
                   4884:                     $regoff = ' ';
                   4885:                 } else {
                   4886:                     $regon = ' ';
                   4887:                     $regoff = ' checked="checked" ';
                   4888:                 }
                   4889:                 $output .= '<label>'.
1.245     raeburn  4890:                            '<input type="radio" name="selfenroll_registered" value="1"'.$regon.'/>'.
1.244     bisitz   4891:                            &mt('Yes').'</label>&nbsp;&nbsp;<label>'.
1.245     raeburn  4892:                            '<input type="radio" name="selfenroll_registered" value="0"'.$regoff.'/>'.
1.244     bisitz   4893:                            &mt('No').'</label>';
1.237     raeburn  4894:             } elsif ($item eq 'enroll_dates') {
                   4895:                 my $starttime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_start_date'};
                   4896:                 my $endtime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_end_date'};
                   4897:                 if ($starttime eq '') {
                   4898:                     $starttime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_start_date'};
                   4899:                 }
                   4900:                 if ($endtime eq '') {
                   4901:                     $endtime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_end_date'};
                   4902:                 }
                   4903:                 my $startform =
                   4904:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_date',$starttime,
                   4905:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   4906:                 my $endform =
                   4907:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_date',$endtime,
                   4908:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   4909:                 $output .= &selfenroll_date_forms($startform,$endform);
                   4910:             } elsif ($item eq 'access_dates') {
                   4911:                 my $starttime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_start_access'};
                   4912:                 my $endtime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_end_access'};
                   4913:                 if ($starttime eq '') {
                   4914:                     $starttime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_start_date'};
                   4915:                 }
                   4916:                 if ($endtime eq '') {
                   4917:                     $endtime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_end_date'};
                   4918:                 }
                   4919:                 my $startform =
                   4920:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_access',$starttime,
                   4921:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   4922:                 my $endform =
                   4923:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_access',$endtime,
                   4924:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   4925:                 $output .= &selfenroll_date_forms($startform,$endform);
                   4926:             } elsif ($item eq 'section') {
                   4927:                 my $currsec = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_section'}; 
                   4928:                 my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   4929:                 my $newsecval;
                   4930:                 if ($currsec ne 'none' && $currsec ne '') {
                   4931:                     if (!defined($sections_count{$currsec})) {
                   4932:                         $newsecval = $currsec;
                   4933:                     }
                   4934:                 }
                   4935:                 my $sections_select = 
                   4936:                     &Apache::lonuserutils::course_sections(\%sections_count,'st',$currsec);
                   4937:                 $output .= '<table class="LC_createuser">'."\n".
                   4938:                            '<tr class="LC_section_row">'."\n".
                   4939:                            '<td align="center">'.&mt('Existing sections')."\n".
                   4940:                            '<br />'.$sections_select.'</td><td align="center">'.
                   4941:                            &mt('New section').'<br />'."\n".
                   4942:                            '<input type="text" name="newsec" size="15" value="'.$newsecval.'" />'."\n".
                   4943:                            '<input type="hidden" name="sections" value="" />'."\n".
                   4944:                            '<input type="hidden" name="state" value="done" />'."\n".
                   4945:                            '</td></tr></table>'."\n";
1.276     raeburn  4946:             } elsif ($item eq 'approval') {
                   4947:                 my ($appon,$appoff);
                   4948:                 my $cid = $env{'request.course.id'};
                   4949:                 my $currnotified = $env{'course.'.$cid.'.internal.selfenroll_notifylist'};
                   4950:                 if ($env{'course.'.$cid.'.internal.selfenroll_approval'}) {
                   4951:                     $appon = ' checked="checked" ';
                   4952:                     $appoff = ' ';
                   4953:                 } else {
                   4954:                     $appon = ' ';
                   4955:                     $appoff = ' checked="checked" ';
                   4956:                 }
                   4957:                 $output .= '<label>'.
                   4958:                            '<input type="radio" name="selfenroll_approval" value="1"'.$appon.'/>'.
                   4959:                            &mt('Yes').'</label>&nbsp;&nbsp;<label>'.
                   4960:                            '<input type="radio" name="selfenroll_approval" value="0"'.$appoff.'/>'.
                   4961:                            &mt('No').'</label>';
                   4962:                 my %advhash = &Apache::lonnet::get_course_adv_roles($cid,1);
                   4963:                 my (@ccs,%notified);
1.322     raeburn  4964:                 my $ccrole = 'cc';
                   4965:                 if ($crstype eq 'Community') {
                   4966:                     $ccrole = 'co';
                   4967:                 }
                   4968:                 if ($advhash{$ccrole}) {
                   4969:                     @ccs = split(/,/,$advhash{$ccrole});
1.276     raeburn  4970:                 }
                   4971:                 if ($currnotified) {
                   4972:                     foreach my $current (split(/,/,$currnotified)) {
                   4973:                         $notified{$current} = 1;
                   4974:                         if (!grep(/^\Q$current\E$/,@ccs)) {
                   4975:                             push(@ccs,$current);
                   4976:                         }
                   4977:                     }
                   4978:                 }
                   4979:                 if (@ccs) {
1.277     raeburn  4980:                     $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  4981:                                &Apache::loncommon::start_data_table_row();
                   4982:                     my $count = 0;
                   4983:                     my $numcols = 4;
                   4984:                     foreach my $cc (sort(@ccs)) {
                   4985:                         my $notifyon;
                   4986:                         my ($ccuname,$ccudom) = split(/:/,$cc);
                   4987:                         if ($notified{$cc}) {
                   4988:                             $notifyon = ' checked="checked" ';
                   4989:                         }
                   4990:                         if ($count && !$count%$numcols) {
                   4991:                             $output .= &Apache::loncommon::end_data_table_row().
                   4992:                                        &Apache::loncommon::start_data_table_row()
                   4993:                         }
                   4994:                         $output .= '<td><span class="LC_nobreak"><label>'.
                   4995:                                    '<input type="checkbox" name="selfenroll_notify"'.$notifyon.' value="'.$cc.'" />'.
                   4996:                                    &Apache::loncommon::plainname($ccuname,$ccudom).
                   4997:                                    '</label></span></td>';
1.343     raeburn  4998:                         $count ++;
1.276     raeburn  4999:                     }
                   5000:                     my $rem = $count%$numcols;
                   5001:                     if ($rem) {
                   5002:                         my $emptycols = $numcols - $rem;
                   5003:                         for (my $i=0; $i<$emptycols; $i++) { 
                   5004:                             $output .= '<td>&nbsp;</td>';
                   5005:                         }
                   5006:                     }
                   5007:                     $output .= &Apache::loncommon::end_data_table_row().
                   5008:                                &Apache::loncommon::end_data_table();
                   5009:                 }
                   5010:             } elsif ($item eq 'limit') {
                   5011:                 my ($crslimit,$selflimit,$nolimit);
                   5012:                 my $cid = $env{'request.course.id'};
                   5013:                 my $currlim = $env{'course.'.$cid.'.internal.selfenroll_limit'};
                   5014:                 my $currcap = $env{'course.'.$cid.'.internal.selfenroll_cap'};
1.343     raeburn  5015:                 $nolimit = ' checked="checked" ';
1.276     raeburn  5016:                 if ($currlim eq 'allstudents') {
                   5017:                     $crslimit = ' checked="checked" ';
                   5018:                     $selflimit = ' ';
                   5019:                     $nolimit = ' ';
                   5020:                 } elsif ($currlim eq 'selfenrolled') {
                   5021:                     $crslimit = ' ';
                   5022:                     $selflimit = ' checked="checked" ';
                   5023:                     $nolimit = ' '; 
                   5024:                 } else {
                   5025:                     $crslimit = ' ';
                   5026:                     $selflimit = ' ';
                   5027:                 }
                   5028:                 $output .= '<table><tr><td><label>'.
1.278     raeburn  5029:                            '<input type="radio" name="selfenroll_limit" value="none"'.$nolimit.'/>'.
1.276     raeburn  5030:                            &mt('No limit').'</label></td><td><label>'.
                   5031:                            '<input type="radio" name="selfenroll_limit" value="allstudents"'.$crslimit.'/>'.
                   5032:                            &mt('Limit by total students').'</label></td><td><label>'.
                   5033:                            '<input type="radio" name="selfenroll_limit" value="selfenrolled"'.$selflimit.'/>'.
                   5034:                            &mt('Limit by total self-enrolled students').
                   5035:                            '</td></tr><tr>'.
                   5036:                            '<td>&nbsp;</td><td colspan="2"><span class="LC_nobreak">'.
                   5037:                            ('&nbsp;'x3).&mt('Maximum number allowed: ').
                   5038:                            '<input type="text" name="selfenroll_cap" size = "5" value="'.$currcap.'" /></td></tr></table>';
1.237     raeburn  5039:             }
                   5040:             $output .= &Apache::lonhtmlcommon::row_closure(1);
                   5041:         }
                   5042:     }
                   5043:     $output .= &Apache::lonhtmlcommon::end_pick_box().
1.241     raeburn  5044:                '<br /><input type="button" name="selfenrollconf" value="'
1.282     schafran 5045:                .&mt('Save').'" onclick="validate_types(this.form);" />'
1.241     raeburn  5046:                .'<input type="hidden" name="action" value="selfenroll" /></form>';
1.237     raeburn  5047:     $r->print($output);
                   5048:     return;
                   5049: }
                   5050: 
1.256     raeburn  5051: sub visible_in_cat {
                   5052:     my ($cdom,$cnum) = @_;
                   5053:     my %domconf = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
                   5054:     my ($cathash,%settable,@vismsgs,$cansetvis);
                   5055:     my %visactions = &Apache::lonlocal::texthash(
1.316     bisitz   5056:                    vis => 'Your course/community currently appears in the Course/Community Catalog for this domain.',
1.256     raeburn  5057:                    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   5058:                    miss => 'Your course/community does not currently appear in the Course/Community Catalog for this domain.',
1.256     raeburn  5059:                    yous => 'You should remedy this if you plan to allow self-enrollment, otherwise students will have difficulty finding your course.',
                   5060:                    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 5061:                    make => 'Make any changes to self-enrollment settings below, click "Save", then take action to include the course in the Catalog:',
1.256     raeburn  5062:                    take => 'Take the following action to ensure the course appears in the Catalog:',
                   5063:                    dc_unhide  => 'Ask a domain coordinator to change the "Exclude from course catalog" setting.',
                   5064:                    dc_addinst => 'Ask a domain coordinator to enable display the catalog of "Official courses (with institutional codes)".',
                   5065:                    dc_instcode => 'Ask a domain coordinator to assign an institutional code (if this is an official course).',
                   5066:                    dc_catalog  => 'Ask a domain coordinator to enable or create at least one course category in the domain.',
                   5067:                    dc_categories => 'Ask a domain coordinator to create a hierarchy of categories and sub categories for courses in the domain.',
                   5068:                    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',
                   5069:                    dc_addcat => 'Ask a domain coordinator to assign a category to the course.',
                   5070:     );
1.347     raeburn  5071:     $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>"');
                   5072:     $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>"');
                   5073:     $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  5074:     if (ref($domconf{'coursecategories'}) eq 'HASH') {
                   5075:         if ($domconf{'coursecategories'}{'togglecats'} eq 'crs') {
                   5076:             $settable{'togglecats'} = 1;
                   5077:         }
                   5078:         if ($domconf{'coursecategories'}{'categorize'} eq 'crs') {
                   5079:             $settable{'categorize'} = 1;
                   5080:         }
                   5081:         $cathash = $domconf{'coursecategories'}{'cats'};
                   5082:     }
1.260     raeburn  5083:     if ($settable{'togglecats'} && $settable{'categorize'}) {
1.256     raeburn  5084:         $cansetvis = &mt('You are able to both assign a course category and choose to exclude this course from the catalog.');   
                   5085:     } elsif ($settable{'togglecats'}) {
                   5086:         $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  5087:     } elsif ($settable{'categorize'}) {
1.256     raeburn  5088:         $cansetvis = &mt('You may assign a course category, but only a Domain Coordinator may choose to exclude this course from the catalog.');  
                   5089:     } else {
                   5090:         $cansetvis = &mt('Only a Domain Coordinator may assign a course category or choose to exclude this course from the catalog.'); 
                   5091:     }
                   5092:      
                   5093:     my %currsettings =
                   5094:         &Apache::lonnet::get('environment',['hidefromcat','categories','internal.coursecode'],
                   5095:                              $cdom,$cnum);
                   5096:     my $visible = 0;
                   5097:     if ($currsettings{'internal.coursecode'} ne '') {
                   5098:         if (ref($domconf{'coursecategories'}) eq 'HASH') {
                   5099:             $cathash = $domconf{'coursecategories'}{'cats'};
                   5100:             if (ref($cathash) eq 'HASH') {
                   5101:                 if ($cathash->{'instcode::0'} eq '') {
                   5102:                     push(@vismsgs,'dc_addinst'); 
                   5103:                 } else {
                   5104:                     $visible = 1;
                   5105:                 }
                   5106:             } else {
                   5107:                 $visible = 1;
                   5108:             }
                   5109:         } else {
                   5110:             $visible = 1;
                   5111:         }
                   5112:     } else {
                   5113:         if (ref($cathash) eq 'HASH') {
                   5114:             if ($cathash->{'instcode::0'} ne '') {
                   5115:                 push(@vismsgs,'dc_instcode');
                   5116:             }
                   5117:         } else {
                   5118:             push(@vismsgs,'dc_instcode');
                   5119:         }
                   5120:     }
                   5121:     if ($currsettings{'categories'} ne '') {
                   5122:         my $cathash;
                   5123:         if (ref($domconf{'coursecategories'}) eq 'HASH') {
                   5124:             $cathash = $domconf{'coursecategories'}{'cats'};
                   5125:             if (ref($cathash) eq 'HASH') {
                   5126:                 if (keys(%{$cathash}) == 0) {
                   5127:                     push(@vismsgs,'dc_catalog');
                   5128:                 } elsif ((keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} ne '')) {
                   5129:                     push(@vismsgs,'dc_categories');
                   5130:                 } else {
                   5131:                     my @currcategories = split('&',$currsettings{'categories'});
                   5132:                     my $matched = 0;
                   5133:                     foreach my $cat (@currcategories) {
                   5134:                         if ($cathash->{$cat} ne '') {
                   5135:                             $visible = 1;
                   5136:                             $matched = 1;
                   5137:                             last;
                   5138:                         }
                   5139:                     }
                   5140:                     if (!$matched) {
1.260     raeburn  5141:                         if ($settable{'categorize'}) { 
1.256     raeburn  5142:                             push(@vismsgs,'chgcat');
                   5143:                         } else {
                   5144:                             push(@vismsgs,'dc_chgcat');
                   5145:                         }
                   5146:                     }
                   5147:                 }
                   5148:             }
                   5149:         }
                   5150:     } else {
                   5151:         if (ref($cathash) eq 'HASH') {
                   5152:             if ((keys(%{$cathash}) > 1) || 
                   5153:                 (keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} eq '')) {
1.260     raeburn  5154:                 if ($settable{'categorize'}) {
1.256     raeburn  5155:                     push(@vismsgs,'addcat');
                   5156:                 } else {
                   5157:                     push(@vismsgs,'dc_addcat');
                   5158:                 }
                   5159:             }
                   5160:         }
                   5161:     }
                   5162:     if ($currsettings{'hidefromcat'} eq 'yes') {
                   5163:         $visible = 0;
                   5164:         if ($settable{'togglecats'}) {
                   5165:             unshift(@vismsgs,'unhide');
                   5166:         } else {
                   5167:             unshift(@vismsgs,'dc_unhide')
                   5168:         }
                   5169:     }
                   5170:     return ($visible,$cansetvis,\@vismsgs,\%visactions);
                   5171: }
                   5172: 
1.241     raeburn  5173: sub new_selfenroll_dom_row {
                   5174:     my ($newdom,$num) = @_;
                   5175:     my $domdesc = &Apache::lonnet::domain($newdom);
                   5176:     my $output;
                   5177:     if ($domdesc ne '') {
                   5178:         $output .= &Apache::loncommon::start_data_table_row()
                   5179:                    .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'&nbsp;<b>'.$domdesc
                   5180:                    .' ('.$newdom.')</b><input type="hidden" name="selfenroll_dom_'.$num
1.249     raeburn  5181:                    .'" value="'.$newdom.'" /></span><br />'
                   5182:                    .'<span class="LC_nobreak"><label><input type="checkbox" '
                   5183:                    .'name="selfenroll_activate" value="'.$num.'" '
                   5184:                    .'onchange="javascript:update_types('
                   5185:                    ."'selfenroll_activate','$num'".');" />'
                   5186:                    .&mt('Activate').'</label></span></td>';
1.241     raeburn  5187:         my @currinsttypes;
                   5188:         $output .= '<td>'.&mt('User types:').'<br />'
                   5189:                    .&selfenroll_inst_types($num,$newdom,\@currinsttypes).'</td>'
                   5190:                    .&Apache::loncommon::end_data_table_row();
                   5191:     }
                   5192:     return $output;
                   5193: }
                   5194: 
                   5195: sub selfenroll_inst_types {
                   5196:     my ($num,$currdom,$currinsttypes) = @_;
                   5197:     my $output;
                   5198:     my $numinrow = 4;
                   5199:     my $count = 0;
                   5200:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($currdom);
1.247     raeburn  5201:     my $othervalue = 'any';
1.241     raeburn  5202:     if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
1.251     raeburn  5203:         if (keys(%{$usertypes}) > 0) {
1.247     raeburn  5204:             $othervalue = 'other';
                   5205:         }
1.241     raeburn  5206:         $output .= '<table><tr>';
                   5207:         foreach my $type (@{$types}) {
                   5208:             if (($count > 0) && ($count%$numinrow == 0)) {
                   5209:                 $output .= '</tr><tr>';
                   5210:             }
                   5211:             if (defined($usertypes->{$type})) {
1.257     raeburn  5212:                 my $esc_type = &escape($type);
1.241     raeburn  5213:                 $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.
1.257     raeburn  5214:                            $esc_type.'" ';
1.241     raeburn  5215:                 if (ref($currinsttypes) eq 'ARRAY') {
                   5216:                     if (@{$currinsttypes} > 0) {
1.249     raeburn  5217:                         if (grep(/^any$/,@{$currinsttypes})) {
                   5218:                             $output .= 'checked="checked"';
1.257     raeburn  5219:                         } elsif (grep(/^\Q$esc_type\E$/,@{$currinsttypes})) {
1.241     raeburn  5220:                             $output .= 'checked="checked"';
                   5221:                         }
1.249     raeburn  5222:                     } else {
                   5223:                         $output .= 'checked="checked"';
1.241     raeburn  5224:                     }
                   5225:                 }
                   5226:                 $output .= ' name="selfenroll_types_'.$num.'" />'.$usertypes->{$type}.'</label></span></td>';
                   5227:             }
                   5228:             $count ++;
                   5229:         }
                   5230:         if (($count > 0) && ($count%$numinrow == 0)) {
                   5231:             $output .= '</tr><tr>';
                   5232:         }
1.249     raeburn  5233:         $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.$othervalue.'"';
1.241     raeburn  5234:         if (ref($currinsttypes) eq 'ARRAY') {
                   5235:             if (@{$currinsttypes} > 0) {
1.249     raeburn  5236:                 if (grep(/^any$/,@{$currinsttypes})) { 
                   5237:                     $output .= ' checked="checked"';
                   5238:                 } elsif ($othervalue eq 'other') {
                   5239:                     if (grep(/^\Q$othervalue\E$/,@{$currinsttypes})) {
                   5240:                         $output .= ' checked="checked"';
                   5241:                     }
1.241     raeburn  5242:                 }
1.249     raeburn  5243:             } else {
                   5244:                 $output .= ' checked="checked"';
1.241     raeburn  5245:             }
1.249     raeburn  5246:         } else {
                   5247:             $output .= ' checked="checked"';
1.241     raeburn  5248:         }
                   5249:         $output .= ' name="selfenroll_types_'.$num.'" />'.$othertitle.'</label></span></td></tr></table>';
                   5250:     }
                   5251:     return $output;
                   5252: }
                   5253: 
1.237     raeburn  5254: sub selfenroll_date_forms {
                   5255:     my ($startform,$endform) = @_;
                   5256:     my $output .= &Apache::lonhtmlcommon::start_pick_box()."\n".
1.244     bisitz   5257:                   &Apache::lonhtmlcommon::row_title(&mt('Start date'),
1.237     raeburn  5258:                                                     'LC_oddrow_value')."\n".
                   5259:                   $startform."\n".
                   5260:                   &Apache::lonhtmlcommon::row_closure(1).
1.244     bisitz   5261:                   &Apache::lonhtmlcommon::row_title(&mt('End date'),
1.237     raeburn  5262:                                                    'LC_oddrow_value')."\n".
                   5263:                   $endform."\n".
                   5264:                   &Apache::lonhtmlcommon::row_closure(1).
                   5265:                   &Apache::lonhtmlcommon::end_pick_box();
                   5266:     return $output;
                   5267: }
                   5268: 
1.239     raeburn  5269: sub print_userchangelogs_display {
                   5270:     my ($r,$context,$permission) = @_;
                   5271:     my $formname = 'roleslog';
                   5272:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5273:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.318     raeburn  5274:     my $crstype = &Apache::loncommon::course_type();
1.239     raeburn  5275:     my %roleslog=&Apache::lonnet::dump('nohist_rolelog',$cdom,$cnum);
                   5276:     if ((keys(%roleslog))[0]=~/^error\:/) { undef(%roleslog); }
                   5277: 
                   5278:     my %saveable_parameters = ('show' => 'scalar',);
                   5279:     &Apache::loncommon::store_course_settings('roles_log',
                   5280:                                               \%saveable_parameters);
                   5281:     &Apache::loncommon::restore_course_settings('roles_log',
                   5282:                                                 \%saveable_parameters);
                   5283:     # set defaults
                   5284:     my $now = time();
                   5285:     my $defstart = $now - (7*24*3600); #7 days ago 
                   5286:     my %defaults = (
                   5287:                      page               => '1',
                   5288:                      show               => '10',
                   5289:                      role               => 'any',
                   5290:                      chgcontext         => 'any',
                   5291:                      rolelog_start_date => $defstart,
                   5292:                      rolelog_end_date   => $now,
                   5293:                    );
                   5294:     my $more_records = 0;
                   5295: 
                   5296:     # set current
                   5297:     my %curr;
                   5298:     foreach my $item ('show','page','role','chgcontext') {
                   5299:         $curr{$item} = $env{'form.'.$item};
                   5300:     }
                   5301:     my ($startdate,$enddate) = 
                   5302:         &Apache::lonuserutils::get_dates_from_form('rolelog_start_date','rolelog_end_date');
                   5303:     $curr{'rolelog_start_date'} = $startdate;
                   5304:     $curr{'rolelog_end_date'} = $enddate;
                   5305:     foreach my $key (keys(%defaults)) {
                   5306:         if ($curr{$key} eq '') {
                   5307:             $curr{$key} = $defaults{$key};
                   5308:         }
                   5309:     }
1.248     raeburn  5310:     my (%whodunit,%changed,$version);
                   5311:     ($version) = ($r->dir_config('lonVersion') =~ /^([\d\.]+)\-/);
1.239     raeburn  5312:     my ($minshown,$maxshown);
1.255     raeburn  5313:     $minshown = 1;
1.239     raeburn  5314:     my $count = 0;
                   5315:     if ($curr{'show'} ne &mt('all')) { 
                   5316:         $maxshown = $curr{'page'} * $curr{'show'};
                   5317:         if ($curr{'page'} > 1) {
                   5318:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
                   5319:         }
                   5320:     }
1.301     bisitz   5321: 
1.327     raeburn  5322:     # Form Header
                   5323:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
                   5324:               &role_display_filter($formname,$cdom,$cnum,\%curr,$version,$crstype));
                   5325: 
                   5326:     # Create navigation
                   5327:     my ($nav_script,$nav_links) = &userlogdisplay_nav($formname,\%curr,$more_records);
                   5328:     my $showntableheader = 0;
                   5329: 
                   5330:     # Table Header
                   5331:     my $tableheader = 
                   5332:         &Apache::loncommon::start_data_table_header_row()
                   5333:        .'<th>&nbsp;</th>'
                   5334:        .'<th>'.&mt('When').'</th>'
                   5335:        .'<th>'.&mt('Who made the change').'</th>'
                   5336:        .'<th>'.&mt('Changed User').'</th>'
                   5337:        .'<th>'.&mt('Role').'</th>'
                   5338:        .'<th>'.&mt('Section').'</th>'
                   5339:        .'<th>'.&mt('Context').'</th>'
                   5340:        .'<th>'.&mt('Start').'</th>'
                   5341:        .'<th>'.&mt('End').'</th>'
                   5342:        .&Apache::loncommon::end_data_table_header_row();
                   5343: 
                   5344:     # Display user change log data
1.239     raeburn  5345:     foreach my $id (sort { $roleslog{$b}{'exe_time'}<=>$roleslog{$a}{'exe_time'} } (keys(%roleslog))) {
                   5346:         next if (($roleslog{$id}{'exe_time'} < $curr{'rolelog_start_date'}) ||
                   5347:                  ($roleslog{$id}{'exe_time'} > $curr{'rolelog_end_date'}));
                   5348:         if ($curr{'show'} ne &mt('all')) {
                   5349:             if ($count >= $curr{'page'} * $curr{'show'}) {
                   5350:                 $more_records = 1;
                   5351:                 last;
                   5352:             }
                   5353:         }
                   5354:         if ($curr{'role'} ne 'any') {
                   5355:             next if ($roleslog{$id}{'logentry'}{'role'} ne $curr{'role'}); 
                   5356:         }
                   5357:         if ($curr{'chgcontext'} ne 'any') {
                   5358:             if ($curr{'chgcontext'} eq 'selfenroll') {
                   5359:                 next if (!$roleslog{$id}{'logentry'}{'selfenroll'});
                   5360:             } else {
                   5361:                 next if ($roleslog{$id}{'logentry'}{'context'} ne $curr{'chgcontext'});
                   5362:             }
                   5363:         }
                   5364:         $count ++;
                   5365:         next if ($count < $minshown);
1.327     raeburn  5366:         unless ($showntableheader) {
                   5367:             $r->print($nav_script
                   5368:                      .$nav_links
                   5369:                      .&Apache::loncommon::start_data_table()
                   5370:                      .$tableheader);
                   5371:             $r->rflush();
                   5372:             $showntableheader = 1;
                   5373:         }
1.239     raeburn  5374:         if ($whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} eq '') {
                   5375:             $whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} =
                   5376:                 &Apache::loncommon::plainname($roleslog{$id}{'exe_uname'},$roleslog{$id}{'exe_udom'});
                   5377:         }
                   5378:         if ($changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} eq '') {
                   5379:             $changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} =
                   5380:                 &Apache::loncommon::plainname($roleslog{$id}{'uname'},$roleslog{$id}{'udom'});
                   5381:         }
                   5382:         my $sec = $roleslog{$id}{'logentry'}{'section'};
                   5383:         if ($sec eq '') {
                   5384:             $sec = &mt('None');
                   5385:         }
                   5386:         my ($rolestart,$roleend);
                   5387:         if ($roleslog{$id}{'delflag'}) {
                   5388:             $rolestart = &mt('deleted');
                   5389:             $roleend = &mt('deleted');
                   5390:         } else {
                   5391:             $rolestart = $roleslog{$id}{'logentry'}{'start'};
                   5392:             $roleend = $roleslog{$id}{'logentry'}{'end'};
                   5393:             if ($rolestart eq '' || $rolestart == 0) {
                   5394:                 $rolestart = &mt('No start date'); 
                   5395:             } else {
                   5396:                 $rolestart = &Apache::lonlocal::locallocaltime($rolestart);
                   5397:             }
                   5398:             if ($roleend eq '' || $roleend == 0) { 
                   5399:                 $roleend = &mt('No end date');
                   5400:             } else {
                   5401:                 $roleend = &Apache::lonlocal::locallocaltime($roleend);
                   5402:             }
                   5403:         }
                   5404:         my $chgcontext = $roleslog{$id}{'logentry'}{'context'};
                   5405:         if ($roleslog{$id}{'logentry'}{'selfenroll'}) {
                   5406:             $chgcontext = 'selfenroll';
                   5407:         }
1.318     raeburn  5408:         my %lt = &rolechg_contexts($crstype);
1.239     raeburn  5409:         if ($chgcontext ne '' && $lt{$chgcontext} ne '') {
                   5410:             $chgcontext = $lt{$chgcontext};
                   5411:         }
1.327     raeburn  5412:         $r->print(
1.301     bisitz   5413:             &Apache::loncommon::start_data_table_row()
                   5414:            .'<td>'.$count.'</td>'
                   5415:            .'<td>'.&Apache::lonlocal::locallocaltime($roleslog{$id}{'exe_time'}).'</td>'
                   5416:            .'<td>'.$whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}}.'</td>'
                   5417:            .'<td>'.$changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}}.'</td>'
1.318     raeburn  5418:            .'<td>'.&Apache::lonnet::plaintext($roleslog{$id}{'logentry'}{'role'},$crstype).'</td>'
1.301     bisitz   5419:            .'<td>'.$sec.'</td>'
                   5420:            .'<td>'.$chgcontext.'</td>'
                   5421:            .'<td>'.$rolestart.'</td>'
                   5422:            .'<td>'.$roleend.'</td>'
1.327     raeburn  5423:            .&Apache::loncommon::end_data_table_row()."\n");
1.301     bisitz   5424:     }
                   5425: 
1.327     raeburn  5426:     if ($showntableheader) { # Table footer, if content displayed above
                   5427:         $r->print(&Apache::loncommon::end_data_table()
                   5428:                  .$nav_links);
                   5429:     } else { # No content displayed above
1.301     bisitz   5430:         $r->print('<p class="LC_info">'
                   5431:                  .&mt('There are no records to display.')
                   5432:                  .'</p>'
                   5433:         );
1.239     raeburn  5434:     }
1.301     bisitz   5435: 
1.327     raeburn  5436:     # Form Footer
                   5437:     $r->print( 
                   5438:         '<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
                   5439:        .'<input type="hidden" name="action" value="changelogs" />'
                   5440:        .'</form>');
                   5441:     return;
                   5442: }
1.301     bisitz   5443: 
1.327     raeburn  5444: sub userlogdisplay_nav {
                   5445:     my ($formname,$curr,$more_records) = @_;
                   5446:     my ($nav_script,$nav_links);
                   5447:     if (ref($curr) eq 'HASH') {
                   5448:         # Create Navigation:
                   5449:         # Navigation Script
                   5450:         $nav_script = <<"ENDSCRIPT";
1.239     raeburn  5451: <script type="text/javascript">
1.301     bisitz   5452: // <![CDATA[
1.239     raeburn  5453: function chgPage(caller) {
                   5454:     if (caller == 'previous') {
                   5455:         document.$formname.page.value --;
                   5456:     }
                   5457:     if (caller == 'next') {
                   5458:         document.$formname.page.value ++;
                   5459:     }
1.327     raeburn  5460:     document.$formname.submit();
1.239     raeburn  5461:     return;
                   5462: }
1.301     bisitz   5463: // ]]>
1.239     raeburn  5464: </script>
                   5465: ENDSCRIPT
1.327     raeburn  5466:         # Navigation Buttons
                   5467:         $nav_links = '<p>';
                   5468:         if (($curr->{'page'} > 1) || ($more_records)) {
                   5469:             if ($curr->{'page'} > 1) {
                   5470:                 $nav_links .= '<input type="button"'
                   5471:                              .' onclick="javascript:chgPage('."'previous'".');"'
                   5472:                              .' value="'.&mt('Previous [_1] changes',$curr->{'show'})
                   5473:                              .'" /> ';
                   5474:             }
                   5475:             if ($more_records) {
                   5476:                 $nav_links .= '<input type="button"'
                   5477:                              .' onclick="javascript:chgPage('."'next'".');"'
                   5478:                              .' value="'.&mt('Next [_1] changes',$curr->{'show'})
                   5479:                              .'" />';
                   5480:             }
1.301     bisitz   5481:         }
1.327     raeburn  5482:         $nav_links .= '</p>';
1.301     bisitz   5483:     }
1.327     raeburn  5484:     return ($nav_script,$nav_links);
1.239     raeburn  5485: }
                   5486: 
                   5487: sub role_display_filter {
1.318     raeburn  5488:     my ($formname,$cdom,$cnum,$curr,$version,$crstype) = @_;
1.239     raeburn  5489:     my $context = 'course';
1.318     raeburn  5490:     my $lctype = lc($crstype);
1.239     raeburn  5491:     my $nolink = 1;
                   5492:     my $output = '<table><tr><td valign="top">'.
1.301     bisitz   5493:                  '<span class="LC_nobreak"><b>'.&mt('Changes/page:').'</b></span><br />'.
1.239     raeburn  5494:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
                   5495:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
                   5496:                  '</td><td>&nbsp;&nbsp;</td>';
                   5497:     my $startform =
                   5498:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_start_date',
                   5499:                                             $curr->{'rolelog_start_date'},undef,
                   5500:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   5501:     my $endform =
                   5502:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_end_date',
                   5503:                                             $curr->{'rolelog_end_date'},undef,
                   5504:                                             undef,undef,undef,undef,undef,undef,$nolink);
1.318     raeburn  5505:     my %lt = &rolechg_contexts($crstype);
1.301     bisitz   5506:     $output .= '<td valign="top"><b>'.&mt('Window during which changes occurred:').'</b><br />'.
                   5507:                '<table><tr><td>'.&mt('After:').
                   5508:                '</td><td>'.$startform.'</td></tr>'.
                   5509:                '<tr><td>'.&mt('Before:').'</td>'.
                   5510:                '<td>'.$endform.'</td></tr></table>'.
                   5511:                '</td>'.
                   5512:                '<td>&nbsp;&nbsp;</td>'.
1.239     raeburn  5513:                '<td valign="top"><b>'.&mt('Role:').'</b><br />'.
                   5514:                '<select name="role"><option value="any"';
                   5515:     if ($curr->{'role'} eq 'any') {
                   5516:         $output .= ' selected="selected"';
                   5517:     }
                   5518:     $output .=  '>'.&mt('Any').'</option>'."\n";
1.318     raeburn  5519:     my @roles = &Apache::lonuserutils::course_roles($context,undef,1,$lctype);
1.239     raeburn  5520:     foreach my $role (@roles) {
                   5521:         my $plrole;
                   5522:         if ($role eq 'cr') {
                   5523:             $plrole = &mt('Custom Role');
                   5524:         } else {
1.318     raeburn  5525:             $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.239     raeburn  5526:         }
                   5527:         my $selstr = '';
                   5528:         if ($role eq $curr->{'role'}) {
                   5529:             $selstr = ' selected="selected"';
                   5530:         }
                   5531:         $output .= '  <option value="'.$role.'"'.$selstr.'>'.$plrole.'</option>';
                   5532:     }
1.301     bisitz   5533:     $output .= '</select></td>'.
                   5534:                '<td>&nbsp;&nbsp;</td>'.
                   5535:                '<td valign="top"><b>'.
1.239     raeburn  5536:                &mt('Context:').'</b><br /><select name="chgcontext">';
1.318     raeburn  5537:     foreach my $chgtype ('any','auto','updatenow','createcourse','course','domain','selfenroll','requestcourses') {
1.239     raeburn  5538:         my $selstr = '';
                   5539:         if ($curr->{'chgcontext'} eq $chgtype) {
1.301     bisitz   5540:             $selstr = ' selected="selected"';
1.239     raeburn  5541:         }
                   5542:         if (($chgtype eq 'auto') || ($chgtype eq 'updatenow')) {
                   5543:             next if (!&Apache::lonnet::auto_run($cnum,$cdom));
                   5544:         }
                   5545:         $output .= '<option value="'.$chgtype.'"'.$selstr.'>'.$lt{$chgtype}.'</option>'."\n";
1.248     raeburn  5546:     }
1.303     bisitz   5547:     $output .= '</select></td>'
                   5548:               .'</tr></table>';
                   5549: 
                   5550:     # Update Display button
                   5551:     $output .= '<p>'
                   5552:               .'<input type="submit" value="'.&mt('Update Display').'" />'
                   5553:               .'</p>';
                   5554: 
                   5555:     # Server version info
                   5556:     $output .= '<p class="LC_info">'
                   5557:               .&mt('Only changes made from servers running LON-CAPA [_1] or later are displayed.'
                   5558:                   ,'2.6.99.0');
1.248     raeburn  5559:     if ($version) {
1.303     bisitz   5560:         $output .= ' '.&mt('This LON-CAPA server is version [_1]',$version);
                   5561:     }
                   5562:     $output .= '</p><hr />';
1.239     raeburn  5563:     return $output;
                   5564: }
                   5565: 
                   5566: sub rolechg_contexts {
1.318     raeburn  5567:     my ($crstype) = @_;
1.239     raeburn  5568:     my %lt = &Apache::lonlocal::texthash (
                   5569:                                              any          => 'Any',
                   5570:                                              auto         => 'Automated enrollment',
                   5571:                                              updatenow    => 'Roster Update',
                   5572:                                              createcourse => 'Course Creation',
                   5573:                                              course       => 'User Management in course',
                   5574:                                              domain       => 'User Management in domain',
1.313     raeburn  5575:                                              selfenroll   => 'Self-enrolled',
1.318     raeburn  5576:                                              requestcourses => 'Course Request',
1.239     raeburn  5577:                                          );
1.318     raeburn  5578:     if ($crstype eq 'Community') {
                   5579:         $lt{'createcourse'} = &mt('Community Creation');
                   5580:         $lt{'course'} = &mt('User Management in community');
                   5581:         $lt{'requestcourses'} = &mt('Community Request');
                   5582:     }
1.239     raeburn  5583:     return %lt;
                   5584: }
                   5585: 
1.27      matthew  5586: #-------------------------------------------------- functions for &phase_two
1.160     raeburn  5587: sub user_search_result {
1.221     raeburn  5588:     my ($context,$srch) = @_;
1.160     raeburn  5589:     my %allhomes;
                   5590:     my %inst_matches;
                   5591:     my %srch_results;
1.181     raeburn  5592:     my ($response,$currstate,$forcenewuser,$dirsrchres);
1.183     raeburn  5593:     $srch->{'srchterm'} =~ s/\s+/ /g;
1.176     raeburn  5594:     if ($srch->{'srchby'} !~ /^(uname|lastname|lastfirst)$/) {
1.160     raeburn  5595:         $response = &mt('Invalid search.');
                   5596:     }
                   5597:     if ($srch->{'srchin'} !~ /^(crs|dom|alc|instd)$/) {
                   5598:         $response = &mt('Invalid search.');
                   5599:     }
1.177     raeburn  5600:     if ($srch->{'srchtype'} !~ /^(exact|contains|begins)$/) {
1.160     raeburn  5601:         $response = &mt('Invalid search.');
                   5602:     }
                   5603:     if ($srch->{'srchterm'} eq '') {
                   5604:         $response = &mt('You must enter a search term.');
                   5605:     }
1.183     raeburn  5606:     if ($srch->{'srchterm'} =~ /^\s+$/) {
                   5607:         $response = &mt('Your search term must contain more than just spaces.');
                   5608:     }
1.160     raeburn  5609:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'instd')) {
                   5610:         if (($srch->{'srchdomain'} eq '') || 
1.163     albertel 5611: 	    ! (&Apache::lonnet::domain($srch->{'srchdomain'}))) {
1.160     raeburn  5612:             $response = &mt('You must specify a valid domain when searching in a domain or institutional directory.')
                   5613:         }
                   5614:     }
                   5615:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs') ||
                   5616:         ($srch->{'srchin'} eq 'alc')) {
1.176     raeburn  5617:         if ($srch->{'srchby'} eq 'uname') {
1.243     raeburn  5618:             my $unamecheck = $srch->{'srchterm'};
                   5619:             if ($srch->{'srchtype'} eq 'contains') {
                   5620:                 if ($unamecheck !~ /^\w/) {
                   5621:                     $unamecheck = 'a'.$unamecheck; 
                   5622:                 }
                   5623:             }
                   5624:             if ($unamecheck !~ /^$match_username$/) {
1.176     raeburn  5625:                 $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @');
                   5626:             }
1.160     raeburn  5627:         }
                   5628:     }
1.180     raeburn  5629:     if ($response ne '') {
                   5630:         $response = '<span class="LC_warning">'.$response.'</span>';
                   5631:     }
1.160     raeburn  5632:     if ($srch->{'srchin'} eq 'instd') {
                   5633:         my $instd_chk = &directorysrch_check($srch);
                   5634:         if ($instd_chk ne 'ok') {
1.180     raeburn  5635:             $response = '<span class="LC_warning">'.$instd_chk.'</span>'.
                   5636:                         '<br />'.&mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').'<br /><br />';
1.160     raeburn  5637:         }
                   5638:     }
                   5639:     if ($response ne '') {
1.180     raeburn  5640:         return ($currstate,$response);
1.160     raeburn  5641:     }
                   5642:     if ($srch->{'srchby'} eq 'uname') {
                   5643:         if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs')) {
                   5644:             if ($env{'form.forcenew'}) {
                   5645:                 if ($srch->{'srchdomain'} ne $env{'request.role.domain'}) {
                   5646:                     my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   5647:                     if ($uhome eq 'no_host') {
                   5648:                         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
1.180     raeburn  5649:                         my $showdom = &display_domain_info($env{'request.role.domain'});
                   5650:                         $response = &mt('New users can only be created in the domain to which your current role belongs - [_1].',$showdom);
1.160     raeburn  5651:                     } else {
1.179     raeburn  5652:                         $currstate = 'modify';
1.160     raeburn  5653:                     }
                   5654:                 } else {
1.179     raeburn  5655:                     $currstate = 'modify';
1.160     raeburn  5656:                 }
                   5657:             } else {
                   5658:                 if ($srch->{'srchin'} eq 'dom') {
1.162     raeburn  5659:                     if ($srch->{'srchtype'} eq 'exact') {
                   5660:                         my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   5661:                         if ($uhome eq 'no_host') {
1.179     raeburn  5662:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  5663:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  5664:                         } else {
1.179     raeburn  5665:                             $currstate = 'modify';
1.310     raeburn  5666:                             my $uname = $srch->{'srchterm'};
                   5667:                             my $udom = $srch->{'srchdomain'};
                   5668:                             $srch_results{$uname.':'.$udom} =
                   5669:                                 { &Apache::lonnet::get('environment',
                   5670:                                                        ['firstname',
                   5671:                                                         'lastname',
                   5672:                                                         'permanentemail'],
                   5673:                                                          $udom,$uname)
                   5674:                                 };
1.162     raeburn  5675:                         }
                   5676:                     } else {
                   5677:                         %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  5678:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  5679:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  5680:                     }
                   5681:                 } else {
1.167     albertel 5682:                     my $courseusers = &get_courseusers();
1.162     raeburn  5683:                     if ($srch->{'srchtype'} eq 'exact') {
1.167     albertel 5684:                         if (exists($courseusers->{$srch->{'srchterm'}.':'.$srch->{'srchdomain'}})) {
1.179     raeburn  5685:                             $currstate = 'modify';
1.162     raeburn  5686:                         } else {
1.179     raeburn  5687:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  5688:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  5689:                         }
1.160     raeburn  5690:                     } else {
1.167     albertel 5691:                         foreach my $user (keys(%$courseusers)) {
1.162     raeburn  5692:                             my ($cuname,$cudomain) = split(/:/,$user);
                   5693:                             if ($cudomain eq $srch->{'srchdomain'}) {
1.177     raeburn  5694:                                 my $matched = 0;
                   5695:                                 if ($srch->{'srchtype'} eq 'begins') {
                   5696:                                     if ($cuname =~ /^\Q$srch->{'srchterm'}\E/i) {
                   5697:                                         $matched = 1;
                   5698:                                     }
                   5699:                                 } else {
                   5700:                                     if ($cuname =~ /\Q$srch->{'srchterm'}\E/i) {
                   5701:                                         $matched = 1;
                   5702:                                     }
                   5703:                                 }
                   5704:                                 if ($matched) {
1.167     albertel 5705:                                     $srch_results{$user} = 
                   5706: 					{&Apache::lonnet::get('environment',
                   5707: 							     ['firstname',
                   5708: 							      'lastname',
1.194     albertel 5709: 							      'permanentemail'],
                   5710: 							      $cudomain,$cuname)};
1.162     raeburn  5711:                                 }
                   5712:                             }
                   5713:                         }
1.179     raeburn  5714:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  5715:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  5716:                     }
                   5717:                 }
                   5718:             }
                   5719:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  5720:             $currstate = 'query';
1.160     raeburn  5721:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  5722:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch);
                   5723:             if ($dirsrchres eq 'ok') {
                   5724:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  5725:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  5726:             } else {
                   5727:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   5728:                 $response = '<span class="LC_warning">'.
                   5729:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   5730:                     '</span><br />'.
                   5731:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
                   5732:                     '<br /><br />'; 
                   5733:             }
1.160     raeburn  5734:         }
                   5735:     } else {
                   5736:         if ($srch->{'srchin'} eq 'dom') {
                   5737:             %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  5738:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  5739:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  5740:         } elsif ($srch->{'srchin'} eq 'crs') {
1.167     albertel 5741:             my $courseusers = &get_courseusers(); 
                   5742:             foreach my $user (keys(%$courseusers)) {
1.160     raeburn  5743:                 my ($uname,$udom) = split(/:/,$user);
                   5744:                 my %names = &Apache::loncommon::getnames($uname,$udom);
                   5745:                 my %emails = &Apache::loncommon::getemails($uname,$udom);
                   5746:                 if ($srch->{'srchby'} eq 'lastname') {
                   5747:                     if ((($srch->{'srchtype'} eq 'exact') && 
                   5748:                          ($names{'lastname'} eq $srch->{'srchterm'})) || 
1.177     raeburn  5749:                         (($srch->{'srchtype'} eq 'begins') &&
                   5750:                          ($names{'lastname'} =~ /^\Q$srch->{'srchterm'}\E/i)) ||
1.160     raeburn  5751:                         (($srch->{'srchtype'} eq 'contains') &&
                   5752:                          ($names{'lastname'} =~ /\Q$srch->{'srchterm'}\E/i))) {
                   5753:                         $srch_results{$user} = {firstname => $names{'firstname'},
                   5754:                                             lastname => $names{'lastname'},
                   5755:                                             permanentemail => $emails{'permanentemail'},
                   5756:                                            };
                   5757:                     }
                   5758:                 } elsif ($srch->{'srchby'} eq 'lastfirst') {
                   5759:                     my ($srchlast,$srchfirst) = split(/,/,$srch->{'srchterm'});
1.177     raeburn  5760:                     $srchlast =~ s/\s+$//;
                   5761:                     $srchfirst =~ s/^\s+//;
1.160     raeburn  5762:                     if ($srch->{'srchtype'} eq 'exact') {
                   5763:                         if (($names{'lastname'} eq $srchlast) &&
                   5764:                             ($names{'firstname'} eq $srchfirst)) {
                   5765:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   5766:                                                 lastname => $names{'lastname'},
                   5767:                                                 permanentemail => $emails{'permanentemail'},
                   5768: 
                   5769:                                            };
                   5770:                         }
1.177     raeburn  5771:                     } elsif ($srch->{'srchtype'} eq 'begins') {
                   5772:                         if (($names{'lastname'} =~ /^\Q$srchlast\E/i) &&
                   5773:                             ($names{'firstname'} =~ /^\Q$srchfirst\E/i)) {
                   5774:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   5775:                                                 lastname => $names{'lastname'},
                   5776:                                                 permanentemail => $emails{'permanentemail'},
                   5777:                                                };
                   5778:                         }
                   5779:                     } else {
1.160     raeburn  5780:                         if (($names{'lastname'} =~ /\Q$srchlast\E/i) && 
                   5781:                             ($names{'firstname'} =~ /\Q$srchfirst\E/i)) {
                   5782:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   5783:                                                 lastname => $names{'lastname'},
                   5784:                                                 permanentemail => $emails{'permanentemail'},
                   5785:                                                };
                   5786:                         }
                   5787:                     }
                   5788:                 }
                   5789:             }
1.179     raeburn  5790:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  5791:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  5792:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  5793:             $currstate = 'query';
1.160     raeburn  5794:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  5795:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch); 
                   5796:             if ($dirsrchres eq 'ok') {
                   5797:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  5798:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  5799:             } else {
                   5800:                 my $showdom = &display_domain_info($srch->{'srchdomain'});                $response = '<span class="LC_warning">'.
                   5801:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   5802:                     '</span><br />'.
                   5803:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
                   5804:                     '<br /><br />';
                   5805:             }
1.160     raeburn  5806:         }
                   5807:     }
1.179     raeburn  5808:     return ($currstate,$response,$forcenewuser,\%srch_results);
1.160     raeburn  5809: }
                   5810: 
                   5811: sub directorysrch_check {
                   5812:     my ($srch) = @_;
                   5813:     my $can_search = 0;
                   5814:     my $response;
                   5815:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
                   5816:                                              ['directorysrch'],$srch->{'srchdomain'});
1.180     raeburn  5817:     my $showdom = &display_domain_info($srch->{'srchdomain'});
1.160     raeburn  5818:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
                   5819:         if (!$dom_inst_srch{'directorysrch'}{'available'}) {
1.180     raeburn  5820:             return &mt('Institutional directory search is not available in domain: [_1]',$showdom); 
1.160     raeburn  5821:         }
                   5822:         if ($dom_inst_srch{'directorysrch'}{'localonly'}) {
                   5823:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
1.180     raeburn  5824:                 return &mt('Institutional directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom); 
1.160     raeburn  5825:             }
                   5826:             my @usertypes = split(/:/,$env{'environment.inststatus'});
                   5827:             if (!@usertypes) {
                   5828:                 push(@usertypes,'default');
                   5829:             }
                   5830:             if (ref($dom_inst_srch{'directorysrch'}{'cansearch'}) eq 'ARRAY') {
                   5831:                 foreach my $type (@usertypes) {
                   5832:                     if (grep(/^\Q$type\E$/,@{$dom_inst_srch{'directorysrch'}{'cansearch'}})) {
                   5833:                         $can_search = 1;
                   5834:                         last;
                   5835:                     }
                   5836:                 }
                   5837:             }
                   5838:             if (!$can_search) {
                   5839:                 my ($insttypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($srch->{'srchdomain'});
                   5840:                 my @longtypes; 
                   5841:                 foreach my $item (@usertypes) {
1.229     raeburn  5842:                     if (defined($insttypes->{$item})) { 
                   5843:                         push (@longtypes,$insttypes->{$item});
                   5844:                     } elsif ($item eq 'default') {
                   5845:                         push (@longtypes,&mt('other')); 
                   5846:                     }
1.160     raeburn  5847:                 }
                   5848:                 my $insttype_str = join(', ',@longtypes); 
1.180     raeburn  5849:                 return &mt('Institutional directory search in domain: [_1] is not available to your user type: ',$showdom).$insttype_str;
1.229     raeburn  5850:             }
1.160     raeburn  5851:         } else {
                   5852:             $can_search = 1;
                   5853:         }
                   5854:     } else {
1.180     raeburn  5855:         return &mt('Institutional directory search has not been configured for domain: [_1]',$showdom);
1.160     raeburn  5856:     }
                   5857:     my %longtext = &Apache::lonlocal::texthash (
1.167     albertel 5858:                        uname     => 'username',
1.160     raeburn  5859:                        lastfirst => 'last name, first name',
1.167     albertel 5860:                        lastname  => 'last name',
1.172     raeburn  5861:                        contains  => 'contains',
1.178     raeburn  5862:                        exact     => 'as exact match to',
                   5863:                        begins    => 'begins with',
1.160     raeburn  5864:                    );
                   5865:     if ($can_search) {
                   5866:         if (ref($dom_inst_srch{'directorysrch'}{'searchby'}) eq 'ARRAY') {
                   5867:             if (!grep(/^\Q$srch->{'srchby'}\E$/,@{$dom_inst_srch{'directorysrch'}{'searchby'}})) {
1.180     raeburn  5868:                 return &mt('Institutional directory search in domain: [_1] is not available for searching by "[_2]"',$showdom,$longtext{$srch->{'srchby'}});
1.160     raeburn  5869:             }
                   5870:         } else {
1.180     raeburn  5871:             return &mt('Institutional directory search in domain: [_1] is not available.', $showdom);
1.160     raeburn  5872:         }
                   5873:     }
                   5874:     if ($can_search) {
1.178     raeburn  5875:         if (ref($dom_inst_srch{'directorysrch'}{'searchtypes'}) eq 'ARRAY') {
                   5876:             if (grep(/^\Q$srch->{'srchtype'}\E/,@{$dom_inst_srch{'directorysrch'}{'searchtypes'}})) {
                   5877:                 return 'ok';
                   5878:             } else {
1.180     raeburn  5879:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  5880:             }
                   5881:         } else {
                   5882:             if ((($dom_inst_srch{'directorysrch'}{'searchtypes'} eq 'specify') &&
                   5883:                  ($srch->{'srchtype'} eq 'exact' || $srch->{'srchtype'} eq 'contains')) ||
                   5884:                 ($dom_inst_srch{'directorysrch'}{'searchtypes'} eq $srch->{'srchtype'})) {
                   5885:                 return 'ok';
                   5886:             } else {
1.180     raeburn  5887:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  5888:             }
1.160     raeburn  5889:         }
                   5890:     }
                   5891: }
                   5892: 
                   5893: sub get_courseusers {
                   5894:     my %advhash;
1.167     albertel 5895:     my $classlist = &Apache::loncoursedata::get_classlist();
1.160     raeburn  5896:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
                   5897:     foreach my $role (sort(keys(%coursepersonnel))) {
                   5898:         foreach my $user (split(/\,/,$coursepersonnel{$role})) {
1.167     albertel 5899: 	    if (!exists($classlist->{$user})) {
                   5900: 		$classlist->{$user} = [];
                   5901: 	    }
1.160     raeburn  5902:         }
                   5903:     }
1.167     albertel 5904:     return $classlist;
1.160     raeburn  5905: }
                   5906: 
                   5907: sub build_search_response {
1.221     raeburn  5908:     my ($context,$srch,%srch_results) = @_;
1.179     raeburn  5909:     my ($currstate,$response,$forcenewuser);
1.160     raeburn  5910:     my %names = (
1.330     bisitz   5911:           'uname'     => 'username',
                   5912:           'lastname'  => 'last name',
1.160     raeburn  5913:           'lastfirst' => 'last name, first name',
1.330     bisitz   5914:           'crs'       => 'this course',
                   5915:           'dom'       => 'LON-CAPA domain',
                   5916:           'instd'     => 'the institutional directory for domain',
1.160     raeburn  5917:     );
                   5918: 
                   5919:     my %single = (
1.180     raeburn  5920:                    begins   => 'A match',
1.160     raeburn  5921:                    contains => 'A match',
1.180     raeburn  5922:                    exact    => 'An exact match',
1.160     raeburn  5923:                  );
                   5924:     my %nomatch = (
1.180     raeburn  5925:                    begins   => 'No match',
1.160     raeburn  5926:                    contains => 'No match',
1.180     raeburn  5927:                    exact    => 'No exact match',
1.160     raeburn  5928:                   );
                   5929:     if (keys(%srch_results) > 1) {
1.179     raeburn  5930:         $currstate = 'select';
1.160     raeburn  5931:     } else {
                   5932:         if (keys(%srch_results) == 1) {
1.179     raeburn  5933:             $currstate = 'modify';
1.180     raeburn  5934:             $response = &mt("$single{$srch->{'srchtype'}} was found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}.",$srch->{'srchterm'});
                   5935:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   5936:                 $response .= ': '.&display_domain_info($srch->{'srchdomain'});
1.180     raeburn  5937:             }
1.330     bisitz   5938:         } else { # Search has nothing found. Prepare message to user.
                   5939:             $response = '<span class="LC_warning">';
1.180     raeburn  5940:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   5941:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}: [_2]",
                   5942:                                  '<b>'.$srch->{'srchterm'}.'</b>',
                   5943:                                  &display_domain_info($srch->{'srchdomain'}));
                   5944:             } else {
                   5945:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}.",
                   5946:                                  '<b>'.$srch->{'srchterm'}.'</b>');
1.180     raeburn  5947:             }
                   5948:             $response .= '</span>';
1.330     bisitz   5949: 
1.160     raeburn  5950:             if ($srch->{'srchin'} ne 'alc') {
                   5951:                 $forcenewuser = 1;
                   5952:                 my $cansrchinst = 0; 
                   5953:                 if ($srch->{'srchdomain'}) {
                   5954:                     my %domconfig = &Apache::lonnet::get_dom('configuration',['directorysrch'],$srch->{'srchdomain'});
                   5955:                     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
                   5956:                         if ($domconfig{'directorysrch'}{'available'}) {
                   5957:                             $cansrchinst = 1;
                   5958:                         } 
                   5959:                     }
                   5960:                 }
1.180     raeburn  5961:                 if ((($srch->{'srchby'} eq 'lastfirst') || 
                   5962:                      ($srch->{'srchby'} eq 'lastname')) &&
                   5963:                     ($srch->{'srchin'} eq 'dom')) {
                   5964:                     if ($cansrchinst) {
                   5965:                         $response .= '<br />'.&mt('You may want to broaden your search to a search of the institutional directory for the domain.');
1.160     raeburn  5966:                     }
                   5967:                 }
1.180     raeburn  5968:                 if ($srch->{'srchin'} eq 'crs') {
                   5969:                     $response .= '<br />'.&mt('You may want to broaden your search to the selected LON-CAPA domain.');
                   5970:                 }
                   5971:             }
1.305     raeburn  5972:             my $createdom = $env{'request.role.domain'};
                   5973:             if ($context eq 'requestcrs') {
                   5974:                 if ($env{'form.coursedom'} ne '') {
                   5975:                     $createdom = $env{'form.coursedom'};
                   5976:                 }
                   5977:             }
                   5978:             if (!($srch->{'srchby'} eq 'uname' && $srch->{'srchin'} eq 'dom' && $srch->{'srchtype'} eq 'exact' && $srch->{'srchdomain'} eq $createdom)) {
1.221     raeburn  5979:                 my $cancreate =
1.305     raeburn  5980:                     &Apache::lonuserutils::can_create_user($createdom,$context);
                   5981:                 my $targetdom = '<span class="LC_cusr_emph">'.$createdom.'</span>';
1.221     raeburn  5982:                 if ($cancreate) {
1.305     raeburn  5983:                     my $showdom = &display_domain_info($createdom); 
1.266     bisitz   5984:                     $response .= '<br /><br />'
                   5985:                                 .'<b>'.&mt('To add a new user:').'</b>'
1.305     raeburn  5986:                                 .'<br />';
                   5987:                     if ($context eq 'requestcrs') {
                   5988:                         $response .= &mt("(You can only define new users in the new course's domain - [_1])",$targetdom);
                   5989:                     } else {
                   5990:                         $response .= &mt("(You can only create new users in your current role's domain - [_1])",$targetdom);
                   5991:                     }
                   5992:                     $response .='<ul><li>'
1.266     bisitz   5993:                                 .&mt("Set 'Domain/institution to search' to: [_1]",'<span class="LC_cusr_emph">'.$showdom.'</span>')
                   5994:                                 .'</li><li>'
                   5995:                                 .&mt("Set 'Search criteria' to: [_1]username is ..... in selected LON-CAPA domain[_2]",'<span class="LC_cusr_emph">','</span>')
                   5996:                                 .'</li><li>'
                   5997:                                 .&mt('Provide the proposed username')
                   5998:                                 .'</li><li>'
                   5999:                                 .&mt("Click 'Search'")
                   6000:                                 .'</li></ul><br />';
1.221     raeburn  6001:                 } else {
                   6002:                     my $helplink = ' href="javascript:helpMenu('."'display'".')"';
1.305     raeburn  6003:                     $response .= '<br /><br />';
                   6004:                     if ($context eq 'requestcrs') {
1.314     raeburn  6005:                         $response .= &mt("You are not authorized to define new users in the new course's domain - [_1].",$targetdom);
1.305     raeburn  6006:                     } else {
                   6007:                         $response .= &mt("You are not authorized to create new users in your current role's domain - [_1].",$targetdom);
                   6008:                     }
                   6009:                     $response .= '<br />'
                   6010:                                  .&mt('Please contact the [_1]helpdesk[_2] if you need to create a new user.'
1.266     bisitz   6011:                                     ,' <a'.$helplink.'>'
                   6012:                                     ,'</a>')
1.305     raeburn  6013:                                  .'<br /><br />';
1.221     raeburn  6014:                 }
1.160     raeburn  6015:             }
                   6016:         }
                   6017:     }
1.179     raeburn  6018:     return ($currstate,$response,$forcenewuser);
1.160     raeburn  6019: }
                   6020: 
1.180     raeburn  6021: sub display_domain_info {
                   6022:     my ($dom) = @_;
                   6023:     my $output = $dom;
                   6024:     if ($dom ne '') { 
                   6025:         my $domdesc = &Apache::lonnet::domain($dom,'description');
                   6026:         if ($domdesc ne '') {
                   6027:             $output .= ' <span class="LC_cusr_emph">('.$domdesc.')</span>';
                   6028:         }
                   6029:     }
                   6030:     return $output;
                   6031: }
                   6032: 
1.160     raeburn  6033: sub crumb_utilities {
                   6034:     my %elements = (
                   6035:        crtuser => {
                   6036:            srchterm => 'text',
1.172     raeburn  6037:            srchin => 'selectbox',
1.160     raeburn  6038:            srchby => 'selectbox',
                   6039:            srchtype => 'selectbox',
                   6040:            srchdomain => 'selectbox',
                   6041:        },
1.207     raeburn  6042:        crtusername => {
                   6043:            srchterm => 'text',
                   6044:            srchdomain => 'selectbox',
                   6045:        },
1.160     raeburn  6046:        docustom => {
                   6047:            rolename => 'selectbox',
                   6048:            newrolename => 'textbox',
                   6049:        },
1.179     raeburn  6050:        studentform => {
                   6051:            srchterm => 'text',
                   6052:            srchin => 'selectbox',
                   6053:            srchby => 'selectbox',
                   6054:            srchtype => 'selectbox',
                   6055:            srchdomain => 'selectbox',
                   6056:        },
1.160     raeburn  6057:     );
                   6058: 
                   6059:     my $jsback .= qq|
                   6060: function backPage(formname,prevphase,prevstate) {
1.211     raeburn  6061:     if (typeof prevphase == 'undefined') {
                   6062:         formname.phase.value = '';
                   6063:     }
                   6064:     else {  
                   6065:         formname.phase.value = prevphase;
                   6066:     }
                   6067:     if (typeof prevstate == 'undefined') {
                   6068:         formname.currstate.value = '';
                   6069:     }
                   6070:     else {
                   6071:         formname.currstate.value = prevstate;
                   6072:     }
1.160     raeburn  6073:     formname.submit();
                   6074: }
                   6075: |;
                   6076:     return ($jsback,\%elements);
                   6077: }
                   6078: 
1.26      matthew  6079: sub course_level_table {
1.89      raeburn  6080:     my (%inccourses) = @_;
1.26      matthew  6081:     my $table = '';
1.62      www      6082: # Custom Roles?
                   6083: 
1.190     raeburn  6084:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.89      raeburn  6085:     my %lt=&Apache::lonlocal::texthash(
                   6086:             'exs'  => "Existing sections",
                   6087:             'new'  => "Define new section",
                   6088:             'ssd'  => "Set Start Date",
                   6089:             'sed'  => "Set End Date",
1.131     raeburn  6090:             'crl'  => "Course Level",
1.89      raeburn  6091:             'act'  => "Activate",
                   6092:             'rol'  => "Role",
                   6093:             'ext'  => "Extent",
1.113     raeburn  6094:             'grs'  => "Section",
1.89      raeburn  6095:             'sta'  => "Start",
                   6096:             'end'  => "End"
                   6097:     );
1.62      www      6098: 
1.329     raeburn  6099:     foreach my $protectedcourse (sort(keys(%inccourses))) {
1.135     raeburn  6100: 	my $thiscourse=$protectedcourse;
1.26      matthew  6101: 	$thiscourse=~s:_:/:g;
                   6102: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
1.329     raeburn  6103:         my $isowner = &is_courseowner($protectedcourse,$coursedata{'internal.courseowner'});
1.26      matthew  6104: 	my $area=$coursedata{'description'};
1.321     raeburn  6105:         my $crstype=$coursedata{'type'};
1.135     raeburn  6106: 	if (!defined($area)) { $area=&mt('Unavailable course').': '.$protectedcourse; }
1.89      raeburn  6107: 	my ($domain,$cnum)=split(/\//,$thiscourse);
1.115     albertel 6108:         my %sections_count;
1.101     albertel 6109:         if (defined($env{'request.course.id'})) {
                   6110:             if ($env{'request.course.id'} eq $domain.'_'.$cnum) {
1.115     albertel 6111:                 %sections_count = 
                   6112: 		    &Apache::loncommon::get_sections($domain,$cnum);
1.92      raeburn  6113:             }
                   6114:         }
1.321     raeburn  6115:         my @roles = &Apache::lonuserutils::roles_by_context('course','',$crstype);
1.213     raeburn  6116: 	foreach my $role (@roles) {
1.321     raeburn  6117:             my $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.329     raeburn  6118: 	    if ((&Apache::lonnet::allowed('c'.$role,$thiscourse)) ||
                   6119:                 ((($role eq 'cc') || ($role eq 'co')) && ($isowner))) {
1.221     raeburn  6120:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.329     raeburn  6121:                                             $plrole,\%sections_count,\%lt);
1.221     raeburn  6122:             } elsif ($env{'request.course.sec'} ne '') {
                   6123:                 if (&Apache::lonnet::allowed('c'.$role,$thiscourse.'/'.
                   6124:                                              $env{'request.course.sec'})) {
                   6125:                     $table .= &course_level_row($protectedcourse,$role,$area,$domain,
                   6126:                                                 $plrole,\%sections_count,\%lt);
1.26      matthew  6127:                 }
                   6128:             }
                   6129:         }
1.221     raeburn  6130:         if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
1.324     raeburn  6131:             foreach my $cust (sort(keys(%customroles))) {
                   6132:                 next if ($crstype eq 'Community' && $customroles{$cust} =~ /bre\&S/);
1.221     raeburn  6133:                 my $role = 'cr_cr_'.$env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cust;
                   6134:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
                   6135:                                             $cust,\%sections_count,\%lt);
                   6136:             }
1.62      www      6137: 	}
1.26      matthew  6138:     }
                   6139:     return '' if ($table eq ''); # return nothing if there is nothing 
                   6140:                                  # in the table
1.188     raeburn  6141:     my $result;
                   6142:     if (!$env{'request.course.id'}) {
                   6143:         $result = '<h4>'.$lt{'crl'}.'</h4>'."\n";
                   6144:     }
                   6145:     $result .= 
1.136     raeburn  6146: &Apache::loncommon::start_data_table().
                   6147: &Apache::loncommon::start_data_table_header_row().
                   6148: '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.'</th>
                   6149: <th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
                   6150: &Apache::loncommon::end_data_table_header_row().
                   6151: $table.
                   6152: &Apache::loncommon::end_data_table();
1.26      matthew  6153:     return $result;
                   6154: }
1.88      raeburn  6155: 
1.221     raeburn  6156: sub course_level_row {
                   6157:     my ($protectedcourse,$role,$area,$domain,$plrole,$sections_count,$lt) = @_;
1.222     raeburn  6158:     my $row = &Apache::loncommon::start_data_table_row().
                   6159:               ' <td><input type="checkbox" name="act_'.
                   6160:               $protectedcourse.'_'.$role.'" /></td>'."\n".
                   6161:               ' <td>'.$plrole.'</td>'."\n".
                   6162:               ' <td>'.$area.'<br />Domain: '.$domain.'</td>'."\n";
1.322     raeburn  6163:     if (($role eq 'cc') || ($role eq 'co')) {
1.222     raeburn  6164:         $row .= '<td>&nbsp;</td>';
1.221     raeburn  6165:     } elsif ($env{'request.course.sec'} ne '') {
1.222     raeburn  6166:         $row .= ' <td><input type="hidden" value="'.
                   6167:                 $env{'request.course.sec'}.'" '.
                   6168:                 'name="sec_'.$protectedcourse.'_'.$role.'" />'.
                   6169:                 $env{'request.course.sec'}.'</td>';
1.221     raeburn  6170:     } else {
                   6171:         if (ref($sections_count) eq 'HASH') {
                   6172:             my $currsec = 
                   6173:                 &Apache::lonuserutils::course_sections($sections_count,
                   6174:                                                        $protectedcourse.'_'.$role);
1.222     raeburn  6175:             $row .= '<td><table class="LC_createuser">'."\n".
                   6176:                     '<tr class="LC_section_row">'."\n".
                   6177:                     ' <td valign="top">'.$lt->{'exs'}.'<br />'.
                   6178:                        $currsec.'</td>'."\n".
                   6179:                      ' <td>&nbsp;&nbsp;</td>'."\n".
                   6180:                      ' <td valign="top">&nbsp;'.$lt->{'new'}.'<br />'.
1.221     raeburn  6181:                      '<input type="text" name="newsec_'.$protectedcourse.'_'.$role.
                   6182:                      '" value="" />'.
                   6183:                      '<input type="hidden" '.
                   6184:                      'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n".
1.222     raeburn  6185:                      '</tr></table></td>'."\n";
1.221     raeburn  6186:         } else {
1.222     raeburn  6187:             $row .= '<td><input type="text" size="10" '.
                   6188:                       'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n";
1.221     raeburn  6189:         }
                   6190:     }
1.222     raeburn  6191:     $row .= <<ENDTIMEENTRY;
                   6192: <td><input type="hidden" name="start_$protectedcourse\_$role" value="" />
1.221     raeburn  6193: <a href=
                   6194: "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  6195: <td><input type="hidden" name="end_$protectedcourse\_$role" value="" />
1.221     raeburn  6196: <a href=
                   6197: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$role.value,'end_$protectedcourse\_$role','cu.pres','dateset')">$lt->{'sed'}</a></td>
                   6198: ENDTIMEENTRY
1.222     raeburn  6199:     $row .= &Apache::loncommon::end_data_table_row();
                   6200:     return $row;
1.221     raeburn  6201: }
                   6202: 
1.88      raeburn  6203: sub course_level_dc {
                   6204:     my ($dcdom) = @_;
1.190     raeburn  6205:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.213     raeburn  6206:     my @roles = &Apache::lonuserutils::roles_by_context('course');
1.88      raeburn  6207:     my $hiddenitems = '<input type="hidden" name="dcdomain" value="'.$dcdom.'" />'.
                   6208:                       '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
1.133     raeburn  6209:                       '<input type="hidden" name="dccourse" value="" />';
1.355   ! www      6210:     my $courseform=&Apache::loncommon::selectcourse_link
        !          6211:             ('cu','dccourse','dcdomain','coursedesc',undef,undef,'Course/Community','crstype');
1.323     raeburn  6212:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom,'currsec','cu','role','Course/Community Browser');
1.88      raeburn  6213:     my %lt=&Apache::lonlocal::texthash(
                   6214:                     'rol'  => "Role",
1.113     raeburn  6215:                     'grs'  => "Section",
1.88      raeburn  6216:                     'exs'  => "Existing sections",
                   6217:                     'new'  => "Define new section", 
                   6218:                     'sta'  => "Start",
                   6219:                     'end'  => "End",
                   6220:                     'ssd'  => "Set Start Date",
1.355   ! www      6221:                     'sed'  => "Set End Date",
        !          6222:                     'scc'  => "Course/Community"
1.88      raeburn  6223:                   );
1.323     raeburn  6224:     my $header = '<h4>'.&mt('Course/Community Level').'</h4>'.
1.136     raeburn  6225:                  &Apache::loncommon::start_data_table().
                   6226:                  &Apache::loncommon::start_data_table_header_row().
1.355   ! www      6227:                  '<th>'.$lt{'scc'}.'</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
1.136     raeburn  6228:                  &Apache::loncommon::end_data_table_header_row();
1.143     raeburn  6229:     my $otheritems = &Apache::loncommon::start_data_table_row()."\n".
1.355   ! www      6230:                      '<td><br /><input type="text" size="40" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'cu','dccourse','dcdomain','coursedesc','','','','crstype'".')" />'.
        !          6231:                      $courseform.'</td>'."\n".
1.323     raeburn  6232:                      '<td valign><br /><select name="role">'."\n";
1.213     raeburn  6233:     foreach my $role (@roles) {
1.135     raeburn  6234:         my $plrole=&Apache::lonnet::plaintext($role);
                   6235:         $otheritems .= '  <option value="'.$role.'">'.$plrole;
1.88      raeburn  6236:     }
                   6237:     if ( keys %customroles > 0) {
1.135     raeburn  6238:         foreach my $cust (sort keys %customroles) {
1.101     albertel 6239:             my $custrole='cr_cr_'.$env{'user.domain'}.
1.135     raeburn  6240:                     '_'.$env{'user.name'}.'_'.$cust;
                   6241:             $otheritems .= '  <option value="'.$custrole.'">'.$cust;
1.88      raeburn  6242:         }
                   6243:     }
                   6244:     $otheritems .= '</select></td><td>'.
                   6245:                      '<table border="0" cellspacing="0" cellpadding="0">'.
                   6246:                      '<tr><td valign="top"><b>'.$lt{'exs'}.'</b><br /><select name="currsec">'.
                   6247:                      ' <option value=""><--'.&mt('Pick course first').'</select></td>'.
                   6248:                      '<td>&nbsp;&nbsp;</td>'.
                   6249:                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
1.113     raeburn  6250:                      '<input type="text" name="newsec" value="" />'.
1.237     raeburn  6251:                      '<input type="hidden" name="section" value="" />'.
1.323     raeburn  6252:                      '<input type="hidden" name="groups" value="" />'.
                   6253:                      '<input type="hidden" name="crstype" value="" /></td>'.
1.88      raeburn  6254:                      '</tr></table></td>';
                   6255:     $otheritems .= <<ENDTIMEENTRY;
1.323     raeburn  6256: <td><br /><input type="hidden" name="start" value='' />
1.88      raeburn  6257: <a href=
                   6258: "javascript:pjump('date_start','Start Date',document.cu.start.value,'start','cu.pres','dateset')">$lt{'ssd'}</a></td>
1.323     raeburn  6259: <td><br /><input type="hidden" name="end" value='' />
1.88      raeburn  6260: <a href=
                   6261: "javascript:pjump('date_end','End Date',document.cu.end.value,'end','cu.pres','dateset')">$lt{'sed'}</a></td>
                   6262: ENDTIMEENTRY
1.136     raeburn  6263:     $otheritems .= &Apache::loncommon::end_data_table_row().
                   6264:                    &Apache::loncommon::end_data_table()."\n";
1.88      raeburn  6265:     return $cb_jscript.$header.$hiddenitems.$otheritems;
                   6266: }
                   6267: 
1.237     raeburn  6268: sub update_selfenroll_config {
1.241     raeburn  6269:     my ($r,$context,$permission) = @_;
1.237     raeburn  6270:     my ($row,$lt) = &get_selfenroll_titles();
1.241     raeburn  6271:     my %curr_groups = &Apache::longroup::coursegroups();
1.237     raeburn  6272:     my (%changes,%warning);
                   6273:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   6274:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.241     raeburn  6275:     my $curr_types;
1.237     raeburn  6276:     if (ref($row) eq 'ARRAY') {
                   6277:         foreach my $item (@{$row}) {
                   6278:             if ($item eq 'enroll_dates') {
                   6279:                 my (%currenrolldate,%newenrolldate);
                   6280:                 foreach my $type ('start','end') {
                   6281:                     $currenrolldate{$type} = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$type.'_date'};
                   6282:                     $newenrolldate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_date');
                   6283:                     if ($newenrolldate{$type} ne $currenrolldate{$type}) {
                   6284:                         $changes{'internal.selfenroll_'.$type.'_date'} = $newenrolldate{$type};
                   6285:                     }
                   6286:                 }
                   6287:             } elsif ($item eq 'access_dates') {
                   6288:                 my (%currdate,%newdate);
                   6289:                 foreach my $type ('start','end') {
                   6290:                     $currdate{$type} = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$type.'_access'};
                   6291:                     $newdate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_access');
                   6292:                     if ($newdate{$type} ne $currdate{$type}) {
                   6293:                         $changes{'internal.selfenroll_'.$type.'_access'} = $newdate{$type};
                   6294:                     }
                   6295:                 }
1.241     raeburn  6296:             } elsif ($item eq 'types') {
                   6297:                 $curr_types =
                   6298:                     $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$item};
                   6299:                 if ($env{'form.selfenroll_all'}) {
                   6300:                     if ($curr_types ne '*') {
                   6301:                         $changes{'internal.selfenroll_types'} = '*';
                   6302:                     } else {
                   6303:                         next;
                   6304:                     }
                   6305:                 } else {
1.249     raeburn  6306:                     my %currdoms;
1.241     raeburn  6307:                     my @entries = split(/;/,$curr_types);
                   6308:                     my @deletedoms = &Apache::loncommon::get_env_multiple('form.selfenroll_delete');
1.249     raeburn  6309:                     my @activations = &Apache::loncommon::get_env_multiple('form.selfenroll_activate');
1.241     raeburn  6310:                     my $newnum = 0;
1.249     raeburn  6311:                     my @latesttypes;
                   6312:                     foreach my $num (@activations) {
                   6313:                         my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$num);
                   6314:                         if (@types > 0) {
1.241     raeburn  6315:                             @types = sort(@types);
                   6316:                             my $typestr = join(',',@types);
1.249     raeburn  6317:                             my $typedom = $env{'form.selfenroll_dom_'.$num};
                   6318:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   6319:                             $currdoms{$typedom} = 1;
1.241     raeburn  6320:                             $newnum ++;
                   6321:                         }
                   6322:                     }
1.338     raeburn  6323:                     for (my $j=0; $j<$env{'form.selfenroll_types_total'}; $j++) {
                   6324:                         if ((!grep(/^$j$/,@deletedoms)) && (!grep(/^$j$/,@activations))) {
1.249     raeburn  6325:                             my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$j);
                   6326:                             if (@types > 0) {
                   6327:                                 @types = sort(@types);
                   6328:                                 my $typestr = join(',',@types);
                   6329:                                 my $typedom = $env{'form.selfenroll_dom_'.$j};
                   6330:                                 $latesttypes[$newnum] = $typedom.':'.$typestr;
                   6331:                                 $currdoms{$typedom} = 1;
                   6332:                                 $newnum ++;
                   6333:                             }
                   6334:                         }
                   6335:                     }
                   6336:                     if ($env{'form.selfenroll_newdom'} ne '') {
                   6337:                         my $typedom = $env{'form.selfenroll_newdom'};
                   6338:                         if ((!defined($currdoms{$typedom})) && 
                   6339:                             (&Apache::lonnet::domain($typedom) ne '')) {
                   6340:                             my $typestr;
                   6341:                             my ($othertitle,$usertypes,$types) = 
                   6342:                                 &Apache::loncommon::sorted_inst_types($typedom);
                   6343:                             my $othervalue = 'any';
                   6344:                             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
                   6345:                                 if (@{$types} > 0) {
1.257     raeburn  6346:                                     my @esc_types = map { &escape($_); } @{$types};
1.249     raeburn  6347:                                     $othervalue = 'other';
1.258     raeburn  6348:                                     $typestr = join(',',(@esc_types,$othervalue));
1.249     raeburn  6349:                                 }
                   6350:                                 $typestr = $othervalue;
                   6351:                             } else {
                   6352:                                 $typestr = $othervalue;
                   6353:                             } 
                   6354:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   6355:                             $newnum ++ ;
                   6356:                         }
                   6357:                     }
1.241     raeburn  6358:                     my $selfenroll_types = join(';',@latesttypes);
                   6359:                     if ($selfenroll_types ne $curr_types) {
                   6360:                         $changes{'internal.selfenroll_types'} = $selfenroll_types;
                   6361:                     }
                   6362:                 }
1.276     raeburn  6363:             } elsif ($item eq 'limit') {
                   6364:                 my $newlimit = $env{'form.selfenroll_limit'};
                   6365:                 my $newcap = $env{'form.selfenroll_cap'};
                   6366:                 $newcap =~s/\s+//g;
                   6367:                 my $currlimit =  $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_limit'};
                   6368:                 $currlimit = 'none' if ($currlimit eq '');
                   6369:                 my $currcap = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_cap'};
                   6370:                 if ($newlimit ne $currlimit) {
                   6371:                     if ($newlimit ne 'none') {
                   6372:                         if ($newcap =~ /^\d+$/) {
                   6373:                             if ($newcap ne $currcap) {
                   6374:                                 $changes{'internal.selfenroll_cap'} = $newcap;
                   6375:                             }
                   6376:                             $changes{'internal.selfenroll_limit'} = $newlimit;
                   6377:                         } else {
                   6378:                             $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.'); 
                   6379:                         }
                   6380:                     } elsif ($currcap ne '') {
                   6381:                         $changes{'internal.selfenroll_cap'} = '';
                   6382:                         $changes{'internal.selfenroll_limit'} = $newlimit; 
                   6383:                     }
                   6384:                 } elsif ($currlimit ne 'none') {
                   6385:                     if ($newcap =~ /^\d+$/) {
                   6386:                         if ($newcap ne $currcap) {
                   6387:                             $changes{'internal.selfenroll_cap'} = $newcap;
                   6388:                         }
                   6389:                     } else {
                   6390:                         $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.');
                   6391:                     }
                   6392:                 }
                   6393:             } elsif ($item eq 'approval') {
                   6394:                 my (@currnotified,@newnotified);
                   6395:                 my $currapproval = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'};
                   6396:                 my $currnotifylist = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_notifylist'};
                   6397:                 if ($currnotifylist ne '') {
                   6398:                     @currnotified = split(/,/,$currnotifylist);
                   6399:                     @currnotified = sort(@currnotified);
                   6400:                 }
                   6401:                 my $newapproval = $env{'form.selfenroll_approval'};
                   6402:                 @newnotified = &Apache::loncommon::get_env_multiple('form.selfenroll_notify');
                   6403:                 @newnotified = sort(@newnotified);
                   6404:                 if ($newapproval ne $currapproval) {
                   6405:                     $changes{'internal.selfenroll_approval'} = $newapproval;
                   6406:                     if (!$newapproval) {
                   6407:                         if ($currnotifylist ne '') {
                   6408:                             $changes{'internal.selfenroll_notifylist'} = '';
                   6409:                         }
                   6410:                     } else {
                   6411:                         my @differences =  
1.295     raeburn  6412:                             &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  6413:                         if (@differences > 0) {
                   6414:                             if (@newnotified > 0) {
                   6415:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   6416:                             } else {
                   6417:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   6418:                             }
                   6419:                         }
                   6420:                     }
                   6421:                 } else {
1.295     raeburn  6422:                     my @differences = &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  6423:                     if (@differences > 0) {
                   6424:                         if (@newnotified > 0) {
                   6425:                             $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   6426:                         } else {
                   6427:                             $changes{'internal.selfenroll_notifylist'} = '';
                   6428:                         }
                   6429:                     }
                   6430:                 }
1.237     raeburn  6431:             } else {
                   6432:                 my $curr_val = 
                   6433:                     $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$item};
                   6434:                 my $newval = $env{'form.selfenroll_'.$item};
                   6435:                 if ($item eq 'section') {
                   6436:                     $newval = $env{'form.sections'};
1.241     raeburn  6437:                     if (defined($curr_groups{$newval})) {
1.237     raeburn  6438:                         $newval = $curr_val;
                   6439:                         $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');
                   6440:                     } elsif ($newval eq 'all') {
                   6441:                         $newval = $curr_val;
1.274     bisitz   6442:                         $warning{$item} = &mt('Section for self-enrolled users unchanged, as "all" is a reserved section name.');
1.237     raeburn  6443:                     }
                   6444:                     if ($newval eq '') {
                   6445:                         $newval = 'none';
                   6446:                     }
                   6447:                 }
                   6448:                 if ($newval ne $curr_val) {
                   6449:                     $changes{'internal.selfenroll_'.$item} = $newval;
                   6450:                 }
1.241     raeburn  6451:             }
1.237     raeburn  6452:         }
                   6453:         if (keys(%warning) > 0) {
                   6454:             foreach my $item (@{$row}) {
                   6455:                 if (exists($warning{$item})) {
                   6456:                     $r->print($warning{$item}.'<br />');
                   6457:                 }
                   6458:             } 
                   6459:         }
                   6460:         if (keys(%changes) > 0) {
                   6461:             my $putresult = &Apache::lonnet::put('environment',\%changes,$cdom,$cnum);
                   6462:             if ($putresult eq 'ok') {
                   6463:                 if ((exists($changes{'internal.selfenroll_types'})) ||
                   6464:                     (exists($changes{'internal.selfenroll_start_date'}))  ||
                   6465:                     (exists($changes{'internal.selfenroll_end_date'}))) {
                   6466:                     my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',
                   6467:                                                                 $cnum,undef,undef,'Course');
                   6468:                     my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
                   6469:                     if (ref($crsinfo{$env{'request.course.id'}}) eq 'HASH') {
                   6470:                         foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date') {
                   6471:                             if (exists($changes{'internal.'.$item})) {
                   6472:                                 $crsinfo{$env{'request.course.id'}}{$item} = 
                   6473:                                     $changes{'internal.'.$item};
                   6474:                             }
                   6475:                         }
                   6476:                         my $crsputresult =
                   6477:                             &Apache::lonnet::courseidput($cdom,\%crsinfo,
                   6478:                                                          $chome,'notime');
                   6479:                     }
                   6480:                 }
                   6481:                 $r->print(&mt('The following changes were made to self-enrollment settings:').'<ul>');
                   6482:                 foreach my $item (@{$row}) {
                   6483:                     my $title = $item;
                   6484:                     if (ref($lt) eq 'HASH') {
                   6485:                         $title = $lt->{$item};
                   6486:                     }
                   6487:                     if ($item eq 'enroll_dates') {
                   6488:                         foreach my $type ('start','end') {
                   6489:                             if (exists($changes{'internal.selfenroll_'.$type.'_date'})) {
                   6490:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_date'});
1.244     bisitz   6491:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  6492:                                           $title,$type,$newdate).'</li>');
                   6493:                             }
                   6494:                         }
                   6495:                     } elsif ($item eq 'access_dates') {
                   6496:                         foreach my $type ('start','end') {
                   6497:                             if (exists($changes{'internal.selfenroll_'.$type.'_access'})) {
                   6498:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_access'});
1.244     bisitz   6499:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  6500:                                           $title,$type,$newdate).'</li>');
                   6501:                             }
                   6502:                         }
1.276     raeburn  6503:                     } elsif ($item eq 'limit') {
                   6504:                         if ((exists($changes{'internal.selfenroll_limit'})) ||
                   6505:                             (exists($changes{'internal.selfenroll_cap'}))) {
                   6506:                             my ($newval,$newcap);
                   6507:                             if ($changes{'internal.selfenroll_cap'} ne '') {
                   6508:                                 $newcap = $changes{'internal.selfenroll_cap'}
                   6509:                             } else {
                   6510:                                 $newcap = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_cap'};
                   6511:                             }
                   6512:                             if ($changes{'internal.selfenroll_limit'} eq 'none') {
                   6513:                                 $newval = &mt('No limit');
                   6514:                             } elsif ($changes{'internal.selfenroll_limit'} eq 
                   6515:                                      'allstudents') {
                   6516:                                 $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   6517:                             } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
                   6518:                                 $newval = &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
                   6519:                             } else {
                   6520:                                 my $currlimit =  $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_limit'};
                   6521:                                 if ($currlimit eq 'allstudents') {
                   6522:                                     $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   6523:                                 } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
1.308     raeburn  6524:                                     $newval =  &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
1.276     raeburn  6525:                                 }
                   6526:                             }
                   6527:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
                   6528:                         }
                   6529:                     } elsif ($item eq 'approval') {
                   6530:                         if ((exists($changes{'internal.selfenroll_approval'})) ||
                   6531:                             (exists($changes{'internal.selfenroll_notifylist'}))) {
                   6532:                             my ($newval,$newnotify);
                   6533:                             if (exists($changes{'internal.selfenroll_notifylist'})) {
                   6534:                                 $newnotify = $changes{'internal.selfenroll_notifylist'};
                   6535:                             } else {   
                   6536:                                 $newnotify = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_notifylist'};
                   6537:                             }
                   6538:                             if ($changes{'internal.selfenroll_approval'}) {
                   6539:                                 $newval = &mt('Yes');
                   6540:                             } elsif ($changes{'internal.selfenroll_approval'} eq '0') {
                   6541:                                 $newval = &mt('No');
                   6542:                             } else {
                   6543:                                 my $currapproval = 
                   6544:                                     $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'};
                   6545:                                 if ($currapproval) {
                   6546:                                     $newval = &mt('Yes');
                   6547:                                 } else {
                   6548:                                     $newval = &mt('No');
                   6549:                                 }
                   6550:                             }
                   6551:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval));
                   6552:                             if ($newnotify) {
1.277     raeburn  6553:                                 $r->print('<br />'.&mt('The following will be notified when an enrollment request needs approval, or has been approved: [_1].',$newnotify));
1.276     raeburn  6554:                             } else {
1.277     raeburn  6555:                                 $r->print('<br />'.&mt('No notifications sent when an enrollment request needs approval, or has been approved.'));
1.276     raeburn  6556:                             }
                   6557:                             $r->print('</li>'."\n");
                   6558:                         }
1.237     raeburn  6559:                     } else {
                   6560:                         if (exists($changes{'internal.selfenroll_'.$item})) {
1.241     raeburn  6561:                             my $newval = $changes{'internal.selfenroll_'.$item};
                   6562:                             if ($item eq 'types') {
                   6563:                                 if ($newval eq '') {
                   6564:                                     $newval = &mt('None');
                   6565:                                 } elsif ($newval eq '*') {
                   6566:                                     $newval = &mt('Any user in any domain');
                   6567:                                 }
1.245     raeburn  6568:                             } elsif ($item eq 'registered') {
                   6569:                                 if ($newval eq '1') {
                   6570:                                     $newval = &mt('Yes');
                   6571:                                 } elsif ($newval eq '0') {
                   6572:                                     $newval = &mt('No');
                   6573:                                 }
1.241     raeburn  6574:                             }
1.244     bisitz   6575:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
1.237     raeburn  6576:                         }
                   6577:                     }
                   6578:                 }
                   6579:                 $r->print('</ul>');
                   6580:                 my %newenvhash;
                   6581:                 foreach my $key (keys(%changes)) {
                   6582:                     $newenvhash{'course.'.$env{'request.course.id'}.'.'.$key} = $changes{$key};
                   6583:                 }
1.238     raeburn  6584:                 &Apache::lonnet::appenv(\%newenvhash);
1.237     raeburn  6585:             } else {
                   6586:                 $r->print(&mt('An error occurred when saving changes to self-enrollment settings in this course.').'<br />'.&mt('The error was: [_1].',$putresult));
                   6587:             }
                   6588:         } else {
1.249     raeburn  6589:             $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.237     raeburn  6590:         }
                   6591:     } else {
1.249     raeburn  6592:         $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.241     raeburn  6593:     }
1.256     raeburn  6594:     my ($visible,$cansetvis,$vismsgs,$visactions) = &visible_in_cat($cdom,$cnum);
                   6595:     if (ref($visactions) eq 'HASH') {
                   6596:         if (!$visible) {
                   6597:             $r->print('<br />'.$visactions->{'miss'}.'<br />'.$visactions->{'yous'}.
                   6598:                       '<br />');
                   6599:             if (ref($vismsgs) eq 'ARRAY') {
                   6600:                 $r->print('<br />'.$visactions->{'take'}.'<ul>');
                   6601:                 foreach my $item (@{$vismsgs}) {
                   6602:                     $r->print('<li>'.$visactions->{$item}.'</li>');
                   6603:                 }
                   6604:                 $r->print('</ul>');
                   6605:             }
                   6606:             $r->print($cansetvis);
                   6607:         }
                   6608:     } 
1.237     raeburn  6609:     return;
                   6610: }
                   6611: 
                   6612: sub get_selfenroll_titles {
1.276     raeburn  6613:     my @row = ('types','registered','enroll_dates','access_dates','section',
                   6614:                'approval','limit');
1.237     raeburn  6615:     my %lt = &Apache::lonlocal::texthash (
                   6616:                 types        => 'Users allowed to self-enroll in this course',
1.245     raeburn  6617:                 registered   => 'Restrict self-enrollment to students officially registered for the course',
1.237     raeburn  6618:                 enroll_dates => 'Dates self-enrollment available',
1.256     raeburn  6619:                 access_dates => 'Course access dates assigned to self-enrolling users',
                   6620:                 section      => 'Section assigned to self-enrolling users',
1.276     raeburn  6621:                 approval     => 'Self-enrollment requests need approval?',
                   6622:                 limit        => 'Enrollment limit',
1.237     raeburn  6623:              );
                   6624:     return (\@row,\%lt);
                   6625: }
                   6626: 
1.329     raeburn  6627: sub is_courseowner {
                   6628:     my ($thiscourse,$courseowner) = @_;
                   6629:     if ($courseowner eq '') {
                   6630:         if ($env{'request.course.id'} eq $thiscourse) {
                   6631:             $courseowner = $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
                   6632:         }
                   6633:     }
                   6634:     if ($courseowner ne '') {
                   6635:         if ($courseowner eq $env{'user.name'}.':'.$env{'user.domain'}) {
1.334     raeburn  6636:             return 1;
1.329     raeburn  6637:         }
                   6638:     }
                   6639:     return;
                   6640: }
                   6641: 
1.27      matthew  6642: #---------------------------------------------- end functions for &phase_two
1.29      matthew  6643: 
                   6644: #--------------------------------- functions for &phase_two and &phase_three
                   6645: 
                   6646: #--------------------------end of functions for &phase_two and &phase_three
1.1       www      6647: 
                   6648: 1;
                   6649: __END__
1.2       www      6650: 
                   6651: 

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