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

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

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