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

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

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