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

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

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