Annotation of loncom/interface/lonuserutils.pm, revision 1.97.2.5

1.1       raeburn     1: # The LearningOnline Network with CAPA
                      2: # Utility functions for managing LON-CAPA user accounts
                      3: #
1.97.2.5! raeburn     4: # $Id: lonuserutils.pm,v 1.97.2.4 2010/01/20 17:41:25 raeburn Exp $
1.1       raeburn     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: # /home/httpd/html/adm/gpl.txt
                     24: #
                     25: # http://www.lon-capa.org/
                     26: #
                     27: #
                     28: ###############################################################
                     29: ###############################################################
                     30: 
                     31: package Apache::lonuserutils;
                     32: 
                     33: use strict;
                     34: use Apache::lonnet;
                     35: use Apache::loncommon();
                     36: use Apache::lonhtmlcommon;
                     37: use Apache::lonlocal;
1.8       raeburn    38: use Apache::longroup;
                     39: use LONCAPA qw(:DEFAULT :match);
1.1       raeburn    40: 
                     41: ###############################################################
                     42: ###############################################################
                     43: # Drop student from all sections of a course, except optional $csec
                     44: sub modifystudent {
1.52      raeburn    45:     my ($udom,$unam,$courseid,$csec,$desiredhost,$context)=@_;
1.1       raeburn    46:     # if $csec is undefined, drop the student from all the courses matching
                     47:     # this one.  If $csec is defined, drop them from all other sections of
                     48:     # this course and add them to section $csec
1.17      raeburn    49:     my ($cnum,$cdom) = &get_course_identity($courseid);
1.1       raeburn    50:     my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
                     51:     my ($tmp) = keys(%roles);
                     52:     # Bail out if we were unable to get the students roles
                     53:     return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
                     54:     # Go through the roles looking for enrollment in this course
                     55:     my $result = '';
                     56:     foreach my $course (keys(%roles)) {
                     57:         if ($course=~m{^/\Q$cdom\E/\Q$cnum\E(?:\/)*(?:\s+)*(\w+)*\_st$}) {
                     58:             # We are in this course
                     59:             my $section=$1;
                     60:             $section='' if ($course eq "/$cdom/$cnum".'_st');
                     61:             if (defined($csec) && $section eq $csec) {
                     62:                 $result .= 'ok:';
                     63:             } elsif ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
                     64:                 my (undef,$end,$start)=split(/\_/,$roles{$course});
                     65:                 my $now=time;
                     66:                 # if this is an active role
                     67:                 if (!($start && ($now<$start)) || !($end && ($now>$end))) {
                     68:                     my $reply=&Apache::lonnet::modifystudent
                     69:                         # dom  name  id mode pass     f     m     l     g
                     70:                         ($udom,$unam,'',  '',  '',undef,undef,undef,undef,
1.22      raeburn    71:                          $section,time,undef,undef,$desiredhost,'','manual',
1.52      raeburn    72:                          '',$courseid,'',$context);
1.1       raeburn    73:                     $result .= $reply.':';
                     74:                 }
                     75:             }
                     76:         }
                     77:     }
                     78:     if ($result eq '') {
1.37      raeburn    79:         $result = &mt('Unable to find section for this student');
1.1       raeburn    80:     } else {
                     81:         $result =~ s/(ok:)+/ok/g;
                     82:     }
                     83:     return $result;
                     84: }
                     85: 
                     86: sub modifyuserrole {
                     87:     my ($context,$setting,$changeauth,$cid,$udom,$uname,$uid,$umode,$upass,
                     88:         $first,$middle,$last,$gene,$sec,$forceid,$desiredhome,$email,$role,
1.84      raeburn    89:         $end,$start,$checkid,$inststatus) = @_;
1.5       raeburn    90:     my ($scope,$userresult,$authresult,$roleresult,$idresult);
1.1       raeburn    91:     if ($setting eq 'course' || $context eq 'course') {
                     92:         $scope = '/'.$cid;
                     93:         $scope =~ s/\_/\//g;
                     94:         if ($role ne 'cc' && $sec ne '') {
                     95:             $scope .='/'.$sec;
                     96:         }
1.5       raeburn    97:     } elsif ($context eq 'domain') {
1.1       raeburn    98:         $scope = '/'.$env{'request.role.domain'}.'/';
1.13      raeburn    99:     } elsif ($context eq 'author') {
1.1       raeburn   100:         $scope =  '/'.$env{'user.domain'}.'/'.$env{'user.name'};
                    101:     }
                    102:     if ($context eq 'domain') {
                    103:         my $uhome = &Apache::lonnet::homeserver($uname,$udom);
                    104:         if ($uhome ne 'no_host') {
1.5       raeburn   105:             if (($changeauth eq 'Yes') && (&Apache::lonnet::allowed('mau',$udom))) {
1.1       raeburn   106:                 if ((($umode =~ /^krb4|krb5|internal$/) && $upass ne '') ||
                    107:                     ($umode eq 'localauth')) {
                    108:                     $authresult = &Apache::lonnet::modifyuserauth($udom,$uname,$umode,$upass);
                    109:                 }
                    110:             }
1.5       raeburn   111:             if (($forceid) && (&Apache::lonnet::allowed('mau',$udom)) &&
                    112:                 ($env{'form.recurseid'}) && ($checkid)) {
                    113:                 my %userupdate = (
                    114:                                   lastname   => $last,
                    115:                                   middlename => $middle,
                    116:                                   firstname  => $first,
                    117:                                   generation => $gene,
                    118:                                   id         => $uid,
                    119:                                  );
                    120:                 $idresult = &propagate_id_change($uname,$udom,\%userupdate);
                    121:             }
1.1       raeburn   122:         }
                    123:     }
                    124:     $userresult =
                    125:         &Apache::lonnet::modifyuser($udom,$uname,$uid,$umode,$upass,$first,
                    126:                                     $middle,$last,$gene,$forceid,$desiredhome,
1.84      raeburn   127:                                     $email,$inststatus);
1.1       raeburn   128:     if ($userresult eq 'ok') {
1.5       raeburn   129:         if ($role ne '') {
1.22      raeburn   130:             $role =~ s/_/\//g;
1.1       raeburn   131:             $roleresult = &Apache::lonnet::assignrole($udom,$uname,$scope,
1.52      raeburn   132:                                                       $role,$end,$start,'',
                    133:                                                       '',$context);
1.1       raeburn   134:         }
                    135:     }
1.5       raeburn   136:     return ($userresult,$authresult,$roleresult,$idresult);
1.1       raeburn   137: }
                    138: 
1.5       raeburn   139: sub propagate_id_change {
                    140:     my ($uname,$udom,$user) = @_;
1.12      raeburn   141:     my (@types,@roles);
1.5       raeburn   142:     @types = ('active','future');
                    143:     @roles = ('st');
                    144:     my $idresult;
                    145:     my %roleshash = &Apache::lonnet::get_my_roles($uname,
1.12      raeburn   146:                         $udom,'userroles',\@types,\@roles);
                    147:     my %args = (
                    148:                 one_time => 1,
                    149:                );
1.5       raeburn   150:     foreach my $item (keys(%roleshash)) {
1.22      raeburn   151:         my ($cnum,$cdom,$role) = split(/:/,$item,-1);
1.5       raeburn   152:         my ($start,$end) = split(/:/,$roleshash{$item});
                    153:         if (&Apache::lonnet::is_course($cdom,$cnum)) {
1.12      raeburn   154:             my $result = &update_classlist($cdom,$cnum,$udom,$uname,$user);
                    155:             my %coursehash = 
                    156:                 &Apache::lonnet::coursedescription($cdom.'_'.$cnum,\%args);
                    157:             my $cdesc = $coursehash{'description'};
                    158:             if ($cdesc eq '') { 
                    159:                 $cdesc = $cdom.'_'.$cnum;
                    160:             }
1.5       raeburn   161:             if ($result eq 'ok') {
1.12      raeburn   162:                 $idresult .= &mt('Classlist update for "[_1]" in "[_2]".',$uname.':'.$udom,$cdesc).'<br />'."\n";
1.5       raeburn   163:             } else {
1.12      raeburn   164:                 $idresult .= &mt('Error: "[_1]" during classlist update for "[_2]" in "[_3]".',$result,$uname.':'.$udom,$cdesc).'<br />'."\n";
1.5       raeburn   165:             }
                    166:         }
                    167:     }
                    168:     return $idresult;
                    169: }
                    170: 
                    171: sub update_classlist {
1.63      raeburn   172:     my ($cdom,$cnum,$udom,$uname,$user,$newend) = @_;
1.6       albertel  173:     my ($uid,$classlistentry);
1.5       raeburn   174:     my $fullname =
                    175:         &Apache::lonnet::format_name($user->{'firstname'},$user->{'middlename'},
                    176:                                      $user->{'lastname'},$user->{'generation'},
                    177:                                      'lastname');
                    178:     my %classhash = &Apache::lonnet::get('classlist',[$uname.':'.$udom],
                    179:                                          $cdom,$cnum);
                    180:     my @classinfo = split(/:/,$classhash{$uname.':'.$udom});
                    181:     my $ididx=&Apache::loncoursedata::CL_ID() - 2;
                    182:     my $nameidx=&Apache::loncoursedata::CL_FULLNAME() - 2;
1.63      raeburn   183:     my $endidx = &Apache::loncoursedata::CL_END() - 2;
                    184:     my $startidx = &Apache::loncoursedata::CL_START() - 2;
1.5       raeburn   185:     for (my $i=0; $i<@classinfo; $i++) {
1.63      raeburn   186:         if ($i == $endidx) {
                    187:             if ($newend ne '') {
                    188:                 $classlistentry .= $newend.':';
                    189:             } else {
                    190:                 $classlistentry .= $classinfo[$i].':';
                    191:             }
                    192:         } elsif ($i == $startidx) {
                    193:             if ($newend ne '') {
                    194:                 if ($classinfo[$i] > $newend) {
                    195:                     $classlistentry .= $newend.':';
                    196:                 } else {
                    197:                     $classlistentry .= $classinfo[$i].':';
                    198:                 }
                    199:             } else {
                    200:                 $classlistentry .= $classinfo[$i].':';
                    201:             }
                    202:         } elsif ($i == $ididx) {
1.5       raeburn   203:             if (defined($user->{'id'})) {
                    204:                 $classlistentry .= $user->{'id'}.':';
                    205:             } else {
                    206:                 $classlistentry .= $classinfo[$i].':';
                    207:             }
                    208:         } elsif ($i == $nameidx) {
1.63      raeburn   209:             if (defined($user->{'lastname'})) {
                    210:                 $classlistentry .= $fullname.':';
                    211:             } else {
                    212:                 $classlistentry .= $classinfo[$i].':';
                    213:             }
1.5       raeburn   214:         } else {
                    215:             $classlistentry .= $classinfo[$i].':';
                    216:         }
                    217:     }
                    218:     $classlistentry =~ s/:$//;
                    219:     my $reply=&Apache::lonnet::cput('classlist',
                    220:                                     {"$uname:$udom" => $classlistentry},
                    221:                                     $cdom,$cnum);
                    222:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
                    223:         return 'ok';
                    224:     } else {
                    225:         return 'error: '.$reply;
                    226:     }
                    227: }
                    228: 
                    229: 
1.1       raeburn   230: ###############################################################
                    231: ###############################################################
1.2       raeburn   232: # build a role type and role selection form
                    233: sub domain_roles_select {
                    234:     # Set up the role type and role selection boxes when in 
                    235:     # domain context   
                    236:     #
                    237:     # Role types
1.97.2.4  raeburn   238:     my @roletypes = ('domain','author','course','community');
1.2       raeburn   239:     my %lt = &role_type_names();
1.1       raeburn   240:     #
                    241:     # build up the menu information to be passed to
                    242:     # &Apache::loncommon::linked_select_forms
                    243:     my %select_menus;
1.2       raeburn   244:     if ($env{'form.roletype'} eq '') {
                    245:         $env{'form.roletype'} = 'domain';
                    246:     }
                    247:     foreach my $roletype (@roletypes) {
1.1       raeburn   248:         # set up the text for this domain
1.2       raeburn   249:         $select_menus{$roletype}->{'text'}= $lt{$roletype};
1.97.2.5! raeburn   250:         my $crstype;
        !           251:         if ($roletype eq 'community') {
        !           252:             $crstype = 'Community';
        !           253:         }
1.1       raeburn   254:         # we want a choice of 'default' as the default in the second menu
1.2       raeburn   255:         if ($env{'form.roletype'} ne '') {
                    256:             $select_menus{$roletype}->{'default'} = $env{'form.showrole'};
                    257:         } else { 
                    258:             $select_menus{$roletype}->{'default'} = 'Any';
                    259:         }
1.1       raeburn   260:         # Now build up the other items in the second menu
1.2       raeburn   261:         my @roles;
                    262:         if ($roletype eq 'domain') {
                    263:             @roles = &domain_roles();
1.13      raeburn   264:         } elsif ($roletype eq 'author') {
1.2       raeburn   265:             @roles = &construction_space_roles();
                    266:         } else {
1.17      raeburn   267:             my $custom = 1;
1.97.2.4  raeburn   268:             @roles = &course_roles('domain',undef,$custom,$roletype);
1.1       raeburn   269:         }
1.2       raeburn   270:         my $order = ['Any',@roles];
                    271:         $select_menus{$roletype}->{'order'} = $order; 
                    272:         foreach my $role (@roles) {
1.5       raeburn   273:             if ($role eq 'cr') {
                    274:                 $select_menus{$roletype}->{'select2'}->{$role} =
                    275:                               &mt('Custom role');
                    276:             } else {
                    277:                 $select_menus{$roletype}->{'select2'}->{$role} = 
1.97.2.5! raeburn   278:                               &Apache::lonnet::plaintext($role,$crstype);
1.5       raeburn   279:             }
1.2       raeburn   280:         }
                    281:         $select_menus{$roletype}->{'select2'}->{'Any'} = &mt('Any');
1.1       raeburn   282:     }
1.2       raeburn   283:     my $result = &Apache::loncommon::linked_select_forms
                    284:         ('studentform',('&nbsp;'x3).&mt('Role: '),$env{'form.roletype'},
1.97.2.4  raeburn   285:          'roletype','showrole',\%select_menus,
                    286:          ['domain','author','course','community']);
1.1       raeburn   287:     return $result;
                    288: }
                    289: 
                    290: ###############################################################
                    291: ###############################################################
                    292: sub hidden_input {
                    293:     my ($name,$value) = @_;
                    294:     return '<input type="hidden" name="'.$name.'" value="'.$value.'" />'."\n";
                    295: }
                    296: 
                    297: sub print_upload_manager_header {
1.22      raeburn   298:     my ($r,$datatoken,$distotal,$krbdefdom,$context,$permission)=@_;
1.1       raeburn   299:     my $javascript;
                    300:     #
                    301:     if (! exists($env{'form.upfile_associate'})) {
                    302:         $env{'form.upfile_associate'} = 'forward';
                    303:     }
                    304:     if ($env{'form.associate'} eq 'Reverse Association') {
                    305:         if ( $env{'form.upfile_associate'} ne 'reverse' ) {
                    306:             $env{'form.upfile_associate'} = 'reverse';
                    307:         } else {
                    308:             $env{'form.upfile_associate'} = 'forward';
                    309:         }
                    310:     }
                    311:     if ($env{'form.upfile_associate'} eq 'reverse') {
                    312:         $javascript=&upload_manager_javascript_reverse_associate();
                    313:     } else {
                    314:         $javascript=&upload_manager_javascript_forward_associate();
                    315:     }
                    316:     #
                    317:     # Deal with restored settings
                    318:     my $password_choice = '';
                    319:     if (exists($env{'form.ipwd_choice'}) &&
                    320:         $env{'form.ipwd_choice'} ne '') {
                    321:         # If a column was specified for password, assume it is for an
                    322:         # internal password.  This is a bug waiting to be filed (could be
                    323:         # local or krb auth instead of internal) but I do not have the
                    324:         # time to mess around with this now.
                    325:         $password_choice = 'int';
                    326:     }
                    327:     #
1.22      raeburn   328:     my $groupslist;
                    329:     if ($context eq 'course') {
                    330:         $groupslist = &get_groupslist();
                    331:     }
1.1       raeburn   332:     my $javascript_validations =
1.22      raeburn   333:         &javascript_validations('upload',$krbdefdom,$password_choice,undef,
                    334:                                 $env{'request.role.domain'},$context,
1.33      raeburn   335:                                 $groupslist);
1.91      bisitz    336:     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
1.95      bisitz    337:     $r->print('<p>'
                    338:              .&mt('Total number of records found in file: [_1]'
                    339:                  ,'<b>'.$distotal.'</b>')
                    340:              ."</p>\n");
1.1       raeburn   341:     $r->print('<div class="LC_left_float"><h3>'.
                    342:               &mt('Identify fields in uploaded list')."</h3>\n");
                    343:     $r->print(&mt('Enter as many fields as you can.<br /> The system will inform you and bring you back to this page, <br /> if the data selected are insufficient to add users.')."<br />\n");
                    344:     $r->print(&hidden_input('action','upload').
                    345:               &hidden_input('state','got_file').
                    346:               &hidden_input('associate','').
                    347:               &hidden_input('datatoken',$datatoken).
                    348:               &hidden_input('fileupload',$env{'form.fileupload'}).
                    349:               &hidden_input('upfiletype',$env{'form.upfiletype'}).
                    350:               &hidden_input('upfile_associate',$env{'form.upfile_associate'}));
1.86      bisitz    351:     $r->print('<br /><label><input type="checkbox" name="noFirstLine"'.$checked.' />'.
1.73      bisitz    352:               &mt('Ignore First Line').'</label><br />');
1.59      bisitz    353:     $r->print('<br /><input type="button" value="'.&mt('Reverse Association').'" '.
                    354:               'name="Reverse Association" '.
1.96      bisitz    355:               'onclick="javascript:this.form.associate.value=\'Reverse Association\';submit(this.form);" />');
1.1       raeburn   356:     $r->print("<br /><br />\n".
                    357:               '<script type="text/javascript" language="Javascript">'."\n".
1.96      bisitz    358:               '// <![CDATA['."\n".
                    359:               $javascript."\n".$javascript_validations."\n".
                    360:               '// ]]>'."\n".
                    361:               '</script>');
1.1       raeburn   362: }
                    363: 
                    364: ###############################################################
                    365: ###############################################################
                    366: sub javascript_validations {
1.22      raeburn   367:     my ($mode,$krbdefdom,$curr_authtype,$curr_authfield,$domain,
1.33      raeburn   368:         $context,$groupslist)=@_;
1.22      raeburn   369:     my %param = (
                    370:                   kerb_def_dom => $krbdefdom,
                    371:                   curr_authtype => $curr_authtype,
                    372:                 );
1.37      raeburn   373:     if ($mode eq 'upload') {
1.22      raeburn   374:         $param{'formname'} = 'studentform';
1.1       raeburn   375:     } elsif ($mode eq 'createcourse') {
1.22      raeburn   376:         $param{'formname'} = 'ccrs';
1.1       raeburn   377:     } elsif ($mode eq 'modifycourse') {
1.22      raeburn   378:         $param{'formname'} = 'cmod';
                    379:         $param{'mode'} = 'modifycourse',
                    380:         $param{'curr_autharg'} = $curr_authfield;
                    381:     }
                    382: 
                    383:     my ($setsection_call,$setsections_js);
                    384:     my $finish = "  vf.submit();\n";
                    385:     if ($mode eq 'upload') {
                    386:         if (($context eq 'course') || ($context eq 'domain')) {
                    387:             if ($context eq 'course') {
                    388:                 if ($env{'request.course.sec'} eq '') {
                    389:                     $setsection_call = 'setSections(document.'.$param{'formname'}.');';
                    390:                     $setsections_js =
                    391:                         &setsections_javascript($param{'formname'},$groupslist,
                    392:                                                 $mode);
                    393:                 } else {
                    394:                     $setsection_call = "'ok'";
                    395:                 }
                    396:             } elsif ($context eq 'domain') {
                    397:                 $setsection_call = 'setCourse()';
1.37      raeburn   398:                 $setsections_js = &dc_setcourse_js($param{'formname'},$mode,$context);
1.22      raeburn   399:             }
                    400:             $finish = "  var checkSec = $setsection_call\n".
                    401:                       "  if (checkSec == 'ok') {\n".
                    402:                       "      vf.submit();\n".
                    403:                       "   }\n";
                    404:         }
1.1       raeburn   405:     }
1.22      raeburn   406:     my $authheader = &Apache::loncommon::authform_header(%param);
1.1       raeburn   407: 
                    408:     my %alert = &Apache::lonlocal::texthash
                    409:         (username => 'You need to specify the username field.',
                    410:          authen   => 'You must choose an authentication type.',
                    411:          krb      => 'You need to specify the Kerberos domain.',
                    412:          ipass    => 'You need to specify the initial password.',
                    413:          name     => 'The optional name field was not specified.',
1.93      bisitz    414:          snum     => 'The optional student/employee ID field was not specified.',
1.1       raeburn   415:          section  => 'The optional section field was not specified.',
1.75      schafran  416:          email    => 'The optional e-mail address field was not specified.',
1.1       raeburn   417:          role     => 'The optional role field was not specified.',
1.57      raeburn   418:          domain   => 'The optional domain field was not specified.',
1.1       raeburn   419:          continue => 'Continue adding users?',
                    420:          );
1.84      raeburn   421:     if (($mode eq 'upload') && ($context eq 'domain')) {
                    422:         $alert{'inststatus'} = &mt('The optional affiliation field was not specified'); 
                    423:     }
1.37      raeburn   424:     my $function_name = <<"END";
1.22      raeburn   425: $setsections_js
                    426: 
1.84      raeburn   427: function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail,foundrole,founddomain,foundinststatus) {
1.1       raeburn   428: END
                    429:     my ($authnum,%can_assign) =  &Apache::loncommon::get_assignable_auth($domain);
                    430:     my $auth_checks;
                    431:     if ($mode eq 'createcourse') {
                    432:         $auth_checks .= (<<END);
                    433:     if (vf.autoadds[0].checked == true) {
                    434:         if (current.radiovalue == null || current.radiovalue == 'nochange') {
                    435:             alert('$alert{'authen'}');
                    436:             return;
                    437:         }
                    438:     }
                    439: END
                    440:     } else {
                    441:         $auth_checks .= (<<END);
                    442:     var foundatype=0;
                    443:     if (founduname==0) {
                    444:         alert('$alert{'username'}');
                    445:         return;
                    446:     }
                    447: 
                    448: END
                    449:         if ($authnum > 1) {
                    450:             $auth_checks .= (<<END);
                    451:     if (current.radiovalue == null || current.radiovalue == '' || current.radiovalue == 'nochange') {
                    452:         // They did not check any of the login radiobuttons.
                    453:         alert('$alert{'authen'}');
                    454:         return;
                    455:     }
                    456: END
                    457:         }
                    458:     }
                    459:     if ($mode eq 'createcourse') {
                    460:         $auth_checks .= "
                    461:     if ( (vf.autoadds[0].checked == true) &&
                    462:          (vf.elements[current.argfield].value == null || vf.elements[current.argfield].value == '') ) {
                    463: ";
                    464:     } elsif ($mode eq 'modifycourse') {
                    465:         $auth_checks .= "
                    466:     if (vf.elements[current.argfield].value == null || vf.elements[current.argfield].value == '') {
                    467: ";
                    468:     }
                    469:     if ( ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
                    470:         $auth_checks .= (<<END);
                    471:         var alertmsg = '';
                    472:         switch (current.radiovalue) {
                    473:             case 'krb':
                    474:                 alertmsg = '$alert{'krb'}';
                    475:                 break;
                    476:             default:
                    477:                 alertmsg = '';
                    478:         }
                    479:         if (alertmsg != '') {
                    480:             alert(alertmsg);
                    481:             return;
                    482:         }
                    483:     }
                    484: END
                    485:     } else {
                    486:         $auth_checks .= (<<END);
                    487:     foundatype=1;
                    488:     if (current.argfield == null || current.argfield == '') {
                    489:         var alertmsg = '';
1.38      raeburn   490:         switch (current.radiovalue) {
1.1       raeburn   491:             case 'krb':
                    492:                 alertmsg = '$alert{'krb'}';
                    493:                 break;
                    494:             case 'loc':
                    495:             case 'fsys':
                    496:                 alertmsg = '$alert{'ipass'}';
                    497:                 break;
                    498:             case 'fsys':
                    499:                 alertmsg = '';
                    500:                 break;
                    501:             default:
                    502:                 alertmsg = '';
                    503:         }
                    504:         if (alertmsg != '') {
                    505:             alert(alertmsg);
                    506:             return;
                    507:         }
                    508:     }
                    509: END
                    510:     }
                    511:     my $section_checks;
                    512:     my $optional_checks = '';
                    513:     if ( ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
                    514:         $optional_checks = (<<END);
                    515:     vf.submit();
                    516: }
                    517: END
                    518:     } else {
                    519:         $section_checks = &section_check_js();
                    520:         $optional_checks = (<<END);
                    521:     var message='';
                    522:     if (foundname==0) {
                    523:         message='$alert{'name'}';
                    524:     }
                    525:     if (foundid==0) {
                    526:         if (message!='') {
                    527:             message+='\\n';
                    528:         }
                    529:         message+='$alert{'snum'}';
                    530:     }
                    531:     if (foundsec==0) {
                    532:         if (message!='') {
                    533:             message+='\\n';
                    534:         }
                    535:     }
                    536:     if (foundemail==0) {
                    537:         if (message!='') {
                    538:             message+='\\n';
                    539:         }
                    540:         message+='$alert{'email'}';
                    541:     }
1.57      raeburn   542:     if (foundrole==0) {
                    543:         if (message!='') {
                    544:             message+='\\n';
                    545:         }
                    546:         message+='$alert{'role'}';
                    547:     }
                    548:     if (founddomain==0) {
                    549:         if (message!='') {
                    550:             message+='\\n';
                    551:         }
                    552:         message+='$alert{'domain'}';
                    553:     }
1.84      raeburn   554: END
                    555:         if (($mode eq 'upload') && ($context eq 'domain')) {
                    556:             $optional_checks .= (<<END);
                    557: 
                    558:     if (foundinststatus==0) {
                    559:         if (message!='') {
                    560:             message+='\\n';
                    561:         }
                    562:         message+='$alert{'inststatus'}';
                    563:     }
                    564: END
                    565:         }
                    566:         $optional_checks .= (<<END);
                    567: 
1.1       raeburn   568:     if (message!='') {
                    569:         message+= '\\n$alert{'continue'}';
                    570:         if (confirm(message)) {
                    571:             vf.state.value='enrolling';
1.22      raeburn   572:             $finish
1.1       raeburn   573:         }
                    574:     } else {
                    575:         vf.state.value='enrolling';
1.22      raeburn   576:         $finish
1.1       raeburn   577:     }
                    578: }
                    579: END
                    580:     }
1.37      raeburn   581:     my $result = $function_name.$auth_checks.$optional_checks."\n".
                    582:                  $section_checks.$authheader;
1.1       raeburn   583:     return $result;
                    584: }
                    585: ###############################################################
                    586: ###############################################################
                    587: sub upload_manager_javascript_forward_associate {
                    588:     return(<<ENDPICK);
                    589: function verify(vf,sec_caller) {
                    590:     var founduname=0;
                    591:     var foundpwd=0;
                    592:     var foundname=0;
                    593:     var foundid=0;
                    594:     var foundsec=0;
                    595:     var foundemail=0;
                    596:     var foundrole=0;
1.57      raeburn   597:     var founddomain=0;
1.84      raeburn   598:     var foundinststatus=0;
1.1       raeburn   599:     var tw;
                    600:     for (i=0;i<=vf.nfields.value;i++) {
                    601:         tw=eval('vf.f'+i+'.selectedIndex');
                    602:         if (tw==1) { founduname=1; }
                    603:         if ((tw>=2) && (tw<=6)) { foundname=1; }
                    604:         if (tw==7) { foundid=1; }
                    605:         if (tw==8) { foundsec=1; }
                    606:         if (tw==9) { foundpwd=1; }
                    607:         if (tw==10) { foundemail=1; }
                    608:         if (tw==11) { foundrole=1; }
1.57      raeburn   609:         if (tw==12) { founddomain=1; }
1.84      raeburn   610:         if (tw==13) { foundinststatus=1; }
1.1       raeburn   611:     }
1.84      raeburn   612:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail,foundrole,founddomain,foundinststatus);
1.1       raeburn   613: }
                    614: 
                    615: //
                    616: // vf = this.form
                    617: // tf = column number
                    618: //
                    619: // values of nw
                    620: //
                    621: // 0 = none
                    622: // 1 = username
                    623: // 2 = names (lastname, firstnames)
                    624: // 3 = fname (firstname)
                    625: // 4 = mname (middlename)
                    626: // 5 = lname (lastname)
                    627: // 6 = gen   (generation)
                    628: // 7 = id
                    629: // 8 = section
                    630: // 9 = ipwd  (password)
                    631: // 10 = email address
                    632: // 11 = role
1.57      raeburn   633: // 12 = domain
1.84      raeburn   634: // 13 = inststatus
1.1       raeburn   635: 
                    636: function flip(vf,tf) {
                    637:    var nw=eval('vf.f'+tf+'.selectedIndex');
                    638:    var i;
                    639:    // make sure no other columns are labeled the same as this one
                    640:    for (i=0;i<=vf.nfields.value;i++) {
                    641:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
                    642:           eval('vf.f'+i+'.selectedIndex=0;')
                    643:       }
                    644:    }
                    645:    // If we set this to 'lastname, firstnames', clear out all the ones
                    646:    // set to 'fname','mname','lname','gen' (3,4,5,6) currently.
                    647:    if (nw==2) {
                    648:       for (i=0;i<=vf.nfields.value;i++) {
                    649:          if ((eval('vf.f'+i+'.selectedIndex')>=3) &&
                    650:              (eval('vf.f'+i+'.selectedIndex')<=6)) {
                    651:              eval('vf.f'+i+'.selectedIndex=0;')
                    652:          }
                    653:       }
                    654:    }
                    655:    // If we set this to one of 'fname','mname','lname','gen' (3,4,5,6),
                    656:    // clear out any that are set to 'lastname, firstnames' (2)
                    657:    if ((nw>=3) && (nw<=6)) {
                    658:       for (i=0;i<=vf.nfields.value;i++) {
                    659:          if (eval('vf.f'+i+'.selectedIndex')==2) {
                    660:              eval('vf.f'+i+'.selectedIndex=0;')
                    661:          }
                    662:       }
                    663:    }
                    664:    // If we set the password, make the password form below correspond to
                    665:    // the new value.
                    666:    if (nw==9) {
                    667:        changed_radio('int',document.studentform);
                    668:        set_auth_radio_buttons('int',document.studentform);
                    669:        vf.intarg.value='';
                    670:        vf.krbarg.value='';
                    671:        vf.locarg.value='';
                    672:    }
                    673: }
                    674: 
                    675: function clearpwd(vf) {
                    676:     var i;
                    677:     for (i=0;i<=vf.nfields.value;i++) {
                    678:         if (eval('vf.f'+i+'.selectedIndex')==9) {
                    679:             eval('vf.f'+i+'.selectedIndex=0;')
                    680:         }
                    681:     }
                    682: }
                    683: 
                    684: ENDPICK
                    685: }
                    686: 
                    687: ###############################################################
                    688: ###############################################################
                    689: sub upload_manager_javascript_reverse_associate {
                    690:     return(<<ENDPICK);
                    691: function verify(vf,sec_caller) {
                    692:     var founduname=0;
                    693:     var foundpwd=0;
                    694:     var foundname=0;
                    695:     var foundid=0;
                    696:     var foundsec=0;
                    697:     var foundrole=0;
1.57      raeburn   698:     var founddomain=0;
1.84      raeburn   699:     var foundinststatus=0;
1.1       raeburn   700:     var tw;
                    701:     for (i=0;i<=vf.nfields.value;i++) {
                    702:         tw=eval('vf.f'+i+'.selectedIndex');
                    703:         if (i==0 && tw!=0) { founduname=1; }
                    704:         if (((i>=1) && (i<=5)) && tw!=0 ) { foundname=1; }
                    705:         if (i==6 && tw!=0) { foundid=1; }
                    706:         if (i==7 && tw!=0) { foundsec=1; }
                    707:         if (i==8 && tw!=0) { foundpwd=1; }
                    708:         if (i==9 && tw!=0) { foundrole=1; }
1.57      raeburn   709:         if (i==10 && tw!=0) { founddomain=1; }
1.84      raeburn   710:         if (i==13 && tw!=0) { foundinstatus=1; }
1.1       raeburn   711:     }
1.84      raeburn   712:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec,foundrole,founddomain,foundinststatus);
1.1       raeburn   713: }
                    714: 
                    715: function flip(vf,tf) {
                    716:    var nw=eval('vf.f'+tf+'.selectedIndex');
                    717:    var i;
                    718:    // picked the all one name field, reset the other name ones to blank
                    719:    if (tf==1 && nw!=0) {
                    720:       for (i=2;i<=5;i++) {
                    721:          eval('vf.f'+i+'.selectedIndex=0;')
                    722:       }
                    723:    }
                    724:    //picked one of the piecewise name fields, reset the all in
                    725:    //one field to blank
                    726:    if ((tf>=2) && (tf<=5) && (nw!=0)) {
                    727:       eval('vf.f1.selectedIndex=0;')
                    728:    }
                    729:    // intial password specified, pick internal authentication
                    730:    if (tf==8 && nw!=0) {
                    731:        changed_radio('int',document.studentform);
                    732:        set_auth_radio_buttons('int',document.studentform);
                    733:        vf.krbarg.value='';
                    734:        vf.intarg.value='';
                    735:        vf.locarg.value='';
                    736:    }
                    737: }
                    738: 
                    739: function clearpwd(vf) {
                    740:     var i;
                    741:     if (eval('vf.f8.selectedIndex')!=0) {
                    742:         eval('vf.f8.selectedIndex=0;')
                    743:     }
                    744: }
                    745: ENDPICK
                    746: }
                    747: 
                    748: ###############################################################
                    749: ###############################################################
                    750: sub print_upload_manager_footer {
1.97.2.4  raeburn   751:     my ($r,$i,$keyfields,$defdom,$today,$halfyear,$context,$permission,$crstype) = @_;
1.22      raeburn   752:     my $form = 'document.studentform';
                    753:     my $formname = 'studentform';
1.1       raeburn   754:     my ($krbdef,$krbdefdom) =
                    755:         &Apache::loncommon::get_kerberos_defaults($defdom);
1.22      raeburn   756:     my %param = ( formname => $form,
1.1       raeburn   757:                   kerb_def_dom => $krbdefdom,
                    758:                   kerb_def_auth => $krbdef
                    759:                   );
                    760:     if (exists($env{'form.ipwd_choice'}) &&
                    761:         defined($env{'form.ipwd_choice'}) &&
                    762:         $env{'form.ipwd_choice'} ne '') {
                    763:         $param{'curr_authtype'} = 'int';
                    764:     }
                    765:     my $krbform = &Apache::loncommon::authform_kerberos(%param);
                    766:     my $intform = &Apache::loncommon::authform_internal(%param);
                    767:     my $locform = &Apache::loncommon::authform_local(%param);
1.22      raeburn   768:     my $date_table = &date_setting_table(undef,undef,$context,undef,
1.97.2.4  raeburn   769:                                          $formname,$permission,$crstype);
1.95      bisitz    770: 
1.1       raeburn   771:     my $Str = "\n".'<div class="LC_left_float">';
                    772:     $Str .= &hidden_input('nfields',$i);
                    773:     $Str .= &hidden_input('keyfields',$keyfields);
1.95      bisitz    774: 
                    775:     $Str .= '<h3>'.&mt('Options').'</h3>'
                    776:            .&Apache::lonhtmlcommon::start_pick_box();
                    777: 
                    778:     $Str .= &Apache::lonhtmlcommon::row_title(&mt('Login Type'));
1.1       raeburn   779:     if ($context eq 'domain') {
1.95      bisitz    780:         $Str .= '<p>'
                    781:                .&mt('Change authentication for existing users in domain "[_1]" to these settings?'
                    782:                    ,$defdom)
                    783:                .'&nbsp;<span class="LC_nobreak"><label>'
                    784:                .'<input type="radio" name="changeauth" value="No" checked="checked" />'
                    785:                .&mt('No').'</label>'
                    786:                .'&nbsp;&nbsp;<label>'
                    787:                .'<input type="radio" name="changeauth" value="Yes" />'
                    788:                .&mt('Yes').'</label>'
                    789:                .'</span></p>'; 
1.1       raeburn   790:     } else {
1.95      bisitz    791:         $Str .= '<p class="LC_info">'."\n".
                    792:             &mt('This will not take effect if the user already exists.').
1.1       raeburn   793:             &Apache::loncommon::help_open_topic('Auth_Options').
                    794:             "</p>\n";
                    795:     }
1.97      raeburn   796:     $Str .= &set_login($defdom,$krbform,$intform,$locform);
1.95      bisitz    797: 
1.1       raeburn   798:     my ($home_server_pick,$numlib) =
                    799:         &Apache::loncommon::home_server_form_item($defdom,'lcserver',
                    800:                                                   'default','hide');
                    801:     if ($numlib > 1) {
1.97      raeburn   802:         $Str .= &Apache::lonhtmlcommon::row_closure()
                    803:                .&Apache::lonhtmlcommon::row_title(
1.95      bisitz    804:                     &mt('LON-CAPA Home Server for New Users'))
                    805:                .&mt('LON-CAPA domain: [_1] with home server:','"'.$defdom.'"')
                    806:                .$home_server_pick
                    807:                .&Apache::lonhtmlcommon::row_closure();
                    808:     } else {
1.97      raeburn   809:         $Str .= $home_server_pick.
                    810:                 &Apache::lonhtmlcommon::row_closure();
1.95      bisitz    811:     }
                    812: 
                    813:     $Str .= &Apache::lonhtmlcommon::row_title(&mt('Default domain'))
                    814:            .&Apache::loncommon::select_dom_form($defdom,'defaultdomain',undef,1)
                    815:            .&Apache::lonhtmlcommon::row_closure();
                    816: 
                    817:     $Str .= &Apache::lonhtmlcommon::row_title(&mt('Starting and Ending Dates'))
                    818:            ."<p>\n".$date_table."</p>\n"
                    819:            .&Apache::lonhtmlcommon::row_closure();
                    820: 
1.1       raeburn   821:     if ($context eq 'domain') {
1.95      bisitz    822:         $Str .= &Apache::lonhtmlcommon::row_title(
                    823:                     &mt('Settings for assigning roles'))
                    824:                .&mt('Pick the action to take on roles for these users:').'<br />'
                    825:                .'<span class="LC_nobreak"><label>'
                    826:                .'<input type="radio" name="roleaction" value="norole" checked="checked" />'
                    827:                .'&nbsp;'.&mt('No role changes').'</label>'
                    828:                .'&nbsp;&nbsp;&nbsp;<label>'
                    829:                .'<input type="radio" name="roleaction" value="domain" />'
                    830:                .'&nbsp;'.&mt('Add a domain role').'</label>'
                    831:                .'&nbsp;&nbsp;&nbsp;<label>'
                    832:                .'<input type="radio" name="roleaction" value="course" />'
                    833:                .'&nbsp;'.&mt('Add a course role').'</label>'
                    834:                .'</span>';
                    835:     } elsif ($context eq 'author') {
                    836:         $Str .= &Apache::lonhtmlcommon::row_title(
                    837:                     &mt('Default role'))
                    838:                .&mt('Choose the role to assign to users without a value specified in the uploaded file.')
1.1       raeburn   839:     } elsif ($context eq 'course') {
1.95      bisitz    840:         $Str .= &Apache::lonhtmlcommon::row_title(
                    841:                     &mt('Default role and section'))
                    842:                .&mt('Choose the role and/or section(s) to assign to users without values specified in the uploaded file.');
                    843:     } else {
                    844:         $Str .= &Apache::lonhtmlcommon::row_title(
                    845:                     &mt('Default role and/or section(s)'))
                    846:                .&mt('Role and/or section(s) for users without values specified in the uploaded file.');
1.1       raeburn   847:     }
1.22      raeburn   848:     if (($context eq 'domain') || ($context eq 'author')) {
1.95      bisitz    849:         $Str .= '<br />';
1.22      raeburn   850:         my ($options,$cb_script,$coursepick) = &default_role_selector($context,1);
                    851:         if ($context eq 'domain') {
1.95      bisitz    852:             $Str .= '<p>'
                    853:                    .'<b>'.&mt('Domain Level').'</b><br />'
                    854:                    .$options
                    855:                    .'</p><p>'
                    856:                    .'<b>'.&mt('Course Level').'</b>'
                    857:                    .'</p>'
                    858:                    .$cb_script.$coursepick
                    859:                    .&Apache::lonhtmlcommon::row_closure();
1.22      raeburn   860:         } elsif ($context eq 'author') {
1.95      bisitz    861:             $Str .= $options
                    862:                    .&Apache::lonhtmlcommon::row_closure(1); # last row in pick_box
1.22      raeburn   863:         }
1.1       raeburn   864:     } else {
1.22      raeburn   865:         my ($cnum,$cdom) = &get_course_identity();
                    866:         my $rowtitle = &mt('section');
                    867:         my $secbox = &section_picker($cdom,$cnum,'Any',$rowtitle,
1.97.2.4  raeburn   868:                                      $permission,$context,'upload',$crstype);
1.95      bisitz    869:         $Str .= $secbox
                    870:                .&Apache::lonhtmlcommon::row_closure();
1.97.2.4  raeburn   871:         my %lt;
                    872:         if ($crstype eq 'Community') {
                    873:             %lt = &Apache::lonlocal::texthash (
                    874:                     disp => 'Display members with current/future access who are not in the uploaded file',
                    875:                     stus => 'Members selected from this list can be dropped.'
                    876:             );
                    877:         } else {
                    878:             %lt = &Apache::lonlocal::texthash (
                    879:                     disp => 'Display students with current/future access who are not in the uploaded file',
                    880:                     stus => 'Students selected from this list can be dropped.'
                    881:             );
                    882:         }
1.95      bisitz    883:         $Str .= &Apache::lonhtmlcommon::row_title(&mt('Full Update'))
1.97.2.4  raeburn   884:                .'<label><input type="checkbox" name="fullup" value="yes" />'
                    885:                .' '.$lt{'disp'}
1.95      bisitz    886:                .'</label><br />'
1.97.2.4  raeburn   887:                .$lt{'stus'}
1.95      bisitz    888:                .&Apache::lonhtmlcommon::row_closure();
1.1       raeburn   889:     }
1.5       raeburn   890:     if ($context eq 'course' || $context eq 'domain') {
                    891:         $Str .= &forceid_change($context);
                    892:     }
1.95      bisitz    893: 
                    894:     $Str .= &Apache::lonhtmlcommon::end_pick_box();
1.73      bisitz    895:     $Str .= '</div>';
1.95      bisitz    896: 
                    897:     # Footer
                    898:     $Str .= '<div class="LC_clear_float_footer">'
                    899:            .'<hr />';
1.1       raeburn   900:     if ($context eq 'course') {
1.95      bisitz    901:         $Str .= '<p class="LC_info">'
1.73      bisitz    902:                .&mt('Note: For large courses, this operation may be time consuming.')
1.95      bisitz    903:                .'</p>';
1.73      bisitz    904:     }
1.95      bisitz    905:     $Str .= '<p><input type="button"'
1.96      bisitz    906:            .' onclick="javascript:verify(this.form,this.form.csec)"'
                    907:            .' value="'.&mt('Update Users').'" />'
1.95      bisitz    908:            .'</p>'."\n"
1.73      bisitz    909:            .'</div>';
1.1       raeburn   910:     $r->print($Str);
                    911:     return;
                    912: }
                    913: 
1.5       raeburn   914: sub forceid_change {
                    915:     my ($context) = @_;
                    916:     my $output = 
1.95      bisitz    917:         &Apache::lonhtmlcommon::row_title(&mt('Student/Employee ID'))
                    918:        .'<label><input type="checkbox" name="forceid" value="yes" />'
                    919:        .&mt('Disable Student/Employee ID Safeguard and force change of conflicting IDs')
                    920:        .'</label><br />'."\n"
                    921:        .&mt('(only do if you know what you are doing.)')."\n";
1.5       raeburn   922:     if ($context eq 'domain') {
1.25      raeburn   923:         $output .= '<br /><label><input type="checkbox" name="recurseid"'.
1.86      bisitz    924:                    ' value="yes" />'. 
1.93      bisitz    925:   &mt('Update student/employee ID in courses in which user is active/future student,[_1](if forcing change).','<br />').
1.25      raeburn   926:                    '</label>'."\n";
1.5       raeburn   927:     }
1.95      bisitz    928:     $output .= &Apache::lonhtmlcommon::row_closure(1); # last row in pick_box
1.5       raeburn   929:     return $output;
                    930: }
                    931: 
1.1       raeburn   932: ###############################################################
                    933: ###############################################################
                    934: sub print_upload_manager_form {
1.97.2.4  raeburn   935:     my ($r,$context,$permission,$crstype) = @_;
1.1       raeburn   936:     my $firstLine;
                    937:     my $datatoken;
                    938:     if (!$env{'form.datatoken'}) {
                    939:         $datatoken=&Apache::loncommon::upfile_store($r);
                    940:     } else {
                    941:         $datatoken=$env{'form.datatoken'};
                    942:         &Apache::loncommon::load_tmp_file($r);
                    943:     }
                    944:     my @records=&Apache::loncommon::upfile_record_sep();
                    945:     if($env{'form.noFirstLine'}){
                    946:         $firstLine=shift(@records);
                    947:     }
                    948:     my $total=$#records;
                    949:     my $distotal=$total+1;
                    950:     my $today=time;
                    951:     my $halfyear=$today+15552000;
                    952:     #
                    953:     # Restore memorized settings
                    954:     my $col_setting_names =  { 'username_choice' => 'scalar', # column settings
                    955:                                'names_choice' => 'scalar',
                    956:                                'fname_choice' => 'scalar',
                    957:                                'mname_choice' => 'scalar',
                    958:                                'lname_choice' => 'scalar',
                    959:                                'gen_choice' => 'scalar',
                    960:                                'id_choice' => 'scalar',
                    961:                                'sec_choice' => 'scalar',
                    962:                                'ipwd_choice' => 'scalar',
                    963:                                'email_choice' => 'scalar',
                    964:                                'role_choice' => 'scalar',
1.57      raeburn   965:                                'domain_choice' => 'scalar',
1.84      raeburn   966:                                'inststatus_choice' => 'scalar',
1.1       raeburn   967:                              };
                    968:     my $defdom = $env{'request.role.domain'};
                    969:     if ($context eq 'course') {
                    970:         &Apache::loncommon::restore_course_settings('enrollment_upload',
                    971:                                                     $col_setting_names);
                    972:     } else {
                    973:         &Apache::loncommon::restore_settings($context,'user_upload',
                    974:                                              $col_setting_names);
                    975:     }
                    976:     #
                    977:     # Determine kerberos parameters as appropriate
                    978:     my ($krbdef,$krbdefdom) =
                    979:         &Apache::loncommon::get_kerberos_defaults($defdom);
                    980:     #
1.22      raeburn   981:     &print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom,$context,
                    982:                                  $permission);
1.1       raeburn   983:     my $i;
                    984:     my $keyfields;
                    985:     if ($total>=0) {
                    986:         my @field=
                    987:             (['username',&mt('Username'),     $env{'form.username_choice'}],
                    988:              ['names',&mt('Last Name, First Names'),$env{'form.names_choice'}],
                    989:              ['fname',&mt('First Name'),      $env{'form.fname_choice'}],
                    990:              ['mname',&mt('Middle Names/Initials'),$env{'form.mname_choice'}],
                    991:              ['lname',&mt('Last Name'),       $env{'form.lname_choice'}],
                    992:              ['gen',  &mt('Generation'),      $env{'form.gen_choice'}],
1.61      bisitz    993:              ['id',   &mt('Student/Employee ID'),$env{'form.id_choice'}],
1.1       raeburn   994:              ['sec',  &mt('Section'),          $env{'form.sec_choice'}],
                    995:              ['ipwd', &mt('Initial Password'),$env{'form.ipwd_choice'}],
                    996:              ['email',&mt('E-mail Address'),   $env{'form.email_choice'}],
1.57      raeburn   997:              ['role',&mt('Role'),             $env{'form.role_choice'}],
1.84      raeburn   998:              ['domain',&mt('Domain'),         $env{'form.domain_choice'}],
                    999:              ['inststatus',&mt('Affiliation'), $env{'form.inststatus_choice'}]);
1.1       raeburn  1000:         if ($env{'form.upfile_associate'} eq 'reverse') {
                   1001:             &Apache::loncommon::csv_print_samples($r,\@records);
                   1002:             $i=&Apache::loncommon::csv_print_select_table($r,\@records,
                   1003:                                                           \@field);
                   1004:             foreach (@field) {
                   1005:                 $keyfields.=$_->[0].',';
                   1006:             }
                   1007:             chop($keyfields);
                   1008:         } else {
                   1009:             unshift(@field,['none','']);
                   1010:             $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
                   1011:                                                             \@field);
                   1012:             my %sone=&Apache::loncommon::record_sep($records[0]);
                   1013:             $keyfields=join(',',sort(keys(%sone)));
                   1014:         }
                   1015:     }
                   1016:     $r->print('</div>');
                   1017:     &print_upload_manager_footer($r,$i,$keyfields,$defdom,$today,$halfyear,
1.97.2.4  raeburn  1018:                                  $context,$permission,$crstype);
1.1       raeburn  1019: }
                   1020: 
                   1021: sub setup_date_selectors {
1.22      raeburn  1022:     my ($starttime,$endtime,$mode,$nolink,$formname) = @_;
                   1023:     if ($formname eq '') {
                   1024:         $formname = 'studentform';
                   1025:     }
1.1       raeburn  1026:     if (! defined($starttime)) {
                   1027:         $starttime = time;
                   1028:         unless ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
                   1029:             if (exists($env{'course.'.$env{'request.course.id'}.
                   1030:                             '.default_enrollment_start_date'})) {
                   1031:                 $starttime = $env{'course.'.$env{'request.course.id'}.
                   1032:                                   '.default_enrollment_start_date'};
                   1033:             }
                   1034:         }
                   1035:     }
                   1036:     if (! defined($endtime)) {
                   1037:         $endtime = time+(6*30*24*60*60); # 6 months from now, approx
                   1038:         unless ($mode eq 'createcourse') {
                   1039:             if (exists($env{'course.'.$env{'request.course.id'}.
                   1040:                             '.default_enrollment_end_date'})) {
                   1041:                 $endtime = $env{'course.'.$env{'request.course.id'}.
                   1042:                                 '.default_enrollment_end_date'};
                   1043:             }
                   1044:         }
                   1045:     }
1.11      raeburn  1046: 
                   1047:     my $startdateform = 
1.22      raeburn  1048:         &Apache::lonhtmlcommon::date_setter($formname,'startdate',$starttime,
1.11      raeburn  1049:             undef,undef,undef,undef,undef,undef,undef,$nolink);
                   1050: 
                   1051:     my $enddateform = 
1.22      raeburn  1052:         &Apache::lonhtmlcommon::date_setter($formname,'enddate',$endtime,
1.11      raeburn  1053:             undef,undef,undef,undef,undef,undef,undef,$nolink);
                   1054: 
1.1       raeburn  1055:     if ($mode eq 'create_enrolldates') {
                   1056:         $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                   1057:                                                             'startenroll',
                   1058:                                                             $starttime);
                   1059:         $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                   1060:                                                           'endenroll',
                   1061:                                                           $endtime);
                   1062:     }
                   1063:     if ($mode eq 'create_defaultdates') {
                   1064:         $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                   1065:                                                             'startaccess',
                   1066:                                                             $starttime);
                   1067:         $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                   1068:                                                           'endaccess',
                   1069:                                                           $endtime);
                   1070:     }
                   1071:     return ($startdateform,$enddateform);
                   1072: }
                   1073: 
                   1074: 
                   1075: sub get_dates_from_form {
1.54      raeburn  1076:     my ($startname,$endname) = @_;
                   1077:     if ($startname eq '') {
                   1078:         $startname = 'startdate';
                   1079:     }
                   1080:     if ($endname eq '') {
                   1081:         $endname = 'enddate';
                   1082:     }
                   1083:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form($startname);
                   1084:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form($endname);
1.1       raeburn  1085:     if ($env{'form.no_end_date'}) {
                   1086:         $enddate = 0;
                   1087:     }
                   1088:     return ($startdate,$enddate);
                   1089: }
                   1090: 
                   1091: sub date_setting_table {
1.97.2.4  raeburn  1092:     my ($starttime,$endtime,$mode,$bulkaction,$formname,$permission,$crstype) = @_;
1.11      raeburn  1093:     my $nolink;
                   1094:     if ($bulkaction) {
                   1095:         $nolink = 1;
                   1096:     }
                   1097:     my ($startform,$endform) = 
1.22      raeburn  1098:         &setup_date_selectors($starttime,$endtime,$mode,$nolink,$formname);
1.1       raeburn  1099:     my $dateDefault;
                   1100:     if ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
                   1101:         $dateDefault = '&nbsp;';
1.13      raeburn  1102:     } elsif ($mode ne 'author' && $mode ne 'domain') {
1.11      raeburn  1103:         if (($bulkaction eq 'reenable') || 
                   1104:             ($bulkaction eq 'activate') || 
1.22      raeburn  1105:             ($bulkaction eq 'chgdates') ||
                   1106:             ($env{'form.action'} eq 'upload')) {
                   1107:             if ($env{'request.course.sec'} eq '') {
                   1108:                 $dateDefault = '<span class="LC_nobreak">'.
1.97.2.4  raeburn  1109:                     '<label><input type="checkbox" name="makedatesdefault" value="1" /> ';
                   1110:                 if ($crstype eq 'Community') {
                   1111:                     $dateDefault .= &mt("make these dates the default access dates for future community enrollment");
                   1112:                 } else {
                   1113:                     $dateDefault .= &mt("make these dates the default access dates for future course enrollment");
                   1114:                 }
                   1115:                 $dateDefault .= '</label></span>';
1.22      raeburn  1116:             }
1.11      raeburn  1117:         }
1.1       raeburn  1118:     }
1.11      raeburn  1119:     my $perpetual = '<span class="LC_nobreak"><label><input type="checkbox" name="no_end_date"';
1.1       raeburn  1120:     if (defined($endtime) && $endtime == 0) {
1.70      bisitz   1121:         $perpetual .= ' checked="checked"';
1.1       raeburn  1122:     }
1.11      raeburn  1123:     $perpetual.= ' /> '.&mt('no ending date').'</label></span>';
1.1       raeburn  1124:     if ($mode eq 'create_enrolldates') {
                   1125:         $perpetual = '&nbsp;';
                   1126:     }
1.11      raeburn  1127:     my $result = &Apache::lonhtmlcommon::start_pick_box()."\n";
                   1128:     $result .= &Apache::lonhtmlcommon::row_title(&mt('Starting Date'),
                   1129:                                                      'LC_oddrow_value')."\n".
                   1130:                $startform."\n".
                   1131:                &Apache::lonhtmlcommon::row_closure(1).
                   1132:                &Apache::lonhtmlcommon::row_title(&mt('Ending Date'), 
                   1133:                                                      'LC_oddrow_value')."\n".
                   1134:                $endform.'&nbsp;'.$perpetual.
                   1135:                &Apache::lonhtmlcommon::row_closure(1).
1.22      raeburn  1136:                &Apache::lonhtmlcommon::end_pick_box();
1.1       raeburn  1137:     if ($dateDefault) {
                   1138:         $result .=  $dateDefault.'<br />'."\n";
                   1139:     }
                   1140:     return $result;
                   1141: }
                   1142: 
                   1143: sub make_dates_default {
1.97.2.4  raeburn  1144:     my ($startdate,$enddate,$context,$crstype = @_;
1.1       raeburn  1145:     my $result = '';
                   1146:     if ($context eq 'course') {
1.17      raeburn  1147:         my ($cnum,$cdom) = &get_course_identity();
1.1       raeburn  1148:         my $put_result = &Apache::lonnet::put('environment',
                   1149:                 {'default_enrollment_start_date'=>$startdate,
1.17      raeburn  1150:                  'default_enrollment_end_date'  =>$enddate},$cdom,$cnum);
1.1       raeburn  1151:         if ($put_result eq 'ok') {
1.97.2.4  raeburn  1152:             if ($crstype eq 'Community') {
                   1153:                 $result .= &mt('Set default start and end access dates for community.');
                   1154:             } else {
                   1155:                 $result .= &mt('Set default start and end access dates for course.');
                   1156:             }
                   1157:             $result .= '<br />'."\n";
1.1       raeburn  1158:             #
                   1159:             # Refresh the course environment
                   1160:             &Apache::lonnet::coursedescription($env{'request.course.id'},
                   1161:                                                {'freshen_cache' => 1});
                   1162:         } else {
1.97.2.4  raeburn  1163:             if ($crstype eq 'Community') {
                   1164:                 $result .= &mt('Unable to set default access dates for community');
                   1165:             } else {
                   1166:                 $result .= &mt('Unable to set default access dates for course');
                   1167:             }
                   1168:             $result .= ':'.$put_result.'<br />';
1.1       raeburn  1169:         }
                   1170:     }
                   1171:     return $result;
                   1172: }
                   1173: 
                   1174: sub default_role_selector {
1.97.2.4  raeburn  1175:     my ($context,$checkpriv,$crstype) = @_;
1.1       raeburn  1176:     my %customroles;
                   1177:     my ($options,$coursepick,$cb_jscript);
1.13      raeburn  1178:     if ($context ne 'author') {
1.1       raeburn  1179:         %customroles = &my_custom_roles();
                   1180:     }
                   1181: 
                   1182:     my %lt=&Apache::lonlocal::texthash(
                   1183:                     'rol'  => "Role",
                   1184:                     'grs'  => "Section",
                   1185:                     'exs'  => "Existing sections",
                   1186:                     'new'  => "New section",
                   1187:                   );
                   1188:     $options = '<select name="defaultrole">'."\n".
                   1189:                ' <option value="">'.&mt('Please select').'</option>'."\n"; 
                   1190:     if ($context eq 'course') {
1.97.2.4  raeburn  1191:         $options .= &default_course_roles($context,$checkpriv,$crstype,%customroles);
1.13      raeburn  1192:     } elsif ($context eq 'author') {
1.2       raeburn  1193:         my @roles = &construction_space_roles($checkpriv);
1.1       raeburn  1194:         foreach my $role (@roles) {
                   1195:            my $plrole=&Apache::lonnet::plaintext($role);
                   1196:            $options .= '  <option value="'.$role.'">'.$plrole.'</option>'."\n";
                   1197:         }
                   1198:     } elsif ($context eq 'domain') {
1.2       raeburn  1199:         my @roles = &domain_roles($checkpriv);
1.1       raeburn  1200:         foreach my $role (@roles) {
                   1201:            my $plrole=&Apache::lonnet::plaintext($role);
                   1202:            $options .= '  <option value="'.$role.'">'.$plrole.'</option>';
                   1203:         }
                   1204:         my $courseform = &Apache::loncommon::selectcourse_link
1.22      raeburn  1205:             ('studentform','dccourse','dcdomain','coursedesc',"$env{'request.role.domain'}",undef,'Course');
1.1       raeburn  1206:         $cb_jscript = 
1.22      raeburn  1207:             &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'},'currsec','studentform');
1.1       raeburn  1208:         $coursepick = &Apache::loncommon::start_data_table().
                   1209:                       &Apache::loncommon::start_data_table_header_row().
                   1210:                       '<th>'.$courseform.'</th><th>'.$lt{'rol'}.'</th>'.
                   1211:                       '<th>'.$lt{'grs'}.'</th>'.
                   1212:                       &Apache::loncommon::end_data_table_header_row().
                   1213:                       &Apache::loncommon::start_data_table_row()."\n".
1.96      bisitz   1214:                       '<td><input type="text" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'studentform','dccourse','dcdomain','coursedesc',''".')" /></td>'."\n".
1.1       raeburn  1215:                       '<td><select name="courserole">'."\n".
1.97.2.4  raeburn  1216:                       &default_course_roles($context,$checkpriv,'Course',%customroles)."\n".
1.1       raeburn  1217:                       '</select></td><td>'.
                   1218:                       '<table class="LC_createuser">'.
                   1219:                       '<tr class="LC_section_row"><td valign"top">'.
1.22      raeburn  1220:                       $lt{'exs'}.'<br /><select name="currsec">'.
1.1       raeburn  1221:                       ' <option value=""><--'.&mt('Pick course first').
                   1222:                       '</select></td>'.
                   1223:                       '<td>&nbsp;&nbsp;</td>'.
                   1224:                       '<td valign="top">'.$lt{'new'}.'<br />'.
                   1225:                       '<input type="text" name="newsec" value="" size="5" />'.
1.22      raeburn  1226:                       '<input type="hidden" name="groups" value="" />'.
                   1227:                       '<input type="hidden" name="sections" value="" />'.
                   1228:                       '<input type="hidden" name="origdom" value="'.
                   1229:                       $env{'request.role.domain'}.'" />'.
                   1230:                       '<input type="hidden" name="dccourse" value="" />'.
                   1231:                       '<input type="hidden" name="dcdomain" value="" />'.
                   1232:                       '</td></tr></table></td>'.
1.1       raeburn  1233:                       &Apache::loncommon::end_data_table_row().
1.22      raeburn  1234:                       &Apache::loncommon::end_data_table()."\n";
1.1       raeburn  1235:     }
                   1236:     $options .= '</select>';
                   1237:     return ($options,$cb_jscript,$coursepick);
                   1238: }
                   1239: 
                   1240: sub default_course_roles {
1.97.2.4  raeburn  1241:     my ($context,$checkpriv,$crstype,%customroles) = @_;
1.1       raeburn  1242:     my $output;
1.17      raeburn  1243:     my $custom = 1;
1.97.2.4  raeburn  1244:     my @roles = &course_roles($context,$checkpriv,$custom,lc($crstype));
1.1       raeburn  1245:     foreach my $role (@roles) {
1.22      raeburn  1246:         if ($role ne 'cr') {
1.97.2.4  raeburn  1247:             my $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.22      raeburn  1248:             $output .= '  <option value="'.$role.'">'.$plrole.'</option>';
                   1249:         }
1.1       raeburn  1250:     }
                   1251:     if (keys(%customroles) > 0) {
1.22      raeburn  1252:         if (grep(/^cr$/,@roles)) {
                   1253:             foreach my $cust (sort(keys(%customroles))) {
                   1254:                 my $custrole='cr_'.$env{'user.domain'}.
                   1255:                              '_'.$env{'user.name'}.'_'.$cust;
                   1256:                 $output .= '  <option value="'.$custrole.'">'.$cust.'</option>';
                   1257:             }
1.1       raeburn  1258:         }
                   1259:     }
                   1260:     return $output;
                   1261: }
                   1262: 
                   1263: sub construction_space_roles {
1.2       raeburn  1264:     my ($checkpriv) = @_;
1.17      raeburn  1265:     my @allroles = &roles_by_context('author');
1.1       raeburn  1266:     my @roles;
1.2       raeburn  1267:     if ($checkpriv) {
                   1268:         foreach my $role (@allroles) {
                   1269:             if (&Apache::lonnet::allowed('c'.$role,$env{'user.domain'}.'/'.$env{'user.name'})) { 
                   1270:                 push(@roles,$role); 
                   1271:             }
1.1       raeburn  1272:         }
1.2       raeburn  1273:         return @roles;
                   1274:     } else {
                   1275:         return @allroles;
1.1       raeburn  1276:     }
                   1277: }
                   1278: 
                   1279: sub domain_roles {
1.2       raeburn  1280:     my ($checkpriv) = @_;
1.17      raeburn  1281:     my @allroles = &roles_by_context('domain');
1.1       raeburn  1282:     my @roles;
1.2       raeburn  1283:     if ($checkpriv) {
                   1284:         foreach my $role (@allroles) {
                   1285:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) {
                   1286:                 push(@roles,$role);
                   1287:             }
1.1       raeburn  1288:         }
1.2       raeburn  1289:         return @roles;
                   1290:     } else {
                   1291:         return @allroles;
1.1       raeburn  1292:     }
                   1293: }
                   1294: 
                   1295: sub course_roles {
1.97.2.4  raeburn  1296:     my ($context,$checkpriv,$custom,$roletype) = @_;
1.97.2.5! raeburn  1297:     my $crstype;
        !          1298:     if ($roletype eq 'community') {
        !          1299:         $crstype = 'Community' ;
        !          1300:     } else {
        !          1301:         $crstype = 'Course';
        !          1302:     }
        !          1303:     my @allroles = &roles_by_context('course',$custom,$crstype);
1.1       raeburn  1304:     my @roles;
                   1305:     if ($context eq 'domain') {
                   1306:         @roles = @allroles;
                   1307:     } elsif ($context eq 'course') {
                   1308:         if ($env{'request.course.id'}) {
1.2       raeburn  1309:             if ($checkpriv) { 
                   1310:                 foreach my $role (@allroles) {
                   1311:                     if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   1312:                         push(@roles,$role);
                   1313:                     } else {
1.97.2.4  raeburn  1314:                         if ((($role ne 'cc') && ($role ne 'co')) && ($env{'request.course.sec'} ne '')) {
1.22      raeburn  1315:                             if (&Apache::lonnet::allowed('c'.$role,
1.2       raeburn  1316:                                              $env{'request.course.id'}.'/'.
1.22      raeburn  1317:                                              $env{'request.course.sec'})) {
1.2       raeburn  1318:                                 push(@roles,$role);
                   1319:                             }
1.1       raeburn  1320:                         }
                   1321:                     }
                   1322:                 }
1.2       raeburn  1323:             } else {
                   1324:                 @roles = @allroles;
1.1       raeburn  1325:             }
                   1326:         }
                   1327:     }
                   1328:     return @roles;
                   1329: }
                   1330: 
                   1331: sub curr_role_permissions {
1.97.2.4  raeburn  1332:     my ($context,$setting,$checkpriv,$type) = @_; 
1.17      raeburn  1333:     my $custom = 1;
1.1       raeburn  1334:     my @roles;
1.13      raeburn  1335:     if ($context eq 'author') {
1.2       raeburn  1336:         @roles = &construction_space_roles($checkpriv);
1.1       raeburn  1337:     } elsif ($context eq 'domain') {
                   1338:         if ($setting eq 'course') {
1.97.2.4  raeburn  1339:             @roles = &course_roles($context,$checkpriv,$custom,$type); 
1.1       raeburn  1340:         } else {
1.2       raeburn  1341:             @roles = &domain_roles($checkpriv);
1.1       raeburn  1342:         }
                   1343:     } elsif ($context eq 'course') {
1.97.2.4  raeburn  1344:         @roles = &course_roles($context,$checkpriv,$custom,$type);
1.1       raeburn  1345:     }
                   1346:     return @roles;
                   1347: }
                   1348: 
                   1349: # ======================================================= Existing Custom Roles
                   1350: 
                   1351: sub my_custom_roles {
                   1352:     my %returnhash=();
                   1353:     my %rolehash=&Apache::lonnet::dump('roles');
                   1354:     foreach my $key (keys %rolehash) {
                   1355:         if ($key=~/^rolesdef\_(\w+)$/) {
                   1356:             $returnhash{$1}=$1;
                   1357:         }
                   1358:     }
                   1359:     return %returnhash;
                   1360: }
                   1361: 
1.2       raeburn  1362: sub print_userlist {
                   1363:     my ($r,$mode,$permission,$context,$formname,$totcodes,$codetitles,
                   1364:         $idlist,$idlist_titles) = @_;
                   1365:     my $format = $env{'form.output'};
1.1       raeburn  1366:     if (! exists($env{'form.sortby'})) {
                   1367:         $env{'form.sortby'} = 'username';
                   1368:     }
1.2       raeburn  1369:     if ($env{'form.Status'} !~ /^(Any|Expired|Active|Future)$/) {
                   1370:         $env{'form.Status'} = 'Active';
1.1       raeburn  1371:     }
                   1372:     my $status_select = &Apache::lonhtmlcommon::StatusOptions
1.2       raeburn  1373:         ($env{'form.Status'});
1.1       raeburn  1374: 
1.2       raeburn  1375:     if ($env{'form.showrole'} eq '') {
1.13      raeburn  1376:         if ($context eq 'course') {
                   1377:             $env{'form.showrole'} = 'st';
                   1378:         } else {
                   1379:             $env{'form.showrole'} = 'Any';            
                   1380:         }
1.2       raeburn  1381:     }
1.1       raeburn  1382:     if (! defined($env{'form.output'}) ||
                   1383:         $env{'form.output'} !~ /^(csv|excel|html)$/ ) {
                   1384:         $env{'form.output'} = 'html';
                   1385:     }
                   1386: 
1.2       raeburn  1387:     my @statuses;
                   1388:     if ($env{'form.Status'} eq 'Any') {
                   1389:         @statuses = ('previous','active','future');
                   1390:     } elsif ($env{'form.Status'} eq 'Expired') {
                   1391:         @statuses = ('previous');
                   1392:     } elsif ($env{'form.Status'} eq 'Active') {
                   1393:         @statuses = ('active');
                   1394:     } elsif ($env{'form.Status'} eq 'Future') {
                   1395:         @statuses = ('future');
                   1396:     }
1.1       raeburn  1397: 
1.2       raeburn  1398: #    if ($context eq 'course') { 
                   1399: #        $r->print(&display_adv_courseroles());
                   1400: #    }
1.1       raeburn  1401:     #
                   1402:     # Interface output
1.2       raeburn  1403:     $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n".
                   1404:               '<input type="hidden" name="action" value="'.
1.1       raeburn  1405:               $env{'form.action'}.'" />');
                   1406:     $r->print("<p>\n");
                   1407:     if ($env{'form.action'} ne 'modifystudent') {
                   1408:         my %lt=&Apache::lonlocal::texthash('csv' => "CSV",
                   1409:                                            'excel' => "Excel",
                   1410:                                            'html'  => 'HTML');
                   1411:         my $output_selector = '<select size="1" name="output" >';
                   1412:         foreach my $outputformat ('html','csv','excel') {
1.96      bisitz   1413:             my $option = '<option value="'.$outputformat.'"';
1.1       raeburn  1414:             if ($outputformat eq $env{'form.output'}) {
1.96      bisitz   1415:                 $option .= ' selected="selected"';
1.1       raeburn  1416:             }
                   1417:             $option .='>'.$lt{$outputformat}.'</option>';
                   1418:             $output_selector .= "\n".$option;
                   1419:         }
                   1420:         $output_selector .= '</select>';
1.70      bisitz   1421:         $r->print('<label><span class="LC_nobreak">'
                   1422:                  .&mt('Output Format: [_1]',$output_selector)
                   1423:                  .'</span></label>'.('&nbsp;'x3));
                   1424:     }
                   1425:     $r->print('<label><span class="LC_nobreak">'
                   1426:              .&mt('User Status: [_1]',$status_select)
                   1427:              .'</span></label>'.('&nbsp;'x3)."\n");
1.2       raeburn  1428:     my $roleselected = '';
                   1429:     if ($env{'form.showrole'} eq 'Any') {
1.91      bisitz   1430:        $roleselected = ' selected="selected"'; 
1.2       raeburn  1431:     }
1.53      raeburn  1432:     my ($cnum,$cdom);
                   1433:     $r->print(&role_filter($context));
                   1434:     if ($context eq 'course') {
                   1435:         ($cnum,$cdom) = &get_course_identity();
                   1436:         $r->print(&section_group_filter($cnum,$cdom));
1.2       raeburn  1437:     }
1.78      raeburn  1438:     if ($env{'form.phase'} eq '') {
                   1439:         $r->print('<br /><br />'.&list_submit_button(&mt('Display List of Users')).
                   1440:                   "\n</p>\n".
                   1441:                   '<input type="hidden" name="phase" value="" /></form>');
                   1442:         return;
                   1443:     }
1.97.2.4  raeburn  1444:     if (!(($context eq 'domain') && 
                   1445:         (($env{'form.roletype'} eq 'course') || ($env{'form.roletype'} eq 'community')))) {
1.25      raeburn  1446:         $r->print('&nbsp;'.&list_submit_button(&mt('Update Display')).
                   1447:                   "\n</p>\n");
1.2       raeburn  1448:     }
                   1449:     my ($indexhash,$keylist) = &make_keylist_array();
1.97.2.4  raeburn  1450:     my (%userlist,%userinfo,$clearcoursepick);
                   1451:     if (($context eq 'domain') && 
                   1452:         ($env{'form.roletype'} eq 'course') ||
                   1453:         ($env{'form.roletype'} eq 'community')) {
                   1454:         my ($crstype,$numcodes,$title,$warning);
                   1455:         if ($env{'form.roletype'} eq 'course') {
                   1456:             $crstype = 'Course';
                   1457:             $numcodes = $totcodes;
                   1458:             $title = &mt('Select Courses');
                   1459:             $warning = &mt('Warning: data retrieval for multiple courses can take considerable time, as this operation is not currently optimized.');
                   1460:         } elsif ($env{'form.roletype'} eq 'community') {
                   1461:             $crstype = 'Community';
                   1462:             $numcodes = 0;
                   1463:             $title = &mt('Select Communities');
                   1464:             $warning = &mt('Warning: data retrieval for multiple communities can take considerable time, as this operation is not currently optimized.');
                   1465:         }
1.3       raeburn  1466:         my $courseform =
1.97.2.4  raeburn  1467:             &Apache::lonhtmlcommon::course_selection($formname,$numcodes,
                   1468:                             $codetitles,$idlist,$idlist_titles,$crstype);
1.3       raeburn  1469:         $r->print('<p>'.&Apache::lonhtmlcommon::start_pick_box()."\n".
                   1470:                   &Apache::lonhtmlcommon::start_pick_box()."\n".
1.97.2.4  raeburn  1471:                   &Apache::lonhtmlcommon::row_title($title,'LC_oddrow_value')."\n".
1.3       raeburn  1472:                   $courseform."\n".
                   1473:                   &Apache::lonhtmlcommon::row_closure(1).
                   1474:                   &Apache::lonhtmlcommon::end_pick_box().'</p>'.
1.97.2.4  raeburn  1475:                   '<p><input type="hidden" name="origroletype" value="'.$env{'form.roletype'}.'" />'.
                   1476:                   &list_submit_button(&mt('Update Display')).
                   1477:                   "\n".'</p><span class="LC_warning">'.$warning.'</span>'."\n");
                   1478:         $clearcoursepick = 0;
                   1479:         if (($env{'form.origroletype'} ne '') &&
                   1480:             ($env{'form.origroletype'} ne $env{'form.roletype'})) {
                   1481:             $clearcoursepick = 1;
                   1482:         }
                   1483:         if (($env{'form.coursepick'}) && (!$clearcoursepick)) {
1.11      raeburn  1484:             $r->print('<hr />'.&mt('Searching').' ...<br />&nbsp;<br />');
                   1485:         }
                   1486:     } else {
                   1487:         $r->print('<hr />'.&mt('Searching').' ...<br />&nbsp;<br />');
1.3       raeburn  1488:     }
                   1489:     $r->rflush();
1.1       raeburn  1490:     if ($context eq 'course') {
1.46      raeburn  1491:         if (($env{'form.showrole'} eq 'st') || ($env{'form.showrole'} eq 'Any')) { 
1.45      raeburn  1492:             my $classlist = &Apache::loncoursedata::get_classlist();
1.66      raeburn  1493:             if (ref($classlist) eq 'HASH') {
                   1494:                 %userlist = %{$classlist};
                   1495:             }
1.45      raeburn  1496:         }
1.43      raeburn  1497:         if ($env{'form.showrole'} ne 'st') {
                   1498:             my $showroles;
                   1499:             if ($env{'form.showrole'} ne 'Any') {
                   1500:                 $showroles = [$env{'form.showrole'}];
1.3       raeburn  1501:             } else {
1.43      raeburn  1502:                 $showroles = undef;
1.1       raeburn  1503:             }
1.43      raeburn  1504:             my $withsec = 1;
                   1505:             my $hidepriv = 1;
                   1506:             my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
                   1507:                               \@statuses,$showroles,undef,$withsec,$hidepriv);
                   1508:             &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
                   1509:                              \%advrolehash,$permission);
1.1       raeburn  1510:         }
1.2       raeburn  1511:     } else {
                   1512:         my (%cstr_roles,%dom_roles);
1.13      raeburn  1513:         if ($context eq 'author') {
1.2       raeburn  1514:             # List co-authors and assistant co-authors
1.17      raeburn  1515:             my @possroles = &roles_by_context($context);
1.2       raeburn  1516:             %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
                   1517:                                               \@statuses,\@possroles);
                   1518:             &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
1.11      raeburn  1519:                              \%cstr_roles,$permission);
1.2       raeburn  1520:         } elsif ($context eq 'domain') {
                   1521:             if ($env{'form.roletype'} eq 'domain') {
                   1522:                 %dom_roles = &Apache::lonnet::get_domain_roles($env{'request.role.domain'});
                   1523:                 foreach my $key (keys(%dom_roles)) {
                   1524:                     if (ref($dom_roles{$key}) eq 'HASH') {
                   1525:                         &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11      raeburn  1526:                                          \%userinfo,$dom_roles{$key},$permission);
1.2       raeburn  1527:                     }
                   1528:                 }
1.13      raeburn  1529:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  1530:                 my %dom_roles = &Apache::lonnet::get_domain_roles($env{'request.role.domain'},['au']);
                   1531:                 my %coauthors;
                   1532:                 foreach my $key (keys(%dom_roles)) {
                   1533:                     if (ref($dom_roles{$key}) eq 'HASH') {
                   1534:                         if ($env{'form.showrole'} eq 'au') {
                   1535:                             &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11      raeburn  1536:                                              \%userinfo,$dom_roles{$key},$permission);
1.2       raeburn  1537:                         } else {
                   1538:                             my @possroles;
                   1539:                             if ($env{'form.showrole'} eq 'Any') {
1.22      raeburn  1540:                                 @possroles = &roles_by_context('author');
1.2       raeburn  1541:                             } else {
                   1542:                                 @possroles = ($env{'form.showrole'}); 
                   1543:                             }
                   1544:                             foreach my $author (sort(keys(%{$dom_roles{$key}}))) {
1.22      raeburn  1545:                                 my ($role,$authorname,$authordom) = split(/:/,$author,-1);
1.2       raeburn  1546:                                 my $extent = '/'.$authordom.'/'.$authorname;
                   1547:                                 %{$coauthors{$extent}} =
                   1548:                                     &Apache::lonnet::get_my_roles($authorname,
                   1549:                                        $authordom,undef,\@statuses,\@possroles);
                   1550:                             }
                   1551:                             &gather_userinfo($context,$format,\%userlist,
1.11      raeburn  1552:                                      $indexhash,\%userinfo,\%coauthors,$permission);
1.2       raeburn  1553:                         }
                   1554:                     }
                   1555:                 }
                   1556:             } elsif ($env{'form.roletype'} eq 'course') {
1.97.2.4  raeburn  1557:                 if (($env{'form.coursepick'}) && (!$clearcoursepick)) {
1.2       raeburn  1558:                     my %courses = &process_coursepick();
1.39      raeburn  1559:                     my %allusers;
                   1560:                     my $hidepriv = 1;
1.2       raeburn  1561:                     foreach my $cid (keys(%courses)) {
1.17      raeburn  1562:                         my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
1.11      raeburn  1563:                         next if ($cnum eq '' || $cdom eq '');
1.17      raeburn  1564:                         my $custom = 1;
1.2       raeburn  1565:                         my (@roles,@sections,%access,%users,%userdata,
1.6       albertel 1566:                             %statushash);
1.2       raeburn  1567:                         if ($env{'form.showrole'} eq 'Any') {
1.97.2.4  raeburn  1568:                             @roles = &course_roles($context,undef,$custom,
                   1569:                                                    $env{'form.roletype'});
1.2       raeburn  1570:                         } else {
                   1571:                             @roles = ($env{'form.showrole'});
                   1572:                         }
                   1573:                         foreach my $role (@roles) {
                   1574:                             %{$users{$role}} = ();
                   1575:                         }
                   1576:                         foreach my $type (@statuses) {
                   1577:                             $access{$type} = $type;
                   1578:                         }
1.39      raeburn  1579:                         &Apache::loncommon::get_course_users($cdom,$cnum,\%access,\@roles,\@sections,\%users,\%userdata,\%statushash,$hidepriv);
1.2       raeburn  1580:                         foreach my $user (keys(%userdata)) {
                   1581:                             next if (ref($userinfo{$user}) eq 'HASH');
                   1582:                             foreach my $item ('fullname','id') {
                   1583:                                 $userinfo{$user}{$item} = $userdata{$user}[$indexhash->{$item}];
                   1584:                             }
                   1585:                         }
                   1586:                         foreach my $role (keys(%users)) {
                   1587:                             foreach my $user (keys(%{$users{$role}})) {
                   1588:                                 my $uniqid = $user.':'.$role;
                   1589:                                 $allusers{$uniqid}{$cid} = { desc => $cdesc,
                   1590:                                                              secs  => $statushash{$user}{$role},
                   1591:                                                            };
                   1592:                             }
                   1593:                         }
                   1594:                     }
                   1595:                     &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11      raeburn  1596:                                      \%userinfo,\%allusers,$permission);
1.2       raeburn  1597:                 } else {
1.10      raeburn  1598:                     $r->print('<input type="hidden" name="phase" value="'.
                   1599:                               $env{'form.phase'}.'" /></form>');
1.2       raeburn  1600:                     return;
                   1601:                 }
1.1       raeburn  1602:             }
                   1603:         }
1.3       raeburn  1604:     }
                   1605:     if (keys(%userlist) == 0) {
1.13      raeburn  1606:         if ($context eq 'author') {
1.3       raeburn  1607:             $r->print(&mt('There are no co-authors to display.')."\n");
                   1608:         } elsif ($context eq 'domain') {
                   1609:             if ($env{'form.roletype'} eq 'domain') {
                   1610:                 $r->print(&mt('There are no users with domain roles to display.')."\n");
1.13      raeburn  1611:             } elsif ($env{'form.roletype'} eq 'author') {
1.3       raeburn  1612:                 $r->print(&mt('There are no authors or co-authors to display.')."\n");
                   1613:             } elsif ($env{'form.roletype'} eq 'course') {
                   1614:                 $r->print(&mt('There are no course users to display')."\n"); 
1.97.2.4  raeburn  1615:             } elsif ($env{'form.roletype'} eq 'community') {
                   1616:                 $r->print(&mt('There are no community users to display')."\n");
1.2       raeburn  1617:             }
1.3       raeburn  1618:         } elsif ($context eq 'course') {
                   1619:             $r->print(&mt('There are no course users to display.')."\n");
                   1620:         }
                   1621:     } else {
                   1622:         # Print out the available choices
1.4       raeburn  1623:         my $usercount;
1.3       raeburn  1624:         if ($env{'form.action'} eq 'modifystudent') {
1.10      raeburn  1625:             ($usercount) = &show_users_list($r,$context,'view',$permission,
1.4       raeburn  1626:                                  $env{'form.Status'},\%userlist,$keylist);
1.1       raeburn  1627:         } else {
1.4       raeburn  1628:             ($usercount) = &show_users_list($r,$context,$env{'form.output'},
1.10      raeburn  1629:                                $permission,$env{'form.Status'},\%userlist,$keylist);
1.4       raeburn  1630:         }
                   1631:         if (!$usercount) {
1.72      bisitz   1632:             $r->print('<br /><span class="LC_warning">'
                   1633:                      .&mt('There are no users matching the search criteria.')
                   1634:                      .'</span>'
                   1635:             ); 
1.2       raeburn  1636:         }
                   1637:     }
1.10      raeburn  1638:     $r->print('<input type="hidden" name="phase" value="'.
                   1639:               $env{'form.phase'}.'" /></form>');
1.2       raeburn  1640: }
                   1641: 
1.53      raeburn  1642: sub role_filter {
                   1643:     my ($context) = @_;
                   1644:     my $output;
                   1645:     my $roleselected = '';
                   1646:     if ($env{'form.showrole'} eq 'Any') {
1.91      bisitz   1647:        $roleselected = ' selected="selected"';
1.53      raeburn  1648:     }
                   1649:     my ($role_select);
                   1650:     if ($context eq 'domain') {
                   1651:         $role_select = &domain_roles_select();
1.70      bisitz   1652:         $output = '<label><span class="LC_nobreak">'
                   1653:                  .&mt('Role Type: [_1]',$role_select)
                   1654:                  .'</span></label>';
1.53      raeburn  1655:     } else {
                   1656:         $role_select = '<select name="showrole">'."\n".
                   1657:                        '<option value="Any" '.$roleselected.'>'.
                   1658:                        &mt('Any role').'</option>';
1.97.2.4  raeburn  1659:         my ($roletype,$crstype);
                   1660:         if ($context eq 'course') {
                   1661:             $crstype = &Apache::loncommon::course_type();
                   1662:             if ($crstype eq 'Community') {
                   1663:                 $roletype = 'community';
                   1664:             } else {
                   1665:                 $roletype = 'course';
                   1666:             } 
                   1667:         }
                   1668:         my @poss_roles = &curr_role_permissions($context,'','',$roletype);
1.53      raeburn  1669:         foreach my $role (@poss_roles) {
                   1670:             $roleselected = '';
                   1671:             if ($role eq $env{'form.showrole'}) {
1.91      bisitz   1672:                 $roleselected = ' selected="selected"';
1.53      raeburn  1673:             }
                   1674:             my $plrole;
                   1675:             if ($role eq 'cr') {
                   1676:                 $plrole = &mt('Custom role');
                   1677:             } else {
1.97.2.4  raeburn  1678:                 $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.53      raeburn  1679:             }
                   1680:             $role_select .= '<option value="'.$role.'"'.$roleselected.'>'.$plrole.'</option>';
                   1681:         }
                   1682:         $role_select .= '</select>';
1.70      bisitz   1683:         $output = '<label><span class="LC_nobreak">'
                   1684:                  .&mt('Role: [_1]',$role_select)
                   1685:                  .'</span></label>';
1.53      raeburn  1686:     }
                   1687:     return $output;
                   1688: }
                   1689: 
1.33      raeburn  1690: sub section_group_filter {
                   1691:     my ($cnum,$cdom) = @_;
                   1692:     my @filters;
                   1693:     if ($env{'request.course.sec'} eq '') {
                   1694:         @filters = ('sec');
                   1695:     }
                   1696:     push(@filters,'grp');
                   1697:     my %name = (
                   1698:                  sec => 'secfilter',
                   1699:                  grp => 'grpfilter',
                   1700:                );
                   1701:     my %title = &Apache::lonlocal::texthash (
                   1702:                                               sec  => 'Section(s)',
                   1703:                                               grp  => 'Group(s)',
                   1704:                                               all  => 'all',
                   1705:                                               none => 'none',
                   1706:                                             );
1.47      raeburn  1707:     my $output;
1.33      raeburn  1708:     foreach my $item (@filters) {
1.47      raeburn  1709:         my ($markup,@options); 
1.33      raeburn  1710:         if ($env{'form.'.$name{$item}} eq '') {
                   1711:             $env{'form.'.$name{$item}} = 'all';
                   1712:         }
                   1713:         if ($item eq 'sec') {
                   1714:             if ($env{'form.showrole'} eq 'cc') {
                   1715:                 $env{'form.'.$name{$item}} = 'none';
                   1716:             }
                   1717:             my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   1718:             @options = sort(keys(%sections_count));
                   1719:         } elsif ($item eq 'grp') {
                   1720:             my %curr_groups = &Apache::longroup::coursegroups();
                   1721:             @options = sort(keys(%curr_groups));
                   1722:         }
                   1723:         if (@options > 0) {
                   1724:             my $currsel;
                   1725:             $markup = '<select name="'.$name{$item}.'" />'."\n";
                   1726:             foreach my $option ('all','none',@options) { 
                   1727:                 $currsel = '';
                   1728:                 if ($env{'form.'.$name{$item}} eq $option) {
1.96      bisitz   1729:                     $currsel = ' selected="selected"';
1.33      raeburn  1730:                 }
                   1731:                 $markup .= ' <option value="'.$option.'"'.$currsel.'>';
                   1732:                 if (($option eq 'all') || ($option eq 'none')) {
                   1733:                     $markup .= $title{$option};
                   1734:                 } else {
                   1735:                     $markup .= $option;
                   1736:                 }   
                   1737:                 $markup .= '</option>'."\n";
                   1738:             }
                   1739:             $markup .= '</select>'."\n";
                   1740:             $output .= ('&nbsp;'x3).'<label>'.$title{$item}.': '.$markup.'</label>';
                   1741:         }
                   1742:     }
                   1743:     return $output;
                   1744: }
                   1745: 
1.2       raeburn  1746: sub list_submit_button {
                   1747:     my ($text) = @_;
1.11      raeburn  1748:     return '<input type="button" name="updatedisplay" value="'.$text.'" onclick="javascript:display_update()" />';
1.2       raeburn  1749: }
                   1750: 
                   1751: sub gather_userinfo {
1.11      raeburn  1752:     my ($context,$format,$userlist,$indexhash,$userinfo,$rolehash,$permission) = @_;
1.52      raeburn  1753:     my $viewablesec;
                   1754:     if ($context eq 'course') {
                   1755:         $viewablesec = &viewable_section($permission);
                   1756:     }
1.2       raeburn  1757:     foreach my $item (keys(%{$rolehash})) {
                   1758:         my %userdata;
1.22      raeburn  1759:         if ($context eq 'author') { 
1.2       raeburn  1760:             ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
                   1761:                 split(/:/,$item);
                   1762:             ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
1.24      raeburn  1763:             &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   1764:                                $item,$userlist);
1.22      raeburn  1765:         } elsif ($context eq 'course') {
                   1766:             ($userdata{'username'},$userdata{'domain'},$userdata{'role'},
                   1767:              $userdata{'section'}) = split(/:/,$item,-1);
                   1768:             ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
                   1769:             if (($viewablesec ne '') && ($userdata{'section'} ne '')) {
                   1770:                 next if ($viewablesec ne $userdata{'section'});
                   1771:             }
1.24      raeburn  1772:             &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   1773:                                $item,$userlist);
1.2       raeburn  1774:         } elsif ($context eq 'domain') {
                   1775:             if ($env{'form.roletype'} eq 'domain') {
                   1776:                 ($userdata{'role'},$userdata{'username'},$userdata{'domain'}) =
                   1777:                     split(/:/,$item);
                   1778:                 ($userdata{'end'},$userdata{'start'})=split(/:/,$rolehash->{$item});
1.24      raeburn  1779:                 &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   1780:                                    $item,$userlist);
1.13      raeburn  1781:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  1782:                 if (ref($rolehash->{$item}) eq 'HASH') {
                   1783:                     $userdata{'extent'} = $item;
                   1784:                     foreach my $key (keys(%{$rolehash->{$item}})) {
                   1785:                         ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =  split(/:/,$key);
                   1786:                         ($userdata{'start'},$userdata{'end'}) = 
                   1787:                             split(/:/,$rolehash->{$item}{$key});
                   1788:                         my $uniqid = $key.':'.$item;
1.25      raeburn  1789:                         &build_user_record($context,\%userdata,$userinfo,
                   1790:                                            $indexhash,$uniqid,$userlist);
1.2       raeburn  1791:                     }
                   1792:                 }
1.97.2.5! raeburn  1793:             } elsif (($env{'form.roletype'} eq 'course') || 
        !          1794:                      ($env{'form.roletype'} eq 'community')) {
1.2       raeburn  1795:                 ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
                   1796:                     split(/:/,$item);
                   1797:                 if (ref($rolehash->{$item}) eq 'HASH') {
1.11      raeburn  1798:                     my $numcids = keys(%{$rolehash->{$item}});
1.2       raeburn  1799:                     foreach my $cid (sort(keys(%{$rolehash->{$item}}))) {
                   1800:                         if (ref($rolehash->{$item}{$cid}) eq 'HASH') {
                   1801:                             my $spanstart = '';
                   1802:                             my $spanend = '; ';
                   1803:                             my $space = ', ';
                   1804:                             if ($format eq 'html' || $format eq 'view') {
                   1805:                                 $spanstart = '<span class="LC_nobreak">';
1.23      raeburn  1806:                                 # FIXME: actions on courses disabled for now
                   1807: #                                if ($permission->{'cusr'}) {
                   1808: #                                    if ($numcids > 1) {
1.25      raeburn  1809: #                                        $spanstart .= '<input type="radio" name="'.$item.'" value="'.$cid.'" />&nbsp;';
1.23      raeburn  1810: #                                    } else {
1.25      raeburn  1811: #                                        $spanstart .= '<input type="hidden" name="'.$item.'" value="'.$cid.'" />&nbsp;';
1.23      raeburn  1812: #                                    }
                   1813: #                                }
1.2       raeburn  1814:                                 $spanend = '</span><br />';
                   1815:                                 $space = ',&nbsp;';
                   1816:                             }
                   1817:                             $userdata{'extent'} .= $spanstart.
                   1818:                                     $rolehash->{$item}{$cid}{'desc'}.$space;
                   1819:                             if (ref($rolehash->{$item}{$cid}{'secs'}) eq 'HASH') { 
                   1820:                                 foreach my $sec (sort(keys(%{$rolehash->{$item}{$cid}{'secs'}}))) {
1.25      raeburn  1821:                                     if (($env{'form.Status'} eq 'Any') ||
                   1822:                                         ($env{'form.Status'} eq $rolehash->{$item}{$cid}{'secs'}{$sec})) {
                   1823:                                         $userdata{'extent'} .= $sec.$space.$rolehash->{$item}{$cid}{'secs'}{$sec}.$spanend;
                   1824:                                         $userdata{'status'} = $rolehash->{$item}{$cid}{'secs'}{$sec};
                   1825:                                     }
1.2       raeburn  1826:                                 }
                   1827:                             }
                   1828:                         }
                   1829:                     }
                   1830:                 }
1.25      raeburn  1831:                 if ($userdata{'status'} ne '') {
                   1832:                     &build_user_record($context,\%userdata,$userinfo,
                   1833:                                        $indexhash,$item,$userlist);
                   1834:                 }
1.2       raeburn  1835:             }
                   1836:         }
                   1837:     }
                   1838:     return;
                   1839: }
                   1840: 
                   1841: sub build_user_record {
1.24      raeburn  1842:     my ($context,$userdata,$userinfo,$indexhash,$record_key,$userlist) = @_;
1.11      raeburn  1843:     next if ($userdata->{'start'} eq '-1' && $userdata->{'end'} eq '-1');
1.97.2.5! raeburn  1844:     if (!(($context eq 'domain') && (($env{'form.roletype'} eq 'course')
        !          1845:                              && ($env{'form.roletype'} eq 'community')))) {
1.24      raeburn  1846:         &process_date_info($userdata);
                   1847:     }
1.2       raeburn  1848:     my $username = $userdata->{'username'};
                   1849:     my $domain = $userdata->{'domain'};
                   1850:     if (ref($userinfo->{$username.':'.$domain}) eq 'HASH') {
1.24      raeburn  1851:         $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
1.2       raeburn  1852:         $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
                   1853:     } else {
                   1854:         &aggregate_user_info($domain,$username,$userinfo);
                   1855:         $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
                   1856:         $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
                   1857:     }
                   1858:     foreach my $key (keys(%{$indexhash})) {
                   1859:         if (defined($userdata->{$key})) {
                   1860:             $userlist->{$record_key}[$indexhash->{$key}] = $userdata->{$key};
                   1861:         }
                   1862:     }
                   1863:     return;
                   1864: }
                   1865: 
                   1866: sub courses_selector {
                   1867:     my ($cdom,$formname) = @_;
                   1868:     my %coursecodes = ();
                   1869:     my %codes = ();
                   1870:     my @codetitles = ();
                   1871:     my %cat_titles = ();
                   1872:     my %cat_order = ();
                   1873:     my %idlist = ();
                   1874:     my %idnums = ();
                   1875:     my %idlist_titles = ();
                   1876:     my $caller = 'global';
                   1877:     my $format_reply;
                   1878:     my $jscript = '';
                   1879: 
1.7       albertel 1880:     my $totcodes = 0;
                   1881:     $totcodes =
1.2       raeburn  1882:         &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,
                   1883:                                                       $cdom,$totcodes);
                   1884:     if ($totcodes > 0) {
                   1885:         $format_reply =
                   1886:              &Apache::lonnet::auto_instcode_format($caller,$cdom,\%coursecodes,
                   1887:                                 \%codes,\@codetitles,\%cat_titles,\%cat_order);
                   1888:         if ($format_reply eq 'ok') {
                   1889:             my $numtypes = @codetitles;
                   1890:             &Apache::courseclassifier::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
                   1891:             my ($scripttext,$longtitles) = &Apache::courseclassifier::javascript_definitions(\@codetitles,\%idlist,\%idlist_titles,\%idnums,\%cat_titles);
                   1892:             my $longtitles_str = join('","',@{$longtitles});
                   1893:             my $allidlist = $idlist{$codetitles[0]};
                   1894:             $jscript .= &Apache::courseclassifier::courseset_js_start($formname,$longtitles_str,$allidlist);
                   1895:             $jscript .= $scripttext;
                   1896:             $jscript .= &Apache::courseclassifier::javascript_code_selections($formname,@codetitles);
                   1897:         }
                   1898:     }
                   1899:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($cdom);
                   1900: 
                   1901:     my %elements = (
                   1902:                      Year => 'selectbox',
                   1903:                      coursepick => 'radio',
                   1904:                      coursetotal => 'text',
                   1905:                      courselist => 'text',
                   1906:                    );
                   1907:     $jscript .= &Apache::lonhtmlcommon::set_form_elements(\%elements);
                   1908:     if ($env{'form.coursepick'} eq 'category') {
                   1909:         $jscript .= qq|
                   1910: function setCourseCat(formname) {
                   1911:     if (formname.Year.options[formname.Year.selectedIndex].value == -1) {
                   1912:         return;
                   1913:     }
                   1914:     courseSet('Year');
                   1915:     for (var j=0; j<formname.Semester.length; j++) {
                   1916:         if (formname.Semester.options[j].value == "$env{'form.Semester'}") {
                   1917:             formname.Semester.options[j].selected = true;
                   1918:         }
                   1919:     }
                   1920:     if (formname.Semester.options[formname.Semester.selectedIndex].value == -1) {
                   1921:         return;
                   1922:     }
                   1923:     courseSet('Semester');
                   1924:     for (var j=0; j<formname.Department.length; j++) {
                   1925:         if (formname.Department.options[j].value == "$env{'form.Department'}") {            formname.Department.options[j].selected = true;
                   1926:         }
                   1927:     }
                   1928:     if (formname.Department.options[formname.Department.selectedIndex].value == -1) {
                   1929:         return;
                   1930:     }
                   1931:     courseSet('Department');
                   1932:     for (var j=0; j<formname.Number.length; j++) {
                   1933:         if (formname.Number.options[j].value == "$env{'form.Number'}") {
                   1934:             formname.Number.options[j].selected = true;
                   1935:         }
                   1936:     }
                   1937: }
                   1938: |;
                   1939:     }
                   1940:     return ($cb_jscript,$jscript,$totcodes,\@codetitles,\%idlist,
                   1941:             \%idlist_titles);
                   1942: }
                   1943: 
                   1944: sub course_selector_loadcode {
                   1945:     my ($formname) = @_;
                   1946:     my $loadcode;
                   1947:     if ($env{'form.coursepick'} ne '') {
                   1948:         $loadcode = 'javascript:setFormElements(document.'.$formname.')';
                   1949:         if ($env{'form.coursepick'} eq 'category') {
                   1950:             $loadcode .= ';javascript:setCourseCat(document.'.$formname.')';
                   1951:         }
                   1952:     }
                   1953:     return $loadcode;
                   1954: }
                   1955: 
                   1956: sub process_coursepick {
                   1957:     my $coursefilter = $env{'form.coursepick'};
                   1958:     my $cdom = $env{'request.role.domain'};
                   1959:     my %courses;
                   1960:     if ($coursefilter eq 'all') {
                   1961:         %courses = &Apache::lonnet::courseiddump($cdom,'.','.','.','.','.',
                   1962:                                                  undef,undef,'Course');
                   1963:     } elsif ($coursefilter eq 'category') {
                   1964:         my $instcode = &instcode_from_coursefilter();
                   1965:         %courses = &Apache::lonnet::courseiddump($cdom,'.','.',$instcode,'.','.',
                   1966:                                                  undef,undef,'Course');
                   1967:     } elsif ($coursefilter eq 'specific') {
                   1968:         if ($env{'form.coursetotal'} > 1) {
                   1969:             my @course_ids = split(/&&/,$env{'form.courselist'});
                   1970:             foreach my $cid (@course_ids) {
                   1971:                 $courses{$cid} = '';
1.1       raeburn  1972:             }
1.2       raeburn  1973:         } else {
                   1974:             $courses{$env{'form.courselist'}} = '';
1.1       raeburn  1975:         }
1.2       raeburn  1976:     }
                   1977:     return %courses;
                   1978: }
                   1979: 
                   1980: sub instcode_from_coursefilter {
                   1981:     my $instcode = '';
                   1982:     my @cats = ('Semester','Year','Department','Number');
                   1983:     foreach my $category (@cats) {
                   1984:         if (defined($env{'form.'.$category})) {
                   1985:             unless ($env{'form.'.$category} eq '-1') {
                   1986:                 $instcode .= $env{'form.'.$category};
                   1987:            }
                   1988:         }
                   1989:     }
                   1990:     if ($instcode eq '') {
                   1991:         $instcode = '.';
                   1992:     }
                   1993:     return $instcode;
                   1994: }
                   1995: 
                   1996: sub display_adv_courseroles {
                   1997:     my $output;
                   1998:     #
                   1999:     # List course personnel
                   2000:     my %coursepersonnel = 
                   2001:        &Apache::lonnet::get_course_adv_roles($env{'request.course.id'});
                   2002:     #
                   2003:     $output = '<br />'.&Apache::loncommon::start_data_table();
                   2004:     foreach my $role (sort(keys(%coursepersonnel))) {
                   2005:         next if ($role =~ /^\s*$/);
                   2006:         $output .= &Apache::loncommon::start_data_table_row().
                   2007:                   '<td>'.$role.'</td><td>';
                   2008:         foreach my $user (split(',',$coursepersonnel{$role})) {
                   2009:             my ($puname,$pudom)=split(':',$user);
                   2010:             $output .= ' '.&Apache::loncommon::aboutmewrapper(
                   2011:                        &Apache::loncommon::plainname($puname,$pudom),
                   2012:                        $puname,$pudom);
                   2013:         }
                   2014:         $output .= '</td>'.&Apache::loncommon::end_data_table_row();
                   2015:     }
                   2016:     $output .= &Apache::loncommon::end_data_table();
                   2017: }
                   2018: 
                   2019: sub make_keylist_array {
                   2020:     my ($index,$keylist);
                   2021:     $index->{'domain'} = &Apache::loncoursedata::CL_SDOM();
                   2022:     $index->{'username'} = &Apache::loncoursedata::CL_SNAME();
                   2023:     $index->{'end'} = &Apache::loncoursedata::CL_END();
                   2024:     $index->{'start'} = &Apache::loncoursedata::CL_START();
                   2025:     $index->{'id'} = &Apache::loncoursedata::CL_ID();
                   2026:     $index->{'section'} = &Apache::loncoursedata::CL_SECTION();
                   2027:     $index->{'fullname'} = &Apache::loncoursedata::CL_FULLNAME();
                   2028:     $index->{'status'} = &Apache::loncoursedata::CL_STATUS();
                   2029:     $index->{'type'} = &Apache::loncoursedata::CL_TYPE();
                   2030:     $index->{'lockedtype'} = &Apache::loncoursedata::CL_LOCKEDTYPE();
                   2031:     $index->{'groups'} = &Apache::loncoursedata::CL_GROUP();
                   2032:     $index->{'email'} = &Apache::loncoursedata::CL_PERMANENTEMAIL();
                   2033:     $index->{'role'} = &Apache::loncoursedata::CL_ROLE();
                   2034:     $index->{'extent'} = &Apache::loncoursedata::CL_EXTENT();
1.44      raeburn  2035:     $index->{'photo'} = &Apache::loncoursedata::CL_PHOTO();
1.47      raeburn  2036:     $index->{'thumbnail'} = &Apache::loncoursedata::CL_THUMBNAIL();
1.2       raeburn  2037:     foreach my $key (keys(%{$index})) {
                   2038:         $keylist->[$index->{$key}] = $key;
                   2039:     }
                   2040:     return ($index,$keylist);
                   2041: }
                   2042: 
                   2043: sub aggregate_user_info {
                   2044:     my ($udom,$uname,$userinfo) = @_;
                   2045:     my %info=&Apache::lonnet::get('environment',
                   2046:                                   ['firstname','middlename',
                   2047:                                    'lastname','generation','id'],
                   2048:                                    $udom,$uname);
                   2049:     my ($tmp) = keys(%info);
                   2050:     my ($fullname,$id);
                   2051:     if ($tmp =~/^(con_lost|error|no_such_host)/i) {
                   2052:         $fullname = 'not available';
                   2053:         $id = 'not available';
                   2054:         &Apache::lonnet::logthis('unable to retrieve environment '.
                   2055:                                  'for '.$uname.':'.$udom);
1.1       raeburn  2056:     } else {
1.2       raeburn  2057:         $fullname = &Apache::lonnet::format_name(@info{qw/firstname middlename lastname generation/},'lastname');
                   2058:         $id = $info{'id'};
                   2059:     }
                   2060:     $userinfo->{$uname.':'.$udom} = { 
                   2061:                                       fullname => $fullname,
                   2062:                                       id       => $id,
                   2063:                                     };
                   2064:     return;
                   2065: }
1.1       raeburn  2066: 
1.2       raeburn  2067: sub process_date_info {
                   2068:     my ($userdata) = @_;
                   2069:     my $now = time;
1.83      raeburn  2070:     $userdata->{'status'} = 'Active';
1.2       raeburn  2071:     if ($userdata->{'start'} > 0) {
                   2072:         if ($now < $userdata->{'start'}) {
1.83      raeburn  2073:             $userdata->{'status'} = 'Future';
1.2       raeburn  2074:         }
1.1       raeburn  2075:     }
1.2       raeburn  2076:     if ($userdata->{'end'} > 0) {
                   2077:         if ($now > $userdata->{'end'}) {
1.83      raeburn  2078:             $userdata->{'status'} = 'Expired';
1.2       raeburn  2079:         }
                   2080:     }
                   2081:     return;
1.1       raeburn  2082: }
                   2083: 
                   2084: sub show_users_list {
1.55      raeburn  2085:     my ($r,$context,$mode,$permission,$statusmode,$userlist,$keylist,$formname)=@_;
                   2086:     if ($formname eq '') {
                   2087:         $formname = 'studentform';
                   2088:     }
1.1       raeburn  2089:     #
                   2090:     # Variables for excel output
                   2091:     my ($excel_workbook, $excel_sheet, $excel_filename,$row,$format);
                   2092:     #
                   2093:     # Variables for csv output
                   2094:     my ($CSVfile,$CSVfilename);
                   2095:     #
                   2096:     my $sortby = $env{'form.sortby'};
1.3       raeburn  2097:     my @sortable = ('username','domain','id','fullname','start','end','email','role');
1.2       raeburn  2098:     if ($context eq 'course') {
1.3       raeburn  2099:         push(@sortable,('section','groups','type'));
1.2       raeburn  2100:     } else {
1.3       raeburn  2101:         push(@sortable,'extent');
                   2102:     }
1.55      raeburn  2103:     if ($mode eq 'pickauthor') {
                   2104:         @sortable = ('username','fullname','email','status');
                   2105:     }
1.3       raeburn  2106:     if (!grep(/^\Q$sortby\E$/,@sortable)) {
                   2107:         $sortby = 'username';
1.1       raeburn  2108:     }
1.22      raeburn  2109:     my $setting = $env{'form.roletype'};
1.97.2.4  raeburn  2110:     my ($cid,$cdom,$cnum,$classgroups,$displayphotos,$displayclickers,$crstype);
1.1       raeburn  2111:     if ($context eq 'course') {
1.22      raeburn  2112:         $cid = $env{'request.course.id'};
1.97.2.4  raeburn  2113:         $crstype = &Apache::loncommon::course_type();
1.17      raeburn  2114:         ($cnum,$cdom) = &get_course_identity($cid);
1.2       raeburn  2115:         ($classgroups) = &Apache::loncoursedata::get_group_memberships(
                   2116:                                      $userlist,$keylist,$cdom,$cnum);
1.16      raeburn  2117:         if ($mode eq 'autoenroll') {
                   2118:             $env{'form.showrole'} = 'st';
                   2119:         } else {
                   2120:             if (! exists($env{'form.displayphotos'})) {
                   2121:                 $env{'form.displayphotos'} = 'off';
                   2122:             }
                   2123:             $displayphotos = $env{'form.displayphotos'};
                   2124:             if (! exists($env{'form.displayclickers'})) {
                   2125:                 $env{'form.displayclickers'} = 'off';
                   2126:             }
                   2127:             $displayclickers = $env{'form.displayclickers'};
                   2128:             if ($env{'course.'.$cid.'.internal.showphoto'}) {
                   2129:                 $r->print('
1.1       raeburn  2130: <script type="text/javascript">
1.96      bisitz   2131: // <![CDATA[
1.1       raeburn  2132: function photowindow(photolink) {
                   2133:     var title = "Photo_Viewer";
                   2134:     var options = "scrollbars=1,resizable=1,menubar=0";
                   2135:     options += ",width=240,height=240";
                   2136:     stdeditbrowser = open(photolink,title,options,"1");
                   2137:     stdeditbrowser.focus();
                   2138: }
1.96      bisitz   2139: // ]]>
1.1       raeburn  2140: </script>
1.16      raeburn  2141:                ');
                   2142:             }
                   2143:             $r->print(<<END);
1.1       raeburn  2144: <input type="hidden" name="displayphotos" value="$displayphotos" />
                   2145: <input type="hidden" name="displayclickers" value="$displayclickers" />
                   2146: END
1.16      raeburn  2147:         }
1.97.2.5! raeburn  2148:     } elsif ($context eq 'domain') {
        !          2149:         if ($setting eq 'community') {
        !          2150:             $crstype = 'Community';
        !          2151:         } elsif ($crstype eq 'course') {
        !          2152:             $crstype = 'Course';
        !          2153:         }
1.1       raeburn  2154:     }
1.55      raeburn  2155:     if ($mode ne 'autoenroll' && $mode ne 'pickauthor') {
1.11      raeburn  2156:         my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
1.40      raeburn  2157:         my $date_sec_selector = &date_section_javascript($context,$setting,$statusmode);
1.56      raeburn  2158:         my $verify_action_js = &bulkaction_javascript($formname);
1.1       raeburn  2159:         $r->print(<<END);
1.10      raeburn  2160: 
                   2161: <script type="text/javascript" language="Javascript">
1.96      bisitz   2162: // <![CDATA[
1.11      raeburn  2163: $check_uncheck_js
                   2164: 
1.56      raeburn  2165: $verify_action_js
1.10      raeburn  2166: 
                   2167: function username_display_launch(username,domain) {
                   2168:     var target;
1.55      raeburn  2169:     for (var i=0; i<document.$formname.usernamelink.length; i++) {
                   2170:         if (document.$formname.usernamelink[i].checked) {
                   2171:             target = document.$formname.usernamelink[i].value;
1.10      raeburn  2172:         }
                   2173:     }
                   2174:     if (target == 'modify') {
1.55      raeburn  2175:         if (document.$formname.userwin.checked == true) {
1.50      raeburn  2176:             var url = '/adm/createuser?srchterm='+username+'&srchdomain='+domain+'&phase=get_user_info&action=singleuser&srchin=dom&srchby=uname&srchtype=exact&popup=1';
                   2177:             var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
                   2178:             modifywin = window.open(url,'',options,1);
                   2179:             modifywin.focus();
                   2180:             return;
                   2181:         } else {
1.55      raeburn  2182:             document.$formname.srchterm.value=username;
                   2183:             document.$formname.srchdomain.value=domain;
                   2184:             document.$formname.phase.value='get_user_info';
                   2185:             document.$formname.action.value = 'singleuser';
                   2186:             document.$formname.submit();
1.50      raeburn  2187:         }
1.10      raeburn  2188:     }
1.48      raeburn  2189:     if (target == 'aboutme') {
1.55      raeburn  2190:         if (document.$formname.userwin.checked == true) {
1.50      raeburn  2191:             var url = '/adm/'+domain+'/'+username+'/aboutme?popup=1';
                   2192:             var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
                   2193:             aboutmewin = window.open(url,'',options,1);
                   2194:             aboutmewin.focus();
                   2195:             return;
                   2196:         } else {
                   2197:             document.location.href = '/adm/'+domain+'/'+username+'/aboutme';
                   2198:         }
1.48      raeburn  2199:     }
1.97.2.2  raeburn  2200:     if (target == 'track') {
                   2201:         if (document.$formname.userwin.checked == true) {
                   2202:             var url = '/adm/trackstudent?selected_student='+username+':'+domain+'&only_body=1';
                   2203:             var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
                   2204:             var trackwin = window.open(url,'',options,1);
                   2205:             trackwin.focus();
                   2206:             return;
                   2207:         } else {
                   2208:             document.location.href = '/adm/trackstudent?selected_student='+username+':'+domain;
                   2209:         }
                   2210:     }
1.10      raeburn  2211: }
1.96      bisitz   2212: // ]]>
1.10      raeburn  2213: </script>
1.11      raeburn  2214: $date_sec_selector
1.1       raeburn  2215: <input type="hidden" name="state" value="$env{'form.state'}" />
                   2216: END
                   2217:     }
                   2218:     $r->print(<<END);
                   2219: <input type="hidden" name="sortby" value="$sortby" />
                   2220: END
                   2221: 
                   2222:     my %lt=&Apache::lonlocal::texthash(
                   2223:                        'username'   => "username",
                   2224:                        'domain'     => "domain",
                   2225:                        'id'         => 'ID',
                   2226:                        'fullname'   => "name",
                   2227:                        'section'    => "section",
                   2228:                        'groups'     => "active groups",
                   2229:                        'start'      => "start date",
                   2230:                        'end'        => "end date",
                   2231:                        'status'     => "status",
1.2       raeburn  2232:                        'role'       => "role",
1.1       raeburn  2233:                        'type'       => "enroll type/action",
1.79      schafran 2234:                        'email'      => "e-mail address",
1.1       raeburn  2235:                        'photo'      => "photo",
1.2       raeburn  2236:                        'extent'     => "extent",
1.11      raeburn  2237:                        'pr'         => "Proceed",
                   2238:                        'ca'         => "check all",
                   2239:                        'ua'         => "uncheck all",
                   2240:                        'ac'         => "Action to take for selected users",
1.56      raeburn  2241:                        'link'       => "Behavior of clickable username link for each user",
1.82      weissno  2242:                        'aboutme'    => "Display a user's personal information page",
1.50      raeburn  2243:                        'owin'       => "Open in a new window",
1.10      raeburn  2244:                        'modify'     => "Modify a user's information",
1.97.2.2  raeburn  2245:                        'track'      => "View a user's recent activity",
1.67      droeschl 2246:                        'clicker'    => "Clicker-ID",
1.1       raeburn  2247:                       );
1.2       raeburn  2248:     if ($context eq 'domain' && $env{'form.roletype'} eq 'course') {
                   2249:         $lt{'extent'} = &mt('Course(s): description, section(s), status');
1.97.2.5! raeburn  2250:     } elsif ($context eq 'domain' && $env{'form.roletype'} eq 'community') {
        !          2251:         $lt{'extent'} = &mt('Communities: description, section(s), status');
1.13      raeburn  2252:     } elsif ($context eq 'author') {
1.2       raeburn  2253:         $lt{'extent'} = &mt('Author'); 
                   2254:     }
1.55      raeburn  2255:     my @cols;
                   2256:     if ($mode eq 'pickauthor') {
                   2257:         @cols = ('username','fullname','status','email');
                   2258:     } else {
                   2259:         @cols = ('username','domain','id','fullname');
                   2260:         if ($context eq 'course') {
                   2261:             push(@cols,'section');
                   2262:         }
1.97.2.5! raeburn  2263:         if (!($context eq 'domain' && ($env{'form.roletype'} eq 'course')
        !          2264:                               && ($env{'form.roletype'} eq 'community'))) {
1.55      raeburn  2265:             push(@cols,('start','end'));
                   2266:         }
                   2267:         if ($env{'form.showrole'} eq 'Any' || $env{'form.showrole'} eq 'cr') {
                   2268:             push(@cols,'role');
                   2269:         }
                   2270:         if ($context eq 'domain' && ($env{'form.roletype'} eq 'author' ||
1.97.2.5! raeburn  2271:                                     $env{'form.roletype'} eq 'course' ||
        !          2272:                                     $env{'form.roletype'} eq 'community')) {
1.55      raeburn  2273:             push (@cols,'extent');
                   2274:         }
1.97.2.5! raeburn  2275:         if (($statusmode eq 'Any') &&
        !          2276:             (!($context eq 'domain' && (($env{'form.roletype'} eq 'course')
        !          2277:              || ($env{'form.roletype'} eq 'community'))))) {
1.55      raeburn  2278:             push(@cols,'status');
                   2279:         }
                   2280:         if ($context eq 'course') {
                   2281:             push(@cols,'groups');
                   2282:         }
                   2283:         push(@cols,'email');
1.2       raeburn  2284:     }
1.1       raeburn  2285: 
1.4       raeburn  2286:     my $rolefilter = $env{'form.showrole'};
1.5       raeburn  2287:     if ($env{'form.showrole'} eq 'cr') {
                   2288:         $rolefilter = &mt('custom');  
                   2289:     } elsif ($env{'form.showrole'} ne 'Any') {
1.2       raeburn  2290:         $rolefilter = &Apache::lonnet::plaintext($env{'form.showrole'});
                   2291:     }
1.16      raeburn  2292:     my $results_description;
                   2293:     if ($mode ne 'autoenroll') {
                   2294:         $results_description = &results_header_row($rolefilter,$statusmode,
1.97.2.5! raeburn  2295:                                                    $context,$permission,$mode,$crstype);
        !          2296: 
1.56      raeburn  2297:         $r->print('<b>'.$results_description.'</b><br /><br />');
1.16      raeburn  2298:     }
1.26      raeburn  2299:     my ($output,$actionselect,%canchange,%canchangesec);
1.55      raeburn  2300:     if ($mode eq 'html' || $mode eq 'view' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
                   2301:         if ($mode ne 'autoenroll' && $mode ne 'pickauthor') {
1.16      raeburn  2302:             if ($permission->{'cusr'}) {
1.55      raeburn  2303:                 $actionselect = &select_actions($context,$setting,$statusmode,$formname);
1.16      raeburn  2304:             }
                   2305:             $r->print(<<END);
1.10      raeburn  2306: <input type="hidden" name="srchby"  value="uname" />
                   2307: <input type="hidden" name="srchin"   value="dom" />
                   2308: <input type="hidden" name="srchtype" value="exact" />
                   2309: <input type="hidden" name="srchterm" value="" />
1.11      raeburn  2310: <input type="hidden" name="srchdomain" value="" /> 
1.1       raeburn  2311: END
1.16      raeburn  2312:             if ($actionselect) {
1.41      raeburn  2313:                 $output .= <<"END";
1.97.2.1  raeburn  2314: <div class="LC_left_float"><fieldset><legend><b>$lt{'ac'}</b></legend>
1.56      raeburn  2315: $actionselect
                   2316: <br/><br /><input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.$formname.actionlist)" /> &nbsp;
                   2317: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.$formname.actionlist)" /><br /><input type="button" value="$lt{'pr'}" onclick="javascript:verify_action('actionlist')" /></fieldset></div>
1.11      raeburn  2318: END
1.26      raeburn  2319:                 my @allroles;
                   2320:                 if ($env{'form.showrole'} eq 'Any') {
                   2321:                     my $custom = 1;
                   2322:                     if ($context eq 'domain') {
1.97.2.4  raeburn  2323:                         @allroles = &roles_by_context($setting,$custom,$crstype);
1.26      raeburn  2324:                     } else {
1.97.2.4  raeburn  2325:                         @allroles = &roles_by_context($context,$custom,$crstype);
1.26      raeburn  2326:                     }
                   2327:                 } else {
                   2328:                     @allroles = ($env{'form.showrole'});
                   2329:                 }
                   2330:                 foreach my $role (@allroles) {
                   2331:                     if ($context eq 'domain') {
                   2332:                         if ($setting eq 'domain') {
                   2333:                             if (&Apache::lonnet::allowed('c'.$role,
                   2334:                                     $env{'request.role.domain'})) {
                   2335:                                 $canchange{$role} = 1;
                   2336:                             }
1.31      raeburn  2337:                         } elsif ($setting eq 'author') {
                   2338:                             if (&Apache::lonnet::allowed('c'.$role,
                   2339:                                     $env{'request.role.domain'})) {
                   2340:                                 $canchange{$role} = 1;
                   2341:                             }
1.26      raeburn  2342:                         }
                   2343:                     } elsif ($context eq 'author') {
                   2344:                         if (&Apache::lonnet::allowed('c'.$role,
                   2345:                             $env{'user.domain'}.'/'.$env{'user.name'})) {
                   2346:                             $canchange{$role} = 1;
                   2347:                         }
                   2348:                     } elsif ($context eq 'course') {
                   2349:                         if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   2350:                             $canchange{$role} = 1;
                   2351:                         } elsif ($env{'request.course.sec'} ne '') {
                   2352:                             if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'}.'/'.$env{'request.course.sec'})) {
                   2353:                                 $canchangesec{$role} = $env{'request.course.sec'};
                   2354:                             }
                   2355:                         }
                   2356:                     }
                   2357:                 }
1.16      raeburn  2358:             }
1.97.2.1  raeburn  2359:             $output .= '<div class="LC_left_float"><fieldset><legend><b>'.$lt{'link'}.'</b></legend>'.
1.56      raeburn  2360:                        '<table><tr>';
                   2361:             my @linkdests = ('aboutme');
                   2362:             if ($permission->{'cusr'}) {
                   2363:                 unshift (@linkdests,'modify');
                   2364:             }
1.97.2.2  raeburn  2365:             if (&Apache::lonnet::allowed('vsa', $env{'request.course.id'}) ||
                   2366:                 &Apache::lonnet::allowed('vsa', $env{'request.course.id'}.'/'.
                   2367:                                          $env{'request.course.sec'})) {
                   2368:                 push(@linkdests,'track');
                   2369:             }
1.56      raeburn  2370:             $output .= '<td>';
                   2371:             my $usernamelink = $env{'form.usernamelink'};
                   2372:             if ($usernamelink eq '') {
                   2373:                 $usernamelink = 'aboutme';
                   2374:             }
                   2375:             foreach my $item (@linkdests) {
                   2376:                 my $checkedstr = '';
                   2377:                 if ($item eq $usernamelink) {
1.86      bisitz   2378:                     $checkedstr = ' checked="checked"';
1.56      raeburn  2379:                 }
1.86      bisitz   2380:                 $output .= '<span class="LC_nobreak"><label><input type="radio" name="usernamelink" value="'.$item.'"'.$checkedstr.' />&nbsp;'.$lt{$item}.'</label></span><br />';
1.56      raeburn  2381:             }
                   2382:             my $checkwin;
                   2383:             if ($env{'form.userwin'}) {
1.86      bisitz   2384:                 $checkwin = ' checked="checked"';
1.56      raeburn  2385:             }
1.86      bisitz   2386:             $output .= '</td><td valign="top"><span class="LC_nobreak"><input type="checkbox" name="userwin" value="1"'.$checkwin.' />'.$lt{'owin'}.'</span></td></tr></table></fieldset></div>';
1.4       raeburn  2387:         }
1.56      raeburn  2388:         $output .= "\n".'<div class="LC_clear_float_footer">&nbsp;</div>'."\n".
1.1       raeburn  2389:                   &Apache::loncommon::start_data_table().
1.4       raeburn  2390:                   &Apache::loncommon::start_data_table_header_row();
1.1       raeburn  2391:         if ($mode eq 'autoenroll') {
1.4       raeburn  2392:             $output .= "
1.55      raeburn  2393:  <th><a href=\"javascript:document.$formname.sortby.value='type';document.$formname.submit();\">$lt{'type'}</a></th>
1.4       raeburn  2394:             ";
1.1       raeburn  2395:         } else {
1.55      raeburn  2396:             if ($mode eq 'pickauthor') {
                   2397:                 $output .= "\n".'<th>&nbsp;</th>'."\n";
                   2398:             } else { 
                   2399:                 $output .= "\n".'<th>'.&mt('Count').'</th>'."\n";
                   2400:             }
1.11      raeburn  2401:             if ($actionselect) {
                   2402:                 $output .= '<th>'.&mt('Select').'</th>'."\n";
                   2403:             }
1.1       raeburn  2404:         }
                   2405:         foreach my $item (@cols) {
1.55      raeburn  2406:             $output .= "<th><a href=\"javascript:document.$formname.sortby.value='$item';document.$formname.submit();\">$lt{$item}</a></th>\n";
1.1       raeburn  2407:         }
1.2       raeburn  2408:         my %role_types = &role_type_names();
1.16      raeburn  2409:         if ($context eq 'course' && $mode ne 'autoenroll') {
1.4       raeburn  2410:             if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
                   2411:                 # Clicker display on or off?
1.58      bisitz   2412:                 my %clicker_options = (
                   2413:                                         'on' => 'Show',
                   2414:                                         'off' => 'Hide',
                   2415:                                       );
1.4       raeburn  2416:                 my $clickerchg = 'on';
                   2417:                 if ($displayclickers eq 'on') {
                   2418:                     $clickerchg = 'off';
                   2419:                 }
1.58      bisitz   2420:                 $output .= '    <th>'."\n".'     '
                   2421:                         .&mt('[_1]'.$clicker_options{$clickerchg}.'[_2] clicker id'
                   2422:                             ,'<a href="javascript:document.'.$formname.'.displayclickers.value='
                   2423:                              ."'".$clickerchg."'".';document.'.$formname.'.submit();">'
                   2424:                             ,'</a>')
                   2425:                         ."\n".'    </th>'."\n";
1.1       raeburn  2426: 
1.4       raeburn  2427:                 # Photo display on or off?
                   2428:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
                   2429:                     my %photo_options = &Apache::lonlocal::texthash(
                   2430:                                                             'on' => 'Show',
                   2431:                                                             'off' => 'Hide',
                   2432:                                                                 );
                   2433:                     my $photochg = 'on';
                   2434:                     if ($displayphotos eq 'on') {
                   2435:                         $photochg = 'off';
                   2436:                     }
                   2437:                     $output .= '    <th>'."\n".'     '.
1.55      raeburn  2438:                 '<a href="javascript:document.'.$formname.'.displayphotos.value='.
                   2439:                       "'".$photochg."'".';document.'.$formname.'.submit();">'.
1.1       raeburn  2440:                       $photo_options{$photochg}.'</a>&nbsp;'.$lt{'photo'}."\n".
1.4       raeburn  2441:                       '    </th>'."\n";
                   2442:                 }
1.1       raeburn  2443:             }
1.4       raeburn  2444:         }
1.16      raeburn  2445:         $output .= &Apache::loncommon::end_data_table_header_row();
1.1       raeburn  2446: # Done with the HTML header line
                   2447:     } elsif ($mode eq 'csv') {
                   2448:         #
                   2449:         # Open a file
                   2450:         $CSVfilename = '/prtspool/'.
1.2       raeburn  2451:                        $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
                   2452:                        time.'_'.rand(1000000000).'.csv';
1.1       raeburn  2453:         unless ($CSVfile = Apache::File->new('>/home/httpd'.$CSVfilename)) {
                   2454:             $r->log_error("Couldn't open $CSVfilename for output $!");
1.69      bisitz   2455:             $r->print(&mt('Problems occurred in writing the CSV file. '
1.64      bisitz   2456:                          .'This error has been logged. '
                   2457:                          .'Please alert your LON-CAPA administrator.'));
1.1       raeburn  2458:             $CSVfile = undef;
                   2459:         }
                   2460:         #
1.67      droeschl 2461:         push @cols,'clicker';
1.1       raeburn  2462:         # Write headers and data to file
1.2       raeburn  2463:         print $CSVfile '"'.$results_description.'"'."\n"; 
1.1       raeburn  2464:         print $CSVfile '"'.join('","',map {
                   2465:             &Apache::loncommon::csv_translate($lt{$_})
1.67      droeschl 2466:             } (@cols))."\"\n";
1.1       raeburn  2467:     } elsif ($mode eq 'excel') {
1.67      droeschl 2468:         push @cols,'clicker';
1.1       raeburn  2469:         # Create the excel spreadsheet
                   2470:         ($excel_workbook,$excel_filename,$format) =
                   2471:             &Apache::loncommon::create_workbook($r);
                   2472:         return if (! defined($excel_workbook));
                   2473:         $excel_sheet = $excel_workbook->addworksheet('userlist');
1.2       raeburn  2474:         $excel_sheet->write($row++,0,$results_description,$format->{'h2'});
1.1       raeburn  2475:         #
                   2476:         my @colnames = map {$lt{$_}} (@cols);
1.67      droeschl 2477: 
1.1       raeburn  2478:         $excel_sheet->write($row++,0,\@colnames,$format->{'bold'});
                   2479:     }
                   2480: 
                   2481: # Done with header lines in all formats
                   2482:     my %index;
                   2483:     my $i;
1.2       raeburn  2484:     foreach my $idx (@$keylist) {
                   2485:         $index{$idx} = $i++;
                   2486:     }
1.4       raeburn  2487:     my $usercount = 0;
1.33      raeburn  2488:     my ($secfilter,$grpfilter);
                   2489:     if ($context eq 'course') {
                   2490:         $secfilter = $env{'form.secfilter'};
                   2491:         $grpfilter = $env{'form.grpfilter'};
                   2492:         if ($secfilter eq '') {
                   2493:             $secfilter = 'all';
                   2494:         }
                   2495:         if ($grpfilter eq '') {
                   2496:             $grpfilter = 'all';
                   2497:         }
                   2498:     }
1.83      raeburn  2499:     my %ltstatus = &Apache::lonlocal::texthash(
                   2500:                                                 Active  => 'Active',
                   2501:                                                 Future  => 'Future',
                   2502:                                                 Expired => 'Expired',
                   2503:                                                );
1.2       raeburn  2504:     # Get groups, role, permanent e-mail so we can sort on them if
                   2505:     # necessary.
                   2506:     foreach my $user (keys(%{$userlist})) {
1.43      raeburn  2507:         if ($user eq '' ) {
                   2508:             delete($userlist->{$user});
                   2509:             next;
                   2510:         }
1.11      raeburn  2511:         if ($context eq 'domain' &&  $user eq $env{'request.role.domain'}.'-domainconfig:'.$env{'request.role.domain'}) {
                   2512:             delete($userlist->{$user});
                   2513:             next;
                   2514:         }
1.2       raeburn  2515:         my ($uname,$udom,$role,$groups,$email);
1.5       raeburn  2516:         if (($statusmode ne 'Any') && 
                   2517:                  ($userlist->{$user}->[$index{'status'}] ne $statusmode)) {
                   2518:             delete($userlist->{$user});
                   2519:             next;
                   2520:         }
1.2       raeburn  2521:         if ($context eq 'domain') {
                   2522:             if ($env{'form.roletype'} eq 'domain') {
                   2523:                 ($role,$uname,$udom) = split(/:/,$user);
1.11      raeburn  2524:                 if (($uname eq $env{'request.role.domain'}.'-domainconfig') &&
                   2525:                     ($udom eq $env{'request.role.domain'})) {
                   2526:                     delete($userlist->{$user});
                   2527:                     next;
                   2528:                 }
1.13      raeburn  2529:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  2530:                 ($uname,$udom,$role) = split(/:/,$user,-1);
1.97.2.5! raeburn  2531:             } elsif (($env{'form.roletype'} eq 'course') || 
        !          2532:                      ($env{'form.roletype'} eq 'community')) {
1.2       raeburn  2533:                 ($uname,$udom,$role) = split(/:/,$user);
                   2534:             }
                   2535:         } else {
                   2536:             ($uname,$udom,$role) = split(/:/,$user,-1);
                   2537:             if (($context eq 'course') && $role eq '') {
                   2538:                 $role = 'st';
                   2539:             }
                   2540:         }
                   2541:         $userlist->{$user}->[$index{'role'}] = $role;
                   2542:         if (($env{'form.showrole'} ne 'Any') && (!($env{'form.showrole'}  eq 'cr' && $role =~ /^cr\//)) && ($role ne $env{'form.showrole'})) {
                   2543:             delete($userlist->{$user});
                   2544:             next;
                   2545:         }
1.33      raeburn  2546:         if ($context eq 'course') {
                   2547:             my @ac_groups;
                   2548:             if (ref($classgroups) eq 'HASH') {
                   2549:                 $groups = $classgroups->{$user};
                   2550:             }
                   2551:             if (ref($groups->{'active'}) eq 'HASH') {
                   2552:                 @ac_groups = keys(%{$groups->{'active'}});
                   2553:                 $userlist->{$user}->[$index{'groups'}] = join(', ',@ac_groups);
                   2554:             }
                   2555:             if ($mode ne 'autoenroll') {
                   2556:                 my $section = $userlist->{$user}->[$index{'section'}];
1.43      raeburn  2557:                 if (($env{'request.course.sec'} ne '') && 
                   2558:                     ($section ne $env{'request.course.sec'})) {
                   2559:                     if ($role eq 'st') {
                   2560:                         delete($userlist->{$user});
                   2561:                         next;
                   2562:                     }
                   2563:                 }
1.33      raeburn  2564:                 if ($secfilter eq 'none') {
                   2565:                     if ($section ne '') {
                   2566:                         delete($userlist->{$user});
                   2567:                         next;
                   2568:                     }
                   2569:                 } elsif ($secfilter ne 'all') {
                   2570:                     if ($section ne $secfilter) {
                   2571:                         delete($userlist->{$user});
                   2572:                         next;
                   2573:                     }
                   2574:                 }
                   2575:                 if ($grpfilter eq 'none') {
                   2576:                     if (@ac_groups > 0) {
                   2577:                         delete($userlist->{$user});
                   2578:                         next;
                   2579:                     }
                   2580:                 } elsif ($grpfilter ne 'all') {
                   2581:                     if (!grep(/^\Q$grpfilter\E$/,@ac_groups)) {
                   2582:                         delete($userlist->{$user});
                   2583:                         next;
                   2584:                     }
                   2585:                 }
1.44      raeburn  2586:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
                   2587:                     if (($displayphotos eq 'on') && ($role eq 'st')) {
                   2588:                         $userlist->{$user}->[$index{'photo'}] =
1.47      raeburn  2589:                             &Apache::lonnet::retrievestudentphoto($udom,$uname,'jpg');
                   2590:                         $userlist->{$user}->[$index{'thumbnail'}] =
1.44      raeburn  2591:                             &Apache::lonnet::retrievestudentphoto($udom,$uname,
                   2592:                                                                 'gif','thumbnail');
                   2593:                     }
                   2594:                 }
1.33      raeburn  2595:             }
1.2       raeburn  2596:         }
                   2597:         my %emails   = &Apache::loncommon::getemails($uname,$udom);
                   2598:         if ($emails{'permanentemail'} =~ /\S/) {
                   2599:             $userlist->{$user}->[$index{'email'}] = $emails{'permanentemail'};
                   2600:         }
1.4       raeburn  2601:         $usercount ++;
                   2602:     }
                   2603:     my $autocount = 0;
                   2604:     my $manualcount = 0;
                   2605:     my $lockcount = 0;
                   2606:     my $unlockcount = 0;
                   2607:     if ($usercount) {
                   2608:         $r->print($output);
                   2609:     } else {
                   2610:         if ($mode eq 'autoenroll') {
                   2611:             return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
                   2612:         } else {
                   2613:             return;
                   2614:         }
1.1       raeburn  2615:     }
1.2       raeburn  2616:     #
                   2617:     # Sort the users
1.1       raeburn  2618:     my $index  = $index{$sortby};
                   2619:     my $second = $index{'username'};
                   2620:     my $third  = $index{'domain'};
1.2       raeburn  2621:     my @sorted_users = sort {
                   2622:         lc($userlist->{$a}->[$index])  cmp lc($userlist->{$b}->[$index])
1.1       raeburn  2623:             ||
1.2       raeburn  2624:         lc($userlist->{$a}->[$second]) cmp lc($userlist->{$b}->[$second])            ||
                   2625:         lc($userlist->{$a}->[$third]) cmp lc($userlist->{$b}->[$third])
                   2626:         } (keys(%$userlist));
1.4       raeburn  2627:     my $rowcount = 0;
1.2       raeburn  2628:     foreach my $user (@sorted_users) {
1.4       raeburn  2629:         my %in;
1.2       raeburn  2630:         my $sdata = $userlist->{$user};
1.4       raeburn  2631:         $rowcount ++; 
1.2       raeburn  2632:         foreach my $item (@{$keylist}) {
                   2633:             $in{$item} = $sdata->[$index{$item}];
                   2634:         }
1.67      droeschl 2635:         my $clickers = (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
                   2636:         if ($clickers!~/\w/) { $clickers='-'; }
                   2637:         $in{'clicker'} = $clickers; 
                   2638: 	my $role = $in{'role'};
1.97.2.4  raeburn  2639:         $in{'role'}=&Apache::lonnet::plaintext($sdata->[$index{'role'}],$crstype); 
1.2       raeburn  2640:         if (! defined($in{'start'}) || $in{'start'} == 0) {
                   2641:             $in{'start'} = &mt('none');
                   2642:         } else {
                   2643:             $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
1.1       raeburn  2644:         }
1.2       raeburn  2645:         if (! defined($in{'end'}) || $in{'end'} == 0) {
                   2646:             $in{'end'} = &mt('none');
1.1       raeburn  2647:         } else {
1.2       raeburn  2648:             $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
1.1       raeburn  2649:         }
1.55      raeburn  2650:         if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
1.2       raeburn  2651:             $r->print(&Apache::loncommon::start_data_table_row());
1.11      raeburn  2652:             my $checkval;
1.16      raeburn  2653:             if ($mode eq 'autoenroll') {
                   2654:                 my $cellentry;
                   2655:                 if ($in{'type'} eq 'auto') {
                   2656:                     $cellentry = '<b>'.&mt('auto').'</b>&nbsp;<label><input type="checkbox" name="chgauto" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;Change</label>';
                   2657:                     $autocount ++;
                   2658:                 } else {
1.76      bisitz   2659:                     $cellentry = '<table border="0" cellspacing="0"><tr><td rowspan="2"><b>'.&mt('manual').'</b></td><td><span class="LC_nobreak"><label><input type="checkbox" name="chgmanual" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;Change</label></span></td></tr><tr><td><span class="LC_nobreak">';
1.16      raeburn  2660:                     $manualcount ++;
                   2661:                     if ($in{'lockedtype'}) {
                   2662:                         $cellentry .= '<label><input type="checkbox" name="unlockchg" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;'.&mt('Unlock').'</label>';
                   2663:                         $unlockcount ++;
                   2664:                     } else {
                   2665:                         $cellentry .= '<label><input type="checkbox" name="lockchg" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;'.&mt('Lock').'</label>';
                   2666:                         $lockcount ++;
1.11      raeburn  2667:                     }
1.76      bisitz   2668:                     $cellentry .= '</span></td></tr></table>';
1.16      raeburn  2669:                 }
                   2670:                 $r->print("<td>$cellentry</td>\n");
                   2671:             } else {
1.55      raeburn  2672:                 if ($mode ne 'pickauthor') {  
                   2673:                     $r->print("<td>$rowcount</td>\n");
                   2674:                 }
1.16      raeburn  2675:                 if ($actionselect) {
1.26      raeburn  2676:                     my $showcheckbox;
                   2677:                     if ($role =~ /^cr\//) {
                   2678:                         $showcheckbox = $canchange{'cr'};
                   2679:                     } else {
                   2680:                         $showcheckbox = $canchange{$role};
                   2681:                     }
                   2682:                     if (!$showcheckbox) {
                   2683:                         if ($context eq 'course') {
                   2684:                             if ($canchangesec{$role} ne '') {
                   2685:                                 if ($canchangesec{$role} eq $in{'section'}) {
                   2686:                                     $showcheckbox = 1;
                   2687:                                 }
                   2688:                             }
1.16      raeburn  2689:                         }
1.26      raeburn  2690:                     }
                   2691:                     if ($showcheckbox) {
                   2692:                         $checkval = $user; 
                   2693:                         if ($context eq 'course') {
                   2694:                             if ($role eq 'st') {
                   2695:                                 $checkval .= ':st';
                   2696:                             }
                   2697:                             $checkval .= ':'.$in{'section'};
                   2698:                             if ($role eq 'st') {
                   2699:                                 $checkval .= ':'.$in{'type'}.':'.
                   2700:                                              $in{'lockedtype'};
                   2701:                             }
1.16      raeburn  2702:                         }
1.26      raeburn  2703:                         $r->print('<td><input type="checkbox" name="'.
1.86      bisitz   2704:                                   'actionlist" value="'.$checkval.'" /></td>');
1.26      raeburn  2705:                     } else {
                   2706:                         $r->print('<td>&nbsp;</td>');
1.16      raeburn  2707:                     }
1.55      raeburn  2708:                 } elsif ($mode eq 'pickauthor') {
                   2709:                         $r->print('<td><input type="button" name="chooseauthor" onclick="javascript:gochoose('."'$in{'username'}'".');" value="'.&mt('Select').'" /></td>');
1.11      raeburn  2710:                 }
                   2711:             }
1.2       raeburn  2712:             foreach my $item (@cols) {
1.10      raeburn  2713:                 if ($item eq 'username') {
1.48      raeburn  2714:                     $r->print('<td>'.&print_username_link($mode,\%in).'</td>');
1.16      raeburn  2715:                 } elsif (($item eq 'start' || $item eq 'end') && ($actionselect)) {
1.11      raeburn  2716:                     $r->print('<td>'.$in{$item}.'<input type="hidden" name="'.$checkval.'_'.$item.'" value="'.$sdata->[$index{$item}].'" /></td>'."\n");
1.83      raeburn  2717:                 } elsif ($item eq 'status') {
                   2718:                     my $showitem = $in{$item};
                   2719:                     if (defined($ltstatus{$in{$item}})) {
                   2720:                         $showitem = $ltstatus{$in{$item}};
                   2721:                     }
                   2722:                     $r->print('<td>'.$showitem.'</td>'."\n");
1.10      raeburn  2723:                 } else {
                   2724:                     $r->print('<td>'.$in{$item}.'</td>'."\n");
                   2725:                 }
1.2       raeburn  2726:             }
1.16      raeburn  2727:             if (($context eq 'course') && ($mode ne 'autoenroll')) {
1.4       raeburn  2728:                 if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
                   2729:                     if ($displayclickers eq 'on') {
                   2730:                         my $clickers =
1.2       raeburn  2731:                    (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
1.4       raeburn  2732:                         if ($clickers!~/\w/) { $clickers='-'; }
                   2733:                         $r->print('<td>'.$clickers.'</td>');
1.2       raeburn  2734:                     } else {
                   2735:                         $r->print('    <td>&nbsp;</td>  ');
                   2736:                     }
1.4       raeburn  2737:                     if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.44      raeburn  2738:                         if ($displayphotos eq 'on' && $role eq 'st' && $in{'photo'} ne '') {
1.90      bisitz   2739:                             $r->print('    <td align="right"><a href="javascript:photowindow('."'".$in{'photo'}."'".')"><img src="'.$in{'thumbnail'}.'" border="1" alt="" /></a></td>');
1.4       raeburn  2740:                         } else {
                   2741:                             $r->print('    <td>&nbsp;</td>  ');
                   2742:                         }
                   2743:                     }
1.2       raeburn  2744:                 }
                   2745:             }
                   2746:             $r->print(&Apache::loncommon::end_data_table_row());
                   2747:         } elsif ($mode eq 'csv') {
                   2748:             next if (! defined($CSVfile));
                   2749:             # no need to bother with $linkto
                   2750:             if (! defined($in{'start'}) || $in{'start'} == 0) {
                   2751:                 $in{'start'} = &mt('none');
                   2752:             } else {
                   2753:                 $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
                   2754:             }
                   2755:             if (! defined($in{'end'}) || $in{'end'} == 0) {
                   2756:                 $in{'end'} = &mt('none');
                   2757:             } else {
                   2758:                 $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
                   2759:             }
                   2760:             my @line = ();
                   2761:             foreach my $item (@cols) {
                   2762:                 push @line,&Apache::loncommon::csv_translate($in{$item});
                   2763:             }
1.67      droeschl 2764:             print $CSVfile '"'.join('","',@line)."\"\n";
1.2       raeburn  2765:         } elsif ($mode eq 'excel') {
                   2766:             my $col = 0;
                   2767:             foreach my $item (@cols) {
                   2768:                 if ($item eq 'start' || $item eq 'end') {
                   2769:                     if (defined($item) && $item != 0) {
                   2770:                         $excel_sheet->write($row,$col++,
                   2771:                             &Apache::lonstathelpers::calc_serial($in{item}),
                   2772:                                     $format->{'date'});
                   2773:                     } else {
                   2774:                         $excel_sheet->write($row,$col++,'none');
                   2775:                     }
                   2776:                 } else {
                   2777:                     $excel_sheet->write($row,$col++,$in{$item});
                   2778:                 }
                   2779:             }
                   2780:             $row++;
1.1       raeburn  2781:         }
                   2782:     }
1.55      raeburn  2783:     if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
1.2       raeburn  2784:             $r->print(&Apache::loncommon::end_data_table().'<br />');
                   2785:     } elsif ($mode eq 'excel') {
                   2786:         $excel_workbook->close();
1.68      droeschl 2787: 	$r->print(&mt('[_1]Your Excel spreadsheet[_2] is ready for download.', '<p><a href="'.$excel_filename.'">','</a>')."</p>\n");
1.2       raeburn  2788:     } elsif ($mode eq 'csv') {
                   2789:         close($CSVfile);
1.68      droeschl 2790: 	$r->print(&mt('[_1]Your CSV file[_2] is ready for download.', '<p><a href="'.$CSVfilename.'">','</a>')."</p>\n");
1.2       raeburn  2791:         $r->rflush();
                   2792:     }
                   2793:     if ($mode eq 'autoenroll') {
                   2794:         return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
1.4       raeburn  2795:     } else {
                   2796:         return ($usercount);
1.2       raeburn  2797:     }
1.1       raeburn  2798: }
                   2799: 
1.56      raeburn  2800: sub bulkaction_javascript {
                   2801:     my ($formname,$caller) = @_;
                   2802:     my $docstart = 'document';
                   2803:     if ($caller eq 'popup') {
                   2804:         $docstart = 'opener.document';
                   2805:     }
                   2806:     my %lt = &Apache::lonlocal::texthash(
                   2807:               acwi => 'Access will be set to start immediately',
                   2808:               asyo => 'as you did not select an end date in the pop-up window',
                   2809:               accw => 'Access will be set to continue indefinitely',
                   2810:               asyd => 'as you did not select an end date in the pop-up window',
                   2811:               sewi => "Sections will be switched to 'No section'",
                   2812:               ayes => "as you either selected the 'No section' option",
                   2813:               oryo => 'or you did not select a section in the pop-up window',
                   2814:               arol => 'A role with no section will be added',
                   2815:               swbs => 'Sections will be switched to:',
                   2816:               rwba => 'Roles will be added for section(s):',
                   2817:             );
                   2818:     my $alert = &mt("You must select at least one user by checking a user's 'Select' checkbox");
                   2819:     my $noaction = &mt("You need to select an action to take for the user(s) you have selected"); 
                   2820:     my $singconfirm = &mt(' for a single user?');
                   2821:     my $multconfirm = &mt(' for multiple users?');
                   2822:     my $output = <<"ENDJS";
                   2823: function verify_action (field) {
                   2824:     var numchecked = 0;
                   2825:     var singconf = '$singconfirm';
                   2826:     var multconf = '$multconfirm';
                   2827:     if ($docstart.$formname.elements[field].length > 0) {
                   2828:         for (i=0; i<$docstart.$formname.elements[field].length; i++) {
                   2829:             if ($docstart.$formname.elements[field][i].checked == true) {
                   2830:                numchecked ++;
                   2831:             }
                   2832:         }
                   2833:     } else {
                   2834:         if ($docstart.$formname.elements[field].checked == true) {
                   2835:             numchecked ++;
                   2836:         }
                   2837:     }
                   2838:     if (numchecked == 0) {
                   2839:         alert("$alert");
                   2840:         return;
                   2841:     } else {
                   2842:         var message = $docstart.$formname.bulkaction[$docstart.$formname.bulkaction.selectedIndex].text;
                   2843:         var choice = $docstart.$formname.bulkaction[$docstart.$formname.bulkaction.selectedIndex].value;
                   2844:         if (choice == '') {
                   2845:             alert("$noaction");
                   2846:             return;
                   2847:         } else {
                   2848:             if (numchecked == 1) {
                   2849:                 message += singconf;
                   2850:             } else {
                   2851:                 message += multconf;
                   2852:             }
                   2853: ENDJS
                   2854:     if ($caller ne 'popup') {
                   2855:         $output .= <<"NEWWIN";
                   2856:             if (choice == 'chgdates' || choice == 'reenable' || choice == 'activate' || choice == 'chgsec') {
                   2857:                 opendatebrowser(document.$formname,'$formname','go');
                   2858:                 return;
                   2859: 
                   2860:             } else {
                   2861:                 if (confirm(message)) {
                   2862:                     document.$formname.phase.value = 'bulkchange';
                   2863:                     document.$formname.submit();
                   2864:                     return;
                   2865:                 }
                   2866:             }
                   2867: NEWWIN
                   2868:     } else {
                   2869:         $output .= <<"POPUP";
                   2870:             if (choice == 'chgdates' || choice == 'reenable' || choice == 'activate') {
                   2871:                 var datemsg = '';
                   2872:                 if (($docstart.$formname.startdate_month.value == '') &&
                   2873:                     ($docstart.$formname.startdate_day.value  == '') &&
                   2874:                     ($docstart.$formname.startdate_year.value == '')) {
                   2875:                     datemsg = "\\n$lt{'acwi'},\\n$lt{'asyo'}.\\n";
                   2876:                 }
                   2877:                 if (($docstart.$formname.enddate_month.value == '') &&
                   2878:                     ($docstart.$formname.enddate_day.value  == '') &&
                   2879:                     ($docstart.$formname.enddate_year.value == '')) {
                   2880:                     datemsg += "\\n$lt{'accw'},\\n$lt{'asyd'}.\\n";
                   2881:                 }
                   2882:                 if (datemsg != '') {
                   2883:                     message += "\\n"+datemsg;
                   2884:                 }
                   2885:             }
                   2886:             if (choice == 'chgsec') {
                   2887:                 var rolefilter = $docstart.$formname.showrole.options[$docstart.$formname.showrole.selectedIndex].value;
                   2888:                 var retained =  $docstart.$formname.retainsec.value;
                   2889:                 var secshow = $docstart.$formname.newsecs.value;
                   2890:                 if (secshow == '') {
                   2891:                     if (rolefilter == 'st' || retained == 0 || retained == "") {
                   2892:                         message += "\\n\\n$lt{'sewi'},\\n$lt{'ayes'},\\n$lt{'oryo'}.\\n";
                   2893:                     } else {
                   2894:                         message += "\\n\\n$lt{'arol'}\\n$lt{'ayes'},\\n$lt{'oryo'}.\\n";
                   2895:                     }
                   2896:                 } else {
                   2897:                     if (rolefilter == 'st' || retained == 0 || retained == "") {
                   2898:                         message += "\\n\\n$lt{'swbs'} "+secshow+".\\n";
                   2899:                     } else {
                   2900:                         message += "\\n\\n$lt{'rwba'} "+secshow+".\\n";
                   2901:                     }
                   2902:                 }
                   2903:             }
                   2904:             if (confirm(message)) {
                   2905:                 $docstart.$formname.phase.value = 'bulkchange';
                   2906:                 $docstart.$formname.submit();
                   2907:                 window.close();
                   2908:             }
                   2909: POPUP
                   2910:     }
                   2911:     $output .= '
                   2912:         }
                   2913:     }
                   2914: }
                   2915: ';
                   2916:     return $output;
                   2917: }
                   2918: 
1.10      raeburn  2919: sub print_username_link {
1.48      raeburn  2920:     my ($mode,$in) = @_;
1.10      raeburn  2921:     my $output;
1.16      raeburn  2922:     if ($mode eq 'autoenroll') {
                   2923:         $output = $in->{'username'};
1.10      raeburn  2924:     } else {
                   2925:         $output = '<a href="javascript:username_display_launch('.
                   2926:                   "'$in->{'username'}','$in->{'domain'}'".')" />'.
                   2927:                   $in->{'username'}.'</a>';
                   2928:     }
                   2929:     return $output;
                   2930: }
                   2931: 
1.2       raeburn  2932: sub role_type_names {
                   2933:     my %lt = &Apache::lonlocal::texthash (
1.13      raeburn  2934:                          'domain' => 'Domain Roles',
                   2935:                          'author' => 'Co-Author Roles',
                   2936:                          'course' => 'Course Roles',
1.97.2.4  raeburn  2937:                          'community' => 'Community Roles',
                   2938: 
1.2       raeburn  2939:              );
                   2940:     return %lt;
                   2941: }
                   2942: 
1.11      raeburn  2943: sub select_actions {
1.55      raeburn  2944:     my ($context,$setting,$statusmode,$formname) = @_;
1.11      raeburn  2945:     my %lt = &Apache::lonlocal::texthash(
                   2946:                 revoke   => "Revoke user roles",
                   2947:                 delete   => "Delete user roles",
                   2948:                 reenable => "Re-enable expired user roles",
                   2949:                 activate => "Make future user roles active now",
                   2950:                 chgdates  => "Change starting/ending dates",
                   2951:                 chgsec   => "Change section associated with user roles",
                   2952:     );
                   2953:     my ($output,$options,%choices);
1.23      raeburn  2954:     # FIXME Disable actions for now for roletype=course in domain context
                   2955:     if ($context eq 'domain' && $setting eq 'course') {
                   2956:         return;
                   2957:     }
1.26      raeburn  2958:     if ($context eq 'course') {
                   2959:         if ($env{'form.showrole'} ne 'Any') {
                   2960:              if (!&Apache::lonnet::allowed('c'.$env{'form.showrole'},
                   2961:                                            $env{'request.course.id'})) {
                   2962:                  if ($env{'request.course.sec'} eq '') {
                   2963:                      return;
                   2964:                  } else {
                   2965:                      if (!&Apache::lonnet::allowed('c'.$env{'form.showrole'},$env{'request.course.id'}.'/'.$env{'request.course.sec'})) {
                   2966:                          return;
                   2967:                      }
                   2968:                  }
                   2969:             }
                   2970:         }
                   2971:     }
1.11      raeburn  2972:     if ($statusmode eq 'Any') {
                   2973:         $options .= '
                   2974: <option value="chgdates">'.$lt{'chgdates'}.'</option>';
                   2975:         $choices{'dates'} = 1;
                   2976:     } else {
                   2977:         if ($statusmode eq 'Future') {
                   2978:             $options .= '
                   2979: <option value="activate">'.$lt{'activate'}.'</option>';
                   2980:             $choices{'dates'} = 1;
                   2981:         } elsif ($statusmode eq 'Expired') {
                   2982:             $options .= '
                   2983: <option value="reenable">'.$lt{'reenable'}.'</option>';
                   2984:             $choices{'dates'} = 1;
                   2985:         }
1.13      raeburn  2986:         if ($statusmode eq 'Active' || $statusmode eq 'Future') {
                   2987:             $options .= '
                   2988: <option value="chgdates">'.$lt{'chgdates'}.'</option>
                   2989: <option value="revoke">'.$lt{'revoke'}.'</option>';
                   2990:             $choices{'dates'} = 1;
                   2991:         }
1.11      raeburn  2992:     }
                   2993:     if ($context eq 'domain') {
                   2994:         $options .= '
                   2995: <option value="delete">'.$lt{'delete'}.'</option>';
                   2996:     }
                   2997:     if (($context eq 'course') || ($context eq 'domain' && $setting eq 'course')) {
1.26      raeburn  2998:         if (($statusmode ne 'Expired') && ($env{'request.course.sec'} eq '')) {
1.11      raeburn  2999:             $options .= '
                   3000: <option value="chgsec">'.$lt{'chgsec'}.'</option>';
                   3001:             $choices{'sections'} = 1;
                   3002:         }
                   3003:     }
                   3004:     if ($options) {
1.56      raeburn  3005:         $output = '<select name="bulkaction">'."\n".
1.11      raeburn  3006:                   '<option value="" selected="selected">'.
                   3007:                   &mt('Please select').'</option>'."\n".$options."\n".'</select>';
                   3008:         if ($choices{'dates'}) {
                   3009:             $output .= 
                   3010:                 '<input type="hidden" name="startdate_month" value="" />'."\n".
                   3011:                 '<input type="hidden" name="startdate_day" value="" />'."\n".
                   3012:                 '<input type="hidden" name="startdate_year" value="" />'."\n".
                   3013:                 '<input type="hidden" name="startdate_hour" value="" />'."\n".
                   3014:                 '<input type="hidden" name="startdate_minute" value="" />'."\n".
                   3015:                 '<input type="hidden" name="startdate_second" value="" />'."\n".
                   3016:                 '<input type="hidden" name="enddate_month" value="" />'."\n".
                   3017:                 '<input type="hidden" name="enddate_day" value="" />'."\n".
                   3018:                 '<input type="hidden" name="enddate_year" value="" />'."\n".
                   3019:                 '<input type="hidden" name="enddate_hour" value="" />'."\n".
                   3020:                 '<input type="hidden" name="enddate_minute" value="" />'."\n".
1.56      raeburn  3021:                 '<input type="hidden" name="enddate_second" value="" />'."\n".
                   3022:                 '<input type="hidden" name="no_end_date" value="" />'."\n";
1.11      raeburn  3023:             if ($context eq 'course') {
                   3024:                 $output .= '<input type="hidden" name="makedatesdefault" value="" />'."\n";
                   3025:             }
                   3026:         }
                   3027:         if ($choices{'sections'}) {
1.91      bisitz   3028:             $output .= '<input type="hidden" name="retainsec" value="" />'."\n".
                   3029:                        '<input type="hidden" name="newsecs" value="" />'."\n";
1.11      raeburn  3030:         }
                   3031:     }
                   3032:     return $output;
                   3033: }
                   3034: 
                   3035: sub date_section_javascript {
                   3036:     my ($context,$setting) = @_;
1.49      raeburn  3037:     my $title = 'Date_And_Section_Selector';
1.41      raeburn  3038:     my %nopopup = &Apache::lonlocal::texthash (
                   3039:         revoke => "Check the boxes for any users for whom roles are to be revoked, and click 'Proceed'",
                   3040:         delete => "Check the boxes for any users for whom roles are to be deleted, and click 'Proceed'",
                   3041:         none   => "Choose an action to take for selected users",
                   3042:     );  
1.96      bisitz   3043:     my $output = <<"ENDONE";
                   3044: <script type="text/javascript">
                   3045: // <![CDATA[
1.41      raeburn  3046:     function opendatebrowser(callingform,formname,calledby) {
1.11      raeburn  3047:         var bulkaction = callingform.bulkaction.options[callingform.bulkaction.selectedIndex].value;
                   3048:         var url = '/adm/createuser?';
                   3049:         var type = '';
                   3050:         var showrole = callingform.showrole.options[callingform.showrole.selectedIndex].value;
                   3051: ENDONE
                   3052:     if ($context eq 'domain') {
                   3053:         $output .= '
                   3054:         type = callingform.roletype.options[callingform.roletype.selectedIndex].value;
                   3055: ';
                   3056:     }
                   3057:     my $width= '700';
                   3058:     my $height = '400';
                   3059:     $output .= <<"ENDTWO";
                   3060:         url += 'action=dateselect&callingform=' + formname + 
                   3061:                '&roletype='+type+'&showrole='+showrole +'&bulkaction='+bulkaction;
                   3062:         var title = '$title';
                   3063:         var options = 'scrollbars=1,resizable=1,menubar=0';
                   3064:         options += ',width=$width,height=$height';
                   3065:         stdeditbrowser = open(url,title,options,'1');
                   3066:         stdeditbrowser.focus();
                   3067:     }
1.96      bisitz   3068: // ]]>
1.11      raeburn  3069: </script>
                   3070: ENDTWO
                   3071:     return $output;
                   3072: }
                   3073: 
                   3074: sub date_section_selector {
1.97.2.4  raeburn  3075:     my ($context,$permission,$crstype) = @_;
1.11      raeburn  3076:     my $callingform = $env{'form.callingform'};
                   3077:     my $formname = 'dateselect';  
                   3078:     my $groupslist = &get_groupslist();
                   3079:     my $sec_js = &setsections_javascript($formname,$groupslist);
                   3080:     my $output = <<"END";
                   3081: <script type="text/javascript">
1.96      bisitz   3082: // <![CDATA[
1.11      raeburn  3083: 
                   3084: $sec_js
                   3085: 
                   3086: function saveselections(formname) {
                   3087: 
                   3088: END
                   3089:     if ($env{'form.bulkaction'} eq 'chgsec') {
                   3090:         $output .= <<"END";
1.40      raeburn  3091:         if (formname.retainsec.length > 1) {  
                   3092:             for (var i=0; i<formname.retainsec.length; i++) {
                   3093:                 if (formname.retainsec[i].checked == true) {
                   3094:                     opener.document.$callingform.retainsec.value = formname.retainsec[i].value;
                   3095:                 }
                   3096:             }
                   3097:         } else {
                   3098:             opener.document.$callingform.retainsec.value = formname.retainsec.value;
                   3099:         }
1.11      raeburn  3100:         setSections(formname);
                   3101:         if (seccheck == 'ok') {
                   3102:             opener.document.$callingform.newsecs.value = formname.sections.value;
                   3103:         }
                   3104: END
                   3105:     } else {
                   3106:         if ($context eq 'course') {
                   3107:             if (($env{'form.bulkaction'} eq 'reenable') || 
                   3108:                 ($env{'form.bulkaction'} eq 'activate') || 
                   3109:                 ($env{'form.bulkaction'} eq 'chgdates')) {
1.26      raeburn  3110:                 if ($env{'request.course.sec'} eq '') {
                   3111:                     $output .= <<"END";
1.11      raeburn  3112:  
                   3113:         if (formname.makedatesdefault.checked == true) {
                   3114:             opener.document.$callingform.makedatesdefault.value = 1;
                   3115:         }
                   3116:         else {
                   3117:             opener.document.$callingform.makedatesdefault.value = 0;
                   3118:         }
                   3119: 
                   3120: END
1.26      raeburn  3121:                 }
1.11      raeburn  3122:             }
                   3123:         }
                   3124:         $output .= <<"END";
                   3125:     opener.document.$callingform.startdate_month.value =  formname.startdate_month.options[formname.startdate_month.selectedIndex].value;
                   3126:     opener.document.$callingform.startdate_day.value =  formname.startdate_day.value;
                   3127:     opener.document.$callingform.startdate_year.value = formname.startdate_year.value;
                   3128:     opener.document.$callingform.startdate_hour.value =  formname.startdate_hour.options[formname.startdate_hour.selectedIndex].value;
                   3129:     opener.document.$callingform.startdate_minute.value =  formname.startdate_minute.value;
                   3130:     opener.document.$callingform.startdate_second.value = formname.startdate_second.value;
                   3131:     opener.document.$callingform.enddate_month.value =  formname.enddate_month.options[formname.enddate_month.selectedIndex].value;
                   3132:     opener.document.$callingform.enddate_day.value =  formname.enddate_day.value;
                   3133:     opener.document.$callingform.enddate_year.value = formname.enddate_year.value;
                   3134:     opener.document.$callingform.enddate_hour.value =  formname.enddate_hour.options[formname.enddate_hour.selectedIndex].value;
                   3135:     opener.document.$callingform.enddate_minute.value =  formname.enddate_minute.value;
                   3136:     opener.document.$callingform.enddate_second.value = formname.enddate_second.value;
1.56      raeburn  3137:     if (formname.no_end_date.checked) {
                   3138:         opener.document.$callingform.no_end_date.value = '1';
                   3139:     } else {
                   3140:         opener.document.$callingform.no_end_date.value = '0';
                   3141:     }
1.11      raeburn  3142: END
                   3143:     }
1.56      raeburn  3144:     my $verify_action_js = &bulkaction_javascript($callingform,'popup');
                   3145:     $output .= <<"ENDJS";
                   3146:     verify_action('actionlist');
1.11      raeburn  3147: }
1.56      raeburn  3148: 
                   3149: $verify_action_js
                   3150: 
1.96      bisitz   3151: // ]]>
1.11      raeburn  3152: </script>
1.56      raeburn  3153: ENDJS
1.11      raeburn  3154:     my %lt = &Apache::lonlocal::texthash (
                   3155:                  chac => 'Access dates to apply for selected users',
                   3156:                  chse => 'Changes in section affiliation to apply to selected users',
                   3157:                  fors => 'For student roles changing the section, will result in a section switch as students may only be in one section of a course at a time.',
                   3158:                  forn => 'For a role in a course that is not a student role, a user may have roles in more than one section of a course at a time.',
                   3159:                  reta => "Retain each user's current section affiliations?", 
                   3160:                  dnap => '(Does not apply to student roles).', 
                   3161:             );
                   3162:     my ($date_items,$headertext);
                   3163:     if ($env{'form.bulkaction'} eq 'chgsec') {
                   3164:         $headertext = $lt{'chse'};
                   3165:     } else {
                   3166:         $headertext = $lt{'chac'};
                   3167:         my $starttime;
                   3168:         if (($env{'form.bulkaction'} eq 'activate') || 
                   3169:             ($env{'form.bulkaction'} eq 'reenable')) {
                   3170:             $starttime = time;
                   3171:         }
                   3172:         $date_items = &date_setting_table($starttime,undef,$context,
1.21      raeburn  3173:                                           $env{'form.bulkaction'},$formname,
1.97.2.4  raeburn  3174:                                           $permission,$crstype);
1.11      raeburn  3175:     }
                   3176:     $output .= '<h3>'.$headertext.'</h3>'.
                   3177:                '<form name="'.$formname.'" method="post">'."\n".
                   3178:                 $date_items;
                   3179:     if ($context eq 'course' && $env{'form.bulkaction'} eq 'chgsec') {
1.17      raeburn  3180:         my ($cnum,$cdom) = &get_course_identity();
1.11      raeburn  3181:         my $info;
                   3182:         if ($env{'form.showrole'} eq 'st') {
                   3183:             $output .= '<p>'.$lt{'fors'}.'</p>'; 
1.26      raeburn  3184:         } elsif ($env{'form.showrole'} eq 'Any') {
1.11      raeburn  3185:             $output .= '<p>'.$lt{'fors'}.'</p>'.
                   3186:                        '<p>'.$lt{'forn'}.'&nbsp;';
                   3187:             $info = $lt{'reta'};
                   3188:         } else {
                   3189:             $output .= '<p>'.$lt{'forn'}.'&nbsp;';
                   3190:             $info = $lt{'reta'};
                   3191:         }
                   3192:         if ($info) {
                   3193:             $info .= '<span class="LC_nobreak">'.
                   3194:                      '<label><input type="radio" name="retainsec" value="1" '.
                   3195:                      'checked="checked" />'.&mt('Yes').'</label>&nbsp;&nbsp;'.
                   3196:                      '<label><input type="radio" name="retainsec" value="0" />'.
                   3197:                      &mt('No').'</label></span>';
                   3198:             if ($env{'form.showrole'} eq 'Any') {
                   3199:                 $info .= '<br />'.$lt{'dnap'};
                   3200:             }
                   3201:             $info .= '</p>';
                   3202:         } else {
                   3203:             $info = '<input type="hidden" name="retainsec" value="0" />'; 
                   3204:         }
1.21      raeburn  3205:         my $rowtitle = &mt('New section to assign');
1.97.2.4  raeburn  3206:         my $secbox = &section_picker($cdom,$cnum,$env{'form.showrole'},$rowtitle,$permission,$context,'',$crstype);
1.11      raeburn  3207:         $output .= $info.$secbox;
                   3208:     }
                   3209:     $output .= '<p>'.
1.81      schafran 3210: '<input type="button" name="dateselection" value="'.&mt('Save').'" onclick="javascript:saveselections(this.form)" /></p>'."\n".
1.11      raeburn  3211: '</form>';
                   3212:     return $output;
                   3213: }
                   3214: 
1.17      raeburn  3215: sub section_picker {
1.97.2.4  raeburn  3216:     my ($cdom,$cnum,$role,$rowtitle,$permission,$context,$mode,$crstype) = @_;
1.17      raeburn  3217:     my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   3218:     my $sections_select .= &course_sections(\%sections_count,$role);
                   3219:     my $secbox = '<p>'.&Apache::lonhtmlcommon::start_pick_box()."\n";
                   3220:     if ($mode eq 'upload') {
                   3221:         my ($options,$cb_script,$coursepick) =
1.97.2.4  raeburn  3222:             &default_role_selector($context,1,$crstype);
1.59      bisitz   3223:         $secbox .= &Apache::lonhtmlcommon::row_title(&mt('role'),'LC_oddrow_value').
1.17      raeburn  3224:                    $options. &Apache::lonhtmlcommon::row_closure(1)."\n";
                   3225:     }
                   3226:     $secbox .= &Apache::lonhtmlcommon::row_title($rowtitle,'LC_oddrow_value')."\n";
                   3227:     if ($env{'request.course.sec'} eq '') {
                   3228:         $secbox .= '<table class="LC_createuser"><tr class="LC_section_row">'."\n".
                   3229:                    '<td align="center">'.&mt('Existing sections')."\n".
                   3230:                    '<br />'.$sections_select.'</td><td align="center">'.
                   3231:                    &mt('New section').'<br />'."\n".
                   3232:                    '<input type="text" name="newsec" size="15" />'."\n".
                   3233:                    '<input type="hidden" name="sections" value="" />'."\n".
                   3234:                    '</td></tr></table>'."\n";
                   3235:     } else {
                   3236:        $secbox .= '<input type="hidden" name="sections" value="'.
                   3237:                    $env{'request.course.sec'}.'" />'.
                   3238:                    $env{'request.course.sec'};
                   3239:     }
                   3240:     $secbox .= &Apache::lonhtmlcommon::row_closure(1)."\n".
                   3241:                &Apache::lonhtmlcommon::end_pick_box().'</p>';
                   3242:     return $secbox;
                   3243: }
                   3244: 
1.2       raeburn  3245: sub results_header_row {
1.97.2.5! raeburn  3246:     my ($rolefilter,$statusmode,$context,$permission,$mode,$crstype) = @_;
1.5       raeburn  3247:     my ($description,$showfilter);
                   3248:     if ($rolefilter ne 'Any') {
                   3249:         $showfilter = $rolefilter;
                   3250:     }
1.2       raeburn  3251:     if ($context eq 'course') {
1.24      raeburn  3252:         if ($mode eq 'csv' || $mode eq 'excel') {
1.97.2.5! raeburn  3253:             if ($crstype eq 'Community') {
        !          3254:                 $description = &mt('Community - [_1]:',$env{'course.'.$env{'request.course.id'}.'.description'}).' ';
        !          3255:             } else {
        !          3256:                 $description = &mt('Course - [_1]:',$env{'course.'.$env{'request.course.id'}.'.description'}).' ';
        !          3257:             }
1.24      raeburn  3258:         }
1.2       raeburn  3259:         if ($statusmode eq 'Expired') {
1.97.2.5! raeburn  3260:             if ($crstype eq 'Community') {
        !          3261:                 $description .= &mt('Users in community with expired [_1] roles',$showfilter);
        !          3262:             } else {
        !          3263:                 $description .= &mt('Users in course with expired [_1] roles',$showfilter);
        !          3264:             }
1.11      raeburn  3265:         } elsif ($statusmode eq 'Future') {
1.97.2.5! raeburn  3266:             if ($crstype eq 'Community') {
        !          3267:                 $description .= &mt('Users in community with future [_1] roles',$showfilter);
        !          3268:             } else {
        !          3269:                 $description .= &mt('Users in course with future [_1] roles',$showfilter);
        !          3270:             }
1.2       raeburn  3271:         } elsif ($statusmode eq 'Active') {
1.97.2.5! raeburn  3272:             if ($crstype eq 'Community') {
        !          3273:                 $description .= &mt('Users in community with active [_1] roles',$showfilter);
        !          3274:             } else {
        !          3275:                 $description .= &mt('Users in course with active [_1] roles',$showfilter);
        !          3276:             }
1.2       raeburn  3277:         } else {
                   3278:             if ($rolefilter eq 'Any') {
1.97.2.5! raeburn  3279:                 if ($crstype eq 'Community') {
        !          3280:                     $description .= &mt('All users in community');
        !          3281:                 } else {
        !          3282:                     $description .= &mt('All users in course');
        !          3283:                 }
1.2       raeburn  3284:             } else {
1.97.2.5! raeburn  3285:                 if ($crstype eq 'Community') {
        !          3286:                     $description .= &mt('All users in community with [_1] roles',$rolefilter);
        !          3287:                 } else {
        !          3288:                     $description .= &mt('All users in course with [_1] roles',$rolefilter);
        !          3289:                 }
1.2       raeburn  3290:             }
                   3291:         }
1.33      raeburn  3292:         my $constraint;
1.26      raeburn  3293:         my $viewablesec = &viewable_section($permission);
                   3294:         if ($viewablesec ne '') {
1.15      raeburn  3295:             if ($env{'form.showrole'} eq 'st') {
1.33      raeburn  3296:                 $constraint = &mt('only users in section "[_1]"',$viewablesec);
1.26      raeburn  3297:             } elsif ($env{'form.showrole'} ne 'cc') {
1.33      raeburn  3298:                 $constraint = &mt('only users affiliated with no section or section "[_1]"',$viewablesec);
                   3299:             }
                   3300:             if (($env{'form.grpfilter'} ne 'all') && ($env{'form.grpfilter'} ne '')) {
                   3301:                 if ($env{'form.grpfilter'} eq 'none') {
                   3302:                     $constraint .= &mt(' and not in any group');
                   3303:                 } else {
                   3304:                     $constraint .= &mt(' and members of group: "[_1]"',$env{'form.grpfilter'});
                   3305:                 }
                   3306:             }
                   3307:         } else {
                   3308:             if (($env{'form.secfilter'} ne 'all') && ($env{'form.secfilter'} ne '')) {
                   3309:                 if ($env{'form.secfilter'} eq 'none') {
                   3310:                     $constraint = &mt('only users affiliated with no section');
                   3311:                 } else {
                   3312:                     $constraint = &mt('only users affiliated with section "[_1]"',$env{'form.secfilter'});
                   3313:                 }
                   3314:             }
                   3315:             if (($env{'form.grpfilter'} ne 'all') && ($env{'form.grpfilter'} ne '')) {
                   3316:                 if ($env{'form.grpfilter'} eq 'none') {
                   3317:                     if ($constraint eq '') {
                   3318:                         $constraint = &mt('only users not in any group');
                   3319:                     } else {
                   3320:                         $constraint .= &mt(' and also not in any group'); 
                   3321:                     }
                   3322:                 } else {
                   3323:                     if ($constraint eq '') {
                   3324:                         $constraint = &mt('only members of group: "[_1]"',$env{'form.grpfilter'});
                   3325:                     } else {
                   3326:                         $constraint .= &mt(' and also members of group: "[_1]"'.$env{'form.grpfilter'});
                   3327:                     }
                   3328:                 }
1.15      raeburn  3329:             }
                   3330:         }
1.33      raeburn  3331:         if ($constraint ne '') {
                   3332:             $description .= ' ('.$constraint.')';
                   3333:         } 
1.13      raeburn  3334:     } elsif ($context eq 'author') {
1.14      raeburn  3335:         $description = 
1.73      bisitz   3336:             &mt('Author space for [_1]'
                   3337:                 ,'<span class="LC_cusr_emph">'
                   3338:                 .&Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'})
                   3339:                 .'</span>')
                   3340:             .':&nbsp;&nbsp;';
1.2       raeburn  3341:         if ($statusmode eq 'Expired') {
1.5       raeburn  3342:             $description .= &mt('Co-authors with expired [_1] roles',$showfilter);
1.2       raeburn  3343:         } elsif ($statusmode eq 'Future') {
1.5       raeburn  3344:             $description .= &mt('Co-authors with future [_1] roles',$showfilter);
1.2       raeburn  3345:         } elsif ($statusmode eq 'Active') {
1.5       raeburn  3346:             $description .= &mt('Co-authors with active [_1] roles',$showfilter);
1.2       raeburn  3347:         } else {
                   3348:             if ($rolefilter eq 'Any') {
1.5       raeburn  3349:                 $description .= &mt('All co-authors');
1.2       raeburn  3350:             } else {
                   3351:                 $description .= &mt('All co-authors with [_1] roles',$rolefilter);
                   3352:             }
                   3353:         }
                   3354:     } elsif ($context eq 'domain') {
                   3355:         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
1.73      bisitz   3356:         $description = &mt('Domain - [_1]:',$domdesc).' ';
1.2       raeburn  3357:         if ($env{'form.roletype'} eq 'domain') {
                   3358:             if ($statusmode eq 'Expired') {
1.5       raeburn  3359:                 $description .= &mt('Users in domain with expired [_1] roles',$showfilter);
1.2       raeburn  3360:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  3361:                 $description .= &mt('Users in domain with future [_1] roles',$showfilter);
1.2       raeburn  3362:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  3363:                 $description .= &mt('Users in domain with active [_1] roles',$showfilter);
1.2       raeburn  3364:             } else {
                   3365:                 if ($rolefilter eq 'Any') {
1.5       raeburn  3366:                     $description .= &mt('All users in domain');
1.2       raeburn  3367:                 } else {
                   3368:                     $description .= &mt('All users in domain with [_1] roles',$rolefilter);
                   3369:                 }
                   3370:             }
1.13      raeburn  3371:         } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  3372:             if ($statusmode eq 'Expired') {
1.5       raeburn  3373:                 $description .= &mt('Co-authors in domain with expired [_1] roles',$showfilter);
1.2       raeburn  3374:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  3375:                 $description .= &mt('Co-authors in domain with future [_1] roles',$showfilter);
1.2       raeburn  3376:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  3377:                $description .= &mt('Co-authors in domain with active [_1] roles',$showfilter);
1.2       raeburn  3378:             } else {
                   3379:                 if ($rolefilter eq 'Any') {
1.5       raeburn  3380:                     $description .= &mt('All users with co-author roles in domain',$showfilter);
1.2       raeburn  3381:                 } else {
                   3382:                     $description .= &mt('All co-authors in domain  with [_1] roles',$rolefilter);
                   3383:                 }
                   3384:             }
1.97.2.5! raeburn  3385:         } elsif (($env{'form.roletype'} eq 'course') || 
        !          3386:                  ($env{'form.roletype'} eq 'community')) {
        !          3387: 
1.2       raeburn  3388:             my $coursefilter = $env{'form.coursepick'};
1.97.2.5! raeburn  3389:             if ($env{'form.roletype'} eq 'course') {
        !          3390:                 if ($coursefilter eq 'category') {
        !          3391:                     my $instcode = &instcode_from_coursefilter();
        !          3392:                     if ($instcode eq '.') {
        !          3393:                         $description .= &mt('All courses in domain').' - ';
        !          3394:                     } else {
        !          3395:                         $description .= &mt('Courses in domain with institutional code: [_1]',$instcode).' - ';
        !          3396:                     }
        !          3397:                 } elsif ($coursefilter eq 'selected') {
        !          3398:                     $description .= &mt('Selected courses in domain').' - ';
        !          3399:                 } elsif ($coursefilter eq 'all') {
1.2       raeburn  3400:                     $description .= &mt('All courses in domain').' - ';
                   3401:                 }
1.97.2.5! raeburn  3402:             } elsif ($env{'form.roletype'} eq 'community') {
        !          3403:                 if ($coursefilter eq 'selected') {
        !          3404:                     $description .= &mt('Selected communities in domain').' - ';
        !          3405:                 } elsif ($coursefilter eq 'all') {
        !          3406:                     $description .= &mt('All communities in domain').' - ';
        !          3407:                 }
1.2       raeburn  3408:             }
                   3409:             if ($statusmode eq 'Expired') {
1.5       raeburn  3410:                 $description .= &mt('users with expired [_1] roles',$showfilter);
1.2       raeburn  3411:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  3412:                 $description .= &mt('users with future [_1] roles',$showfilter);
1.2       raeburn  3413:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  3414:                 $description .= &mt('users with active [_1] roles',$showfilter);
1.2       raeburn  3415:             } else {
                   3416:                 if ($rolefilter eq 'Any') {
                   3417:                     $description .= &mt('all users');
                   3418:                 } else {
                   3419:                     $description .= &mt('users with [_1] roles',$rolefilter);
                   3420:                 }
                   3421:             }
                   3422:         }
                   3423:     }
                   3424:     return $description;
                   3425: }
1.22      raeburn  3426: 
                   3427: sub viewable_section {
                   3428:     my ($permission) = @_;
                   3429:     my $viewablesec;
                   3430:     if (ref($permission) eq 'HASH') {
                   3431:         if (exists($permission->{'view_section'})) {
                   3432:             $viewablesec = $permission->{'view_section'};
                   3433:         } elsif (exists($permission->{'cusr_section'})) {
                   3434:             $viewablesec = $permission->{'cusr_section'};
                   3435:         }
                   3436:     }
                   3437:     return $viewablesec;
                   3438: }
                   3439: 
1.2       raeburn  3440:     
1.1       raeburn  3441: #################################################
                   3442: #################################################
                   3443: sub show_drop_list {
1.97.2.4  raeburn  3444:     my ($r,$classlist,$nosort,$permission,$crstype) = @_;
1.29      raeburn  3445:     my $cid = $env{'request.course.id'};
1.17      raeburn  3446:     my ($cnum,$cdom) = &get_course_identity($cid);
1.1       raeburn  3447:     if (! exists($env{'form.sortby'})) {
                   3448:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   3449:                                                 ['sortby']);
                   3450:     }
                   3451:     my $sortby = $env{'form.sortby'};
                   3452:     if ($sortby !~ /^(username|domain|section|groups|fullname|id|start|end)$/) {
                   3453:         $sortby = 'username';
                   3454:     }
                   3455:     my $action = "drop";
1.17      raeburn  3456:     my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
1.1       raeburn  3457:     $r->print(<<END);
                   3458: <input type="hidden" name="sortby" value="$sortby" />
                   3459: <input type="hidden" name="action" value="$action" />
                   3460: <input type="hidden" name="state"  value="done" />
1.17      raeburn  3461: <script type="text/javascript" language="Javascript">
1.96      bisitz   3462: // <![CDATA[
1.17      raeburn  3463: $check_uncheck_js
1.96      bisitz   3464: // ]]>
1.1       raeburn  3465: </script>
                   3466: <p>
1.86      bisitz   3467: <input type="hidden" name="phase" value="four" />
1.1       raeburn  3468: END
1.30      raeburn  3469:     my ($indexhash,$keylist) = &make_keylist_array();
                   3470:     my $studentcount = 0;
                   3471:     if (ref($classlist) eq 'HASH') {
                   3472:         foreach my $student (keys(%{$classlist})) {
                   3473:             my $sdata = $classlist->{$student}; 
                   3474:             my $status = $sdata->[$indexhash->{'status'}];
                   3475:             my $section = $sdata->[$indexhash->{'section'}];
                   3476:             if ($status ne 'Active') {
                   3477:                 delete($classlist->{$student});
                   3478:                 next;
                   3479:             }
                   3480:             if ($env{'request.course.sec'} ne '') {
                   3481:                 if ($section ne $env{'request.course.sec'}) {
                   3482:                     delete($classlist->{$student});
                   3483:                     next;
                   3484:                 }
                   3485:             }
                   3486:             $studentcount ++;
                   3487:         }
                   3488:     }
                   3489:     if (!$studentcount) {
1.97.2.4  raeburn  3490:         if ($crstype eq 'Community') {
                   3491:             $r->print(&mt('There are no members to drop.'));
                   3492:         } else {
                   3493:             $r->print(&mt('There are no students to drop.'));
                   3494:         }
1.30      raeburn  3495:         return;
                   3496:     }
                   3497:     my ($classgroups) = &Apache::loncoursedata::get_group_memberships(
                   3498:                                               $classlist,$keylist,$cdom,$cnum);
                   3499:     my %lt=&Apache::lonlocal::texthash('usrn'   => "username",
                   3500:                                        'dom'    => "domain",
                   3501:                                        'sn'     => "student name",
1.97.2.4  raeburn  3502:                                        'mn'     => "member name",
1.30      raeburn  3503:                                        'sec'    => "section",
                   3504:                                        'start'  => "start date",
                   3505:                                        'end'    => "end date",
                   3506:                                        'groups' => "active groups",
                   3507:                                       );
1.97.2.4  raeburn  3508:     my $nametitle = $lt{'sn'};
                   3509:     if ($crstype eq 'Community') {
                   3510:         $nametitle = $lt{'mn'};
                   3511:     }
1.1       raeburn  3512:     if ($nosort) {
1.17      raeburn  3513:         $r->print(&Apache::loncommon::start_data_table().
                   3514:                   &Apache::loncommon::start_data_table_header_row());
1.1       raeburn  3515:         $r->print(<<END);
                   3516:     <th>&nbsp;</th>
                   3517:     <th>$lt{'usrn'}</th>
                   3518:     <th>$lt{'dom'}</th>
                   3519:     <th>ID</th>
1.97.2.4  raeburn  3520:     <th>$nametitle</th>
1.1       raeburn  3521:     <th>$lt{'sec'}</th>
                   3522:     <th>$lt{'start'}</th>
                   3523:     <th>$lt{'end'}</th>
                   3524:     <th>$lt{'groups'}</th>
                   3525: END
1.17      raeburn  3526:         $r->print(&Apache::loncommon::end_data_table_header_row());
1.1       raeburn  3527:     } else  {
1.17      raeburn  3528:         $r->print(&Apache::loncommon::start_data_table().
                   3529:                   &Apache::loncommon::start_data_table_header_row());
1.1       raeburn  3530:         $r->print(<<END);
1.17      raeburn  3531:     <th>&nbsp;</th>
1.1       raeburn  3532:     <th>
1.17      raeburn  3533:        <a href="/adm/createuser?action=$action&sortby=username">$lt{'usrn'}</a>
1.1       raeburn  3534:     </th><th>
1.17      raeburn  3535:        <a href="/adm/createuser?action=$action&sortby=domain">$lt{'dom'}</a>
1.1       raeburn  3536:     </th><th>
1.17      raeburn  3537:        <a href="/adm/createuser?action=$action&sortby=id">ID</a>
1.1       raeburn  3538:     </th><th>
1.97.2.4  raeburn  3539:        <a href="/adm/createuser?action=$action&sortby=fullname">$nametitle</a>
1.1       raeburn  3540:     </th><th>
1.17      raeburn  3541:        <a href="/adm/createuser?action=$action&sortby=section">$lt{'sec'}</a>
1.1       raeburn  3542:     </th><th>
1.17      raeburn  3543:        <a href="/adm/createuser?action=$action&sortby=start">$lt{'start'}</a>
1.1       raeburn  3544:     </th><th>
1.17      raeburn  3545:        <a href="/adm/createuser?action=$action&sortby=end">$lt{'end'}</a>
1.1       raeburn  3546:     </th><th>
1.17      raeburn  3547:        <a href="/adm/createuser?action=$action&sortby=groups">$lt{'groups'}</a>
1.1       raeburn  3548:     </th>
                   3549: END
1.17      raeburn  3550:         $r->print(&Apache::loncommon::end_data_table_header_row());
1.1       raeburn  3551:     }
                   3552:     #
                   3553:     # Sort the students
1.30      raeburn  3554:     my $index  = $indexhash->{$sortby};
                   3555:     my $second = $indexhash->{'username'};
                   3556:     my $third  = $indexhash->{'domain'};
1.1       raeburn  3557:     my @Sorted_Students = sort {
                   3558:         lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
                   3559:             ||
                   3560:         lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
                   3561:             ||
                   3562:         lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
1.30      raeburn  3563:         } (keys(%{$classlist}));
1.1       raeburn  3564:     foreach my $student (@Sorted_Students) {
                   3565:         my $error;
                   3566:         my $sdata = $classlist->{$student};
1.30      raeburn  3567:         my $username = $sdata->[$indexhash->{'username'}];
                   3568:         my $domain   = $sdata->[$indexhash->{'domain'}];
                   3569:         my $section  = $sdata->[$indexhash->{'section'}];
                   3570:         my $name     = $sdata->[$indexhash->{'fullname'}];
                   3571:         my $id       = $sdata->[$indexhash->{'id'}];
                   3572:         my $start    = $sdata->[$indexhash->{'start'}];
                   3573:         my $end      = $sdata->[$indexhash->{'end'}];
1.1       raeburn  3574:         my $groups = $classgroups->{$student};
                   3575:         my $active_groups;
                   3576:         if (ref($groups->{active}) eq 'HASH') {
                   3577:             $active_groups = join(', ',keys(%{$groups->{'active'}}));
                   3578:         }
                   3579:         if (! defined($start) || $start == 0) {
                   3580:             $start = &mt('none');
                   3581:         } else {
                   3582:             $start = &Apache::lonlocal::locallocaltime($start);
                   3583:         }
                   3584:         if (! defined($end) || $end == 0) {
                   3585:             $end = &mt('none');
                   3586:         } else {
                   3587:             $end = &Apache::lonlocal::locallocaltime($end);
                   3588:         }
1.17      raeburn  3589:         my $studentkey = $student.':'.$section;
1.30      raeburn  3590:         my $startitem = '<input type="hidden" name="'.$studentkey.'_start" value="'.$sdata->[$indexhash->{'start'}].'" />';
1.1       raeburn  3591:         #
                   3592:         $r->print(&Apache::loncommon::start_data_table_row());
                   3593:         $r->print(<<"END");
1.86      bisitz   3594:     <td><input type="checkbox" name="droplist" value="$studentkey" /></td>
1.1       raeburn  3595:     <td>$username</td>
                   3596:     <td>$domain</td>
                   3597:     <td>$id</td>
                   3598:     <td>$name</td>
                   3599:     <td>$section</td>
1.29      raeburn  3600:     <td>$start $startitem</td>
1.1       raeburn  3601:     <td>$end</td>
                   3602:     <td>$active_groups</td>
                   3603: END
                   3604:         $r->print(&Apache::loncommon::end_data_table_row());
                   3605:     }
                   3606:     $r->print(&Apache::loncommon::end_data_table().'<br />');
                   3607:     %lt=&Apache::lonlocal::texthash(
1.29      raeburn  3608:                        'dp'   => "Drop Students",
1.97.2.4  raeburn  3609:                        'dm'   => "Drop Members",
1.1       raeburn  3610:                        'ca'   => "check all",
                   3611:                        'ua'   => "uncheck all",
                   3612:                                        );
1.97.2.4  raeburn  3613:     my $btn = $lt{'dp'};
                   3614:     if ($crstype eq 'Community') {
                   3615:         $btn = $lt{'dm'}; 
                   3616:     }
1.1       raeburn  3617:     $r->print(<<"END");
1.89      bisitz   3618: </p>
                   3619: <p>
1.86      bisitz   3620: <input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.studentform.droplist)" /> &nbsp;
                   3621: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.studentform.droplist)" />
1.89      bisitz   3622: </p>
                   3623: <p>
1.97.2.4  raeburn  3624: <input type="submit" value="$btn" />
1.89      bisitz   3625: </p>
1.1       raeburn  3626: END
                   3627:     return;
                   3628: }
                   3629: 
                   3630: #
                   3631: # Print out the initial form to get the file containing a list of users
                   3632: #
                   3633: sub print_first_users_upload_form {
                   3634:     my ($r,$context) = @_;
                   3635:     my $str;
1.86      bisitz   3636:     $str  = '<input type="hidden" name="phase" value="two" />';
1.1       raeburn  3637:     $str .= '<input type="hidden" name="action" value="upload" />';
1.97.2.4  raeburn  3638:     $str .= '<input type="hidden" name="state"  value="got_file" />';
                   3639: 
1.95      bisitz   3640: 
1.85      bisitz   3641:     $str .= '<h2>'.&mt('Upload a file containing information about users').'</h2>'."\n";
1.95      bisitz   3642: 
                   3643:     # Excel and CSV Help
                   3644:     $str .= '<p>'
                   3645:            .&Apache::loncommon::help_open_topic("Course_Create_Class_List",
                   3646:                 &mt("How do I create a users list from a spreadsheet"))
                   3647:            ."<br />\n"
                   3648:            .&Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
                   3649:                 &mt("How do I create a CSV file from a spreadsheet"))
                   3650:            ."</p>\n";
                   3651: 
                   3652:     $str .= &Apache::lonhtmlcommon::start_pick_box()
                   3653:            .&Apache::lonhtmlcommon::row_title(&mt('File'))
                   3654:            .'<p class="LC_info">'."\n"
                   3655:            .&mt('Please upload an UTF8 encoded file to ensure a correct character encoding in your classlist.')."\n"
                   3656:            .'</p>'."\n"
                   3657:            .&Apache::loncommon::upfile_select_html()
                   3658:            .&Apache::lonhtmlcommon::row_closure()
                   3659:            .&Apache::lonhtmlcommon::row_title(
                   3660:                 '<label for="noFirstLine">'
                   3661:                .&mt('Ignore First Line')
                   3662:                .'</label>')
                   3663:            .'<input type="checkbox" name="noFirstLine" id="noFirstLine" />'
                   3664:            .&Apache::lonhtmlcommon::row_closure(1)
                   3665:            .&Apache::lonhtmlcommon::end_pick_box();
                   3666: 
                   3667:     $str .= '<p>'
                   3668:            .'<input type="submit" name="fileupload" value="'.&mt('Next').'" />'
                   3669:            .'</p>';
                   3670: 
1.1       raeburn  3671:     $str .= &Apache::loncommon::end_page();
1.95      bisitz   3672: 
1.1       raeburn  3673:     $r->print($str);
                   3674:     return;
                   3675: }
                   3676: 
                   3677: # ================================================= Drop/Add from uploaded file
                   3678: sub upfile_drop_add {
1.21      raeburn  3679:     my ($r,$context,$permission) = @_;
1.1       raeburn  3680:     &Apache::loncommon::load_tmp_file($r);
                   3681:     my @userdata=&Apache::loncommon::upfile_record_sep();
                   3682:     if($env{'form.noFirstLine'}){shift(@userdata);}
                   3683:     my @keyfields = split(/\,/,$env{'form.keyfields'});
                   3684:     my %fields=();
                   3685:     for (my $i=0; $i<=$env{'form.nfields'}; $i++) {
                   3686:         if ($env{'form.upfile_associate'} eq 'reverse') {
                   3687:             if ($env{'form.f'.$i} ne 'none') {
                   3688:                 $fields{$keyfields[$i]}=$env{'form.f'.$i};
                   3689:             }
                   3690:         } else {
                   3691:             $fields{$env{'form.f'.$i}}=$keyfields[$i];
                   3692:         }
                   3693:     }
1.29      raeburn  3694:     if ($env{'form.fullup'} ne 'yes') {
                   3695:         $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n".
                   3696:                   '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />');
                   3697:     }
1.1       raeburn  3698:     #
                   3699:     # Store the field choices away
                   3700:     foreach my $field (qw/username names
1.57      raeburn  3701:                        fname mname lname gen id sec ipwd email role domain/) {
1.1       raeburn  3702:         $env{'form.'.$field.'_choice'}=$fields{$field};
                   3703:     }
                   3704:     &Apache::loncommon::store_course_settings('enrollment_upload',
                   3705:                                               { 'username_choice' => 'scalar',
                   3706:                                                 'names_choice' => 'scalar',
                   3707:                                                 'fname_choice' => 'scalar',
                   3708:                                                 'mname_choice' => 'scalar',
                   3709:                                                 'lname_choice' => 'scalar',
                   3710:                                                 'gen_choice' => 'scalar',
                   3711:                                                 'id_choice' => 'scalar',
                   3712:                                                 'sec_choice' => 'scalar',
                   3713:                                                 'ipwd_choice' => 'scalar',
                   3714:                                                 'email_choice' => 'scalar',
1.57      raeburn  3715:                                                 'role_choice'  => 'scalar',
1.84      raeburn  3716:                                                 'domain_choice' => 'scalar',
                   3717:                                                 'inststatus_choice' => 'scalar'});
1.1       raeburn  3718:     #
1.97.2.4  raeburn  3719:     my ($cid,$crstype,$setting);
                   3720:     if ($context eq 'domain') {
                   3721:         $setting = $env{'form.roleaction'};
                   3722:     }
                   3723:     if ($env{'request.course.id'} ne '') {
                   3724:         $cid = $env{'request.course.id'};
                   3725:         $crstype = &Apache::loncommon::course_type();
                   3726:     } elsif ($setting eq 'course') {
                   3727:         if (&Apache::lonnet::is_course($env{'form.dcdomain'},$env{'form.dccourse'})) {
                   3728:             $cid = $env{'form.dcdomain'}.'_'.$env{'form.dccourse'};
                   3729:             $crstype = &Apache::loncommon::course_type($cid);
                   3730:         }
                   3731:     }
1.1       raeburn  3732:     my ($startdate,$enddate) = &get_dates_from_form();
                   3733:     if ($env{'form.makedatesdefault'}) {
1.97.2.4  raeburn  3734:         $r->print(&make_dates_default($startdate,$enddate,$context,$crstype);
1.1       raeburn  3735:     }
                   3736:     # Determine domain and desired host (home server)
1.57      raeburn  3737:     my $defdom=$env{'request.role.domain'};
                   3738:     my $domain;
                   3739:     if ($env{'form.defaultdomain'} ne '') {
                   3740:         $domain = $env{'form.defaultdomain'};
                   3741:     } else {
                   3742:         $domain = $defdom;
                   3743:     }
1.1       raeburn  3744:     my $desiredhost = $env{'form.lcserver'};
                   3745:     if (lc($desiredhost) eq 'default') {
                   3746:         $desiredhost = undef;
                   3747:     } else {
1.57      raeburn  3748:         my %home_servers = &Apache::lonnet::get_servers($defdom,'library');
1.1       raeburn  3749:         if (! exists($home_servers{$desiredhost})) {
                   3750:             $r->print('<span class="LC_error">'.&mt('Error').
                   3751:                       &mt('Invalid home server specified').'</span>');
                   3752:             $r->print(&Apache::loncommon::end_page());
                   3753:             return;
                   3754:         }
                   3755:     }
                   3756:     # Determine authentication mechanism
                   3757:     my $changeauth;
                   3758:     if ($context eq 'domain') {
                   3759:         $changeauth = $env{'form.changeauth'};
                   3760:     }
                   3761:     my $amode  = '';
                   3762:     my $genpwd = '';
                   3763:     if ($env{'form.login'} eq 'krb') {
                   3764:         $amode='krb';
                   3765:         $amode.=$env{'form.krbver'};
                   3766:         $genpwd=$env{'form.krbarg'};
                   3767:     } elsif ($env{'form.login'} eq 'int') {
                   3768:         $amode='internal';
                   3769:         if ((defined($env{'form.intarg'})) && ($env{'form.intarg'})) {
                   3770:             $genpwd=$env{'form.intarg'};
                   3771:         }
                   3772:     } elsif ($env{'form.login'} eq 'loc') {
                   3773:         $amode='localauth';
                   3774:         if ((defined($env{'form.locarg'})) && ($env{'form.locarg'})) {
                   3775:             $genpwd=$env{'form.locarg'};
                   3776:         }
                   3777:     }
                   3778:     if ($amode =~ /^krb/) {
                   3779:         if (! defined($genpwd) || $genpwd eq '') {
                   3780:             $r->print('<span class="Error">'.
                   3781:                       &mt('Unable to enroll users').' '.
                   3782:                       &mt('No Kerberos domain was specified.').'</span></p>');
                   3783:             $amode = ''; # This causes the loop below to be skipped
                   3784:         }
                   3785:     }
1.97.2.4  raeburn  3786:     my ($defaultsec,$defaultrole);
1.1       raeburn  3787:     if ($context eq 'domain') {
                   3788:         if ($setting eq 'domain') {
                   3789:             $defaultrole = $env{'form.defaultrole'};
                   3790:         } elsif ($setting eq 'course') {
                   3791:             $defaultrole = $env{'form.courserole'};
1.27      raeburn  3792:             $defaultsec = $env{'form.sections'};
1.97.2.4  raeburn  3793:         }
1.13      raeburn  3794:     } elsif ($context eq 'author') {
1.1       raeburn  3795:         $defaultrole = $env{'form.defaultrole'};
1.27      raeburn  3796:     } elsif ($context eq 'course') {
                   3797:         $defaultrole = $env{'form.defaultrole'};
                   3798:         $defaultsec = $env{'form.sections'};
1.1       raeburn  3799:     }
1.27      raeburn  3800:     # Check to see if user information can be changed
                   3801:     my @userinfo = ('firstname','middlename','lastname','generation',
                   3802:                     'permanentemail','id');
                   3803:     my %canmodify;
                   3804:     if (&Apache::lonnet::allowed('mau',$domain)) {
1.84      raeburn  3805:         push(@userinfo,'inststatus');
1.27      raeburn  3806:         foreach my $field (@userinfo) {
                   3807:             $canmodify{$field} = 1;
                   3808:         }
                   3809:     }
                   3810:     my (%userlist,%modifiable_fields,@poss_roles);
                   3811:     my $secidx = &Apache::loncoursedata::CL_SECTION();
1.97.2.5! raeburn  3812:     my @courseroles = &roles_by_context('course',1,$crstype);
1.27      raeburn  3813:     if (!&Apache::lonnet::allowed('mau',$domain)) {
                   3814:         if ($context eq 'course' || $context eq 'author') {
1.97.2.4  raeburn  3815:             @poss_roles =  &curr_role_permissions($context,'','',$crstype);
1.27      raeburn  3816:             my @statuses = ('active','future');
                   3817:             my ($indexhash,$keylist) = &make_keylist_array();
                   3818:             my %info;
                   3819:             foreach my $role (@poss_roles) {
                   3820:                 %{$modifiable_fields{$role}} = &can_modify_userinfo($context,$domain,
                   3821:                                                         \@userinfo,[$role]);
                   3822:             }
                   3823:             if ($context eq 'course') {
                   3824:                 my ($cnum,$cdom) = &get_course_identity();
                   3825:                 my $roster = &Apache::loncoursedata::get_classlist();
1.66      raeburn  3826:                 if (ref($roster) eq 'HASH') {
                   3827:                     %userlist = %{$roster};
                   3828:                 }
1.27      raeburn  3829:                 my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
                   3830:                                                          \@statuses,\@poss_roles);
                   3831:                 &gather_userinfo($context,'view',\%userlist,$indexhash,\%info,
                   3832:                                 \%advrolehash,$permission);
                   3833:             } elsif ($context eq 'author') {
                   3834:                 my %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
                   3835:                                                   \@statuses,\@poss_roles);
                   3836:                 &gather_userinfo($context,'view',\%userlist,$indexhash,\%info,
                   3837:                              \%cstr_roles,$permission);
                   3838: 
                   3839:             }
                   3840:         }
1.1       raeburn  3841:     }
                   3842:     if ( $domain eq &LONCAPA::clean_domain($domain)
                   3843:         && ($amode ne '')) {
                   3844:         #######################################
                   3845:         ##         Add/Modify Users          ##
                   3846:         #######################################
                   3847:         if ($context eq 'course') {
                   3848:             $r->print('<h3>'.&mt('Enrolling Users')."</h3>\n<p>\n");
1.13      raeburn  3849:         } elsif ($context eq 'author') {
1.1       raeburn  3850:             $r->print('<h3>'.&mt('Updating Co-authors')."</h3>\n<p>\n");
                   3851:         } else {
                   3852:             $r->print('<h3>'.&mt('Adding/Modifying Users')."</h3>\n<p>\n");
                   3853:         }
1.87      bisitz   3854:         $r->rflush;
                   3855: 
1.1       raeburn  3856:         my %counts = (
                   3857:                        user => 0,
                   3858:                        auth => 0,
                   3859:                        role => 0,
                   3860:                      );
                   3861:         my $flushc=0;
                   3862:         my %student=();
1.42      raeburn  3863:         my (%curr_groups,@sections,@cleansec,$defaultwarn,$groupwarn);
1.1       raeburn  3864:         my %userchg;
1.27      raeburn  3865:         if ($context eq 'course' || $setting eq 'course') {
                   3866:             if ($context eq 'course') {
                   3867:                 # Get information about course groups
                   3868:                 %curr_groups = &Apache::longroup::coursegroups();
                   3869:             } elsif ($setting eq 'course') {
                   3870:                 if ($cid) {
                   3871:                     %curr_groups =
                   3872:                         &Apache::longroup::coursegroups($env{'form.dcdomain'},
                   3873:                                                         $env{'form.dccourse'});
                   3874:                 }
                   3875:             }
                   3876:             # determine section number
                   3877:             if ($defaultsec =~ /,/) {
                   3878:                 push(@sections,split(/,/,$defaultsec));
                   3879:             } else {
                   3880:                 push(@sections,$defaultsec);
                   3881:             }
                   3882:             # remove non alphanumeric values from section
                   3883:             foreach my $item (@sections) {
                   3884:                 $item =~ s/\W//g;
                   3885:                 if ($item eq "none" || $item eq 'all') {
                   3886:                     $defaultwarn = &mt('Default section name [_1] could not be used as it is a reserved word.',$item);
                   3887:                 } elsif ($item ne ''  && exists($curr_groups{$item})) {
                   3888:                     $groupwarn = &mt('Default section name "[_1]" is the name of a course group. Section names and group names must be distinct.',$item);
                   3889:                 } elsif ($item ne '') {
                   3890:                     push(@cleansec,$item);
                   3891:                 }
                   3892:             }
                   3893:             if ($defaultwarn) {
                   3894:                 $r->print($defaultwarn.'<br />');
                   3895:             }
                   3896:             if ($groupwarn) {
                   3897:                 $r->print($groupwarn.'<br />');
                   3898:             }
1.1       raeburn  3899:         }
1.5       raeburn  3900:         my (%curr_rules,%got_rules,%alerts);
1.27      raeburn  3901:         my %customroles = &my_custom_roles();
1.97.2.4  raeburn  3902:         my @permitted_roles = 
                   3903:             &roles_on_upload($context,$setting,$crstype,%customroles); 
1.1       raeburn  3904:         # Get new users list
1.27      raeburn  3905:         foreach my $line (@userdata) {
1.42      raeburn  3906:             my @secs;
1.27      raeburn  3907:             my %entries=&Apache::loncommon::record_sep($line);
1.1       raeburn  3908:             # Determine user name
                   3909:             unless (($entries{$fields{'username'}} eq '') ||
                   3910:                     (!defined($entries{$fields{'username'}}))) {
                   3911:                 my ($fname, $mname, $lname,$gen) = ('','','','');
                   3912:                 if (defined($fields{'names'})) {
                   3913:                     ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
                   3914:                                             /([^\,]+)\,\s*(\w+)\s*(.*)$/);
                   3915:                 } else {
                   3916:                     if (defined($fields{'fname'})) {
                   3917:                         $fname=$entries{$fields{'fname'}};
                   3918:                     }
                   3919:                     if (defined($fields{'mname'})) {
                   3920:                         $mname=$entries{$fields{'mname'}};
                   3921:                     }
                   3922:                     if (defined($fields{'lname'})) {
                   3923:                         $lname=$entries{$fields{'lname'}};
                   3924:                     }
                   3925:                     if (defined($fields{'gen'})) {
                   3926:                         $gen=$entries{$fields{'gen'}};
                   3927:                     }
                   3928:                 }
                   3929:                 if ($entries{$fields{'username'}}
                   3930:                     ne &LONCAPA::clean_username($entries{$fields{'username'}})) {
                   3931:                     $r->print('<br />'.
1.74      bisitz   3932:       &mt('[_1]: Unacceptable username for user [_2] [_3] [_4] [_5]',
1.77      raeburn  3933:           '<b>'.$entries{$fields{'username'}}.'</b>',$fname,$mname,$lname,$gen));
1.27      raeburn  3934:                     next;
1.1       raeburn  3935:                 } else {
1.71      droeschl 3936:                     if ($entries{$fields{'domain'}} 
1.57      raeburn  3937:                         ne &LONCAPA::clean_domain($entries{$fields{'domain'}})) {
                   3938:                         $r->print('<br />'. '<b>'.$entries{$fields{'domain'}}.
1.77      raeburn  3939:                                   '</b>: '.&mt('Unacceptable domain for user [_2] [_3] [_4] [_5]',$fname,$mname,$lname,$gen));
1.57      raeburn  3940:                         next;
                   3941:                     }
1.5       raeburn  3942:                     my $username = $entries{$fields{'username'}};
1.57      raeburn  3943:                     my $userdomain = $entries{$fields{'domain'}};
                   3944:                     if ($userdomain eq '') {
                   3945:                         $userdomain = $domain;
                   3946:                     }
1.27      raeburn  3947:                     if (defined($fields{'sec'})) {
                   3948:                         if (defined($entries{$fields{'sec'}})) {
1.42      raeburn  3949:                             $entries{$fields{'sec'}} =~ s/\W//g;
1.27      raeburn  3950:                             my $item = $entries{$fields{'sec'}};
                   3951:                             if ($item eq "none" || $item eq 'all') {
1.74      bisitz   3952:                                 $r->print('<br />'.&mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]" - this is a reserved word.','<b>'.$username.'</b>',$fname,$mname,$lname,$gen,$item));
1.27      raeburn  3953:                                 next;
                   3954:                             } elsif (exists($curr_groups{$item})) {
1.74      bisitz   3955:                                 $r->print('<br />'.&mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]" - this is a course group.','<b>'.$username.'</b>',$fname,$mname,$lname,$gen,$item).' '.&mt('Section names and group names must be distinct.'));
1.27      raeburn  3956:                                 next;
                   3957:                             } else {
                   3958:                                 push(@secs,$item);
                   3959:                             }
                   3960:                         }
                   3961:                     }
                   3962:                     if ($env{'request.course.sec'} ne '') {
                   3963:                         @secs = ($env{'request.course.sec'});
1.57      raeburn  3964:                         if (ref($userlist{$username.':'.$userdomain}) eq 'ARRAY') {
                   3965:                             my $currsec = $userlist{$username.':'.$userdomain}[$secidx];
1.27      raeburn  3966:                             if ($currsec ne $env{'request.course.sec'}) {
1.74      bisitz   3967:                                 $r->print('<br />'.&mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]".','<b>'.$username.'</b>',$fname,$mname,$lname,$gen,$secs[0]).'<br />');
1.27      raeburn  3968:                                 if ($currsec eq '') {
                   3969:                                     $r->print(&mt('This user already has an active/future student role in the course, unaffiliated to any section.'));
                   3970: 
                   3971:                                 } else {
                   3972:                                     $r->print(&mt('This user already has an active/future role in section "[_1]" of the course.',$currsec));
                   3973:                                 }
                   3974:                                 $r->print('<br />'.&mt('Although your current role has privileges to add students to section "[_1]", you do not have privileges to modify existing enrollments in other sections.',$secs[0]).'<br />');
                   3975:                                 next;
1.1       raeburn  3976:                             }
                   3977:                         }
1.27      raeburn  3978:                     } elsif ($context eq 'course' || $setting eq 'course') {
                   3979:                         if (@secs == 0) {
                   3980:                             @secs = @cleansec;
1.1       raeburn  3981:                         }
                   3982:                     }
                   3983:                     # determine id number
                   3984:                     my $id='';
                   3985:                     if (defined($fields{'id'})) {
                   3986:                         if (defined($entries{$fields{'id'}})) {
                   3987:                             $id=$entries{$fields{'id'}};
                   3988:                         }
                   3989:                         $id=~tr/A-Z/a-z/;
                   3990:                     }
                   3991:                     # determine email address
                   3992:                     my $email='';
                   3993:                     if (defined($fields{'email'})) {
                   3994:                         if (defined($entries{$fields{'email'}})) {
                   3995:                             $email=$entries{$fields{'email'}};
1.84      raeburn  3996:                             unless ($email=~/^[^\@]+\@[^\@]+$/) { $email=''; }
                   3997:                         }
                   3998:                     }
                   3999:                     # determine affiliation
                   4000:                     my $inststatus='';
                   4001:                     if (defined($fields{'inststatus'})) {
                   4002:                         if (defined($entries{$fields{'inststatus'}})) {
                   4003:                             $inststatus=$entries{$fields{'inststatus'}};
                   4004:                         }
1.1       raeburn  4005:                     }
                   4006:                     # determine user password
                   4007:                     my $password = $genpwd;
                   4008:                     if (defined($fields{'ipwd'})) {
                   4009:                         if ($entries{$fields{'ipwd'}}) {
                   4010:                             $password=$entries{$fields{'ipwd'}};
                   4011:                         }
                   4012:                     }
                   4013:                     # determine user role
                   4014:                     my $role = '';
                   4015:                     if (defined($fields{'role'})) {
                   4016:                         if ($entries{$fields{'role'}}) {
1.42      raeburn  4017:                             $entries{$fields{'role'}}  =~ s/(\s+$|^\s+)//g;
                   4018:                             if ($entries{$fields{'role'}} ne '') {
                   4019:                                 if (grep(/^\Q$entries{$fields{'role'}}\E$/,@permitted_roles)) {
                   4020:                                     $role = $entries{$fields{'role'}};
1.27      raeburn  4021:                                 }
                   4022:                             }
                   4023:                             if ($role eq '') {
                   4024:                                 my $rolestr = join(', ',@permitted_roles);
1.74      bisitz   4025:                                 $r->print('<br />'
                   4026:                                          .&mt('[_1]: You do not have permission to add the requested role [_2] for the user.'
                   4027:                                              ,'<b>'.$entries{$fields{'username'}}.'</b>'
                   4028:                                              ,$entries{$fields{'role'}})
                   4029:                                          .'<br />'
                   4030:                                          .&mt('Allowable role(s) is/are: [_1].',$rolestr)."\n"
                   4031:                                 );
1.1       raeburn  4032:                                 next;
                   4033:                             }
                   4034:                         }
                   4035:                     }
                   4036:                     if ($role eq '') {
                   4037:                         $role = $defaultrole;
                   4038:                     }
                   4039:                     # Clean up whitespace
1.57      raeburn  4040:                     foreach (\$id,\$fname,\$mname,\$lname,\$gen) {
1.1       raeburn  4041:                         $$_ =~ s/(\s+$|^\s+)//g;
                   4042:                     }
1.5       raeburn  4043:                     # check against rules
                   4044:                     my $checkid = 0;
                   4045:                     my $newuser = 0;
                   4046:                     my (%rulematch,%inst_results,%idinst_results);
1.57      raeburn  4047:                     my $uhome=&Apache::lonnet::homeserver($username,$userdomain);
1.5       raeburn  4048:                     if ($uhome eq 'no_host') {
1.57      raeburn  4049:                         next if ($userdomain ne $domain);
1.5       raeburn  4050:                         $checkid = 1;
                   4051:                         $newuser = 1;
                   4052:                         my $checkhash;
                   4053:                         my $checks = { 'username' => 1 };
                   4054:                         $checkhash->{$username.':'.$domain} = { 'newuser' => 1, };
                   4055:                         &Apache::loncommon::user_rule_check($checkhash,$checks,
                   4056:                             \%alerts,\%rulematch,\%inst_results,\%curr_rules,
                   4057:                             \%got_rules);
                   4058:                         if (ref($alerts{'username'}) eq 'HASH') {
                   4059:                             if (ref($alerts{'username'}{$domain}) eq 'HASH') {
                   4060:                                 next if ($alerts{'username'}{$domain}{$username});
                   4061:                             }
                   4062:                         }
1.13      raeburn  4063:                     } else {
1.27      raeburn  4064:                         if ($context eq 'course' || $context eq 'author') {
1.57      raeburn  4065:                             if ($userdomain eq $domain ) {
                   4066:                                 if ($role eq '') {
                   4067:                                     my @checkroles;
                   4068:                                     foreach my $role (@poss_roles) {
                   4069:                                         my $endkey;
                   4070:                                         if ($role ne 'st') {
                   4071:                                             $endkey = ':'.$role;
                   4072:                                         }
                   4073:                                         if (exists($userlist{$username.':'.$userdomain.$endkey})) {
                   4074:                                             if (!grep(/^\Q$role\E$/,@checkroles)) {
                   4075:                                                 push(@checkroles,$role);
                   4076:                                             }
                   4077:                                         }
1.27      raeburn  4078:                                     }
1.57      raeburn  4079:                                     if (@checkroles > 0) {
                   4080:                                         %canmodify = &can_modify_userinfo($context,$domain,\@userinfo,\@checkroles);
1.27      raeburn  4081:                                     }
1.57      raeburn  4082:                                 } elsif (ref($modifiable_fields{$role}) eq 'HASH') {
                   4083:                                     %canmodify = %{$modifiable_fields{$role}};
1.27      raeburn  4084:                                 }
                   4085:                             }
1.57      raeburn  4086:                             my @newinfo = (\$fname,\$mname,\$lname,\$gen,\$email,\$id);
1.84      raeburn  4087:                             for (my $i=0; $i<@newinfo; $i++) {
1.57      raeburn  4088:                                 if (${$newinfo[$i]} ne '') {
                   4089:                                     if (!$canmodify{$userinfo[$i]}) {
                   4090:                                         ${$newinfo[$i]} = '';
                   4091:                                     }
1.27      raeburn  4092:                                 }
                   4093:                             }
                   4094:                         }
1.5       raeburn  4095:                     }
                   4096:                     if ($id ne '') {
                   4097:                         if (!$newuser) {
1.57      raeburn  4098:                             my %idhash = &Apache::lonnet::idrget($userdomain,($username));
1.5       raeburn  4099:                             if ($idhash{$username} ne $id) {
                   4100:                                 $checkid = 1;
                   4101:                             }
                   4102:                         }
                   4103:                         if ($checkid) {
                   4104:                             my $checkhash;
                   4105:                             my $checks = { 'id' => 1 };
1.57      raeburn  4106:                             $checkhash->{$username.':'.$userdomain} = { 'newuser' => $newuser,
1.5       raeburn  4107:                                                                     'id'  => $id };
                   4108:                             &Apache::loncommon::user_rule_check($checkhash,$checks,
                   4109:                                 \%alerts,\%rulematch,\%idinst_results,\%curr_rules,
                   4110:                                 \%got_rules);
                   4111:                             if (ref($alerts{'id'}) eq 'HASH') {
1.57      raeburn  4112:                                 if (ref($alerts{'id'}{$userdomain}) eq 'HASH') {
                   4113:                                     next if ($alerts{'id'}{$userdomain}{$id});
1.5       raeburn  4114:                                 }
                   4115:                             }
                   4116:                         }
                   4117:                     }
1.1       raeburn  4118:                     if ($password || $env{'form.login'} eq 'loc') {
1.27      raeburn  4119:                         my $multiple = 0;
                   4120:                         my ($userresult,$authresult,$roleresult,$idresult);
                   4121:                         my (%userres,%authres,%roleres,%idres);
1.42      raeburn  4122:                         my $singlesec = '';
1.1       raeburn  4123:                         if ($role eq 'st') {
1.27      raeburn  4124:                             my $sec;
1.42      raeburn  4125:                             if (@secs > 0) {
                   4126:                                 $sec = $secs[0];
1.27      raeburn  4127:                             }
1.57      raeburn  4128:                             &modifystudent($userdomain,$username,$cid,$sec,
1.52      raeburn  4129:                                            $desiredhost,$context);
1.42      raeburn  4130:                             $roleresult =
                   4131:                                 &Apache::lonnet::modifystudent
1.57      raeburn  4132:                                     ($userdomain,$username,$id,$amode,$password,
1.42      raeburn  4133:                                      $fname,$mname,$lname,$gen,$sec,$enddate,
                   4134:                                      $startdate,$env{'form.forceid'},
1.52      raeburn  4135:                                      $desiredhost,$email,'manual','',$cid,
1.84      raeburn  4136:                                      '',$context,$inststatus);
1.42      raeburn  4137:                             $userresult = $roleresult;
1.1       raeburn  4138:                         } else {
1.42      raeburn  4139:                             if ($role ne '') { 
                   4140:                                 if ($context eq 'course' || $setting eq 'course') {
                   4141:                                     if ($customroles{$role}) {
                   4142:                                         $role = 'cr_'.$env{'user.domain'}.'_'.
                   4143:                                                 $env{'user.name'}.'_'.$role;
                   4144:                                     }
                   4145:                                     if ($role ne 'cc') { 
                   4146:                                         if (@secs > 1) {
                   4147:                                             $multiple = 1;
                   4148:                                             foreach my $sec (@secs) {
                   4149:                                                 ($userres{$sec},$authres{$sec},$roleres{$sec},$idres{$sec}) =
                   4150:                                                 &modifyuserrole($context,$setting,
1.57      raeburn  4151:                                                     $changeauth,$cid,$userdomain,$username,
1.42      raeburn  4152:                                                     $id,$amode,$password,$fname,
                   4153:                                                     $mname,$lname,$gen,$sec,
                   4154:                                                     $env{'form.forceid'},$desiredhost,
                   4155:                                                     $email,$role,$enddate,
1.84      raeburn  4156:                                                     $startdate,$checkid,$inststatus);
1.42      raeburn  4157:                                             }
                   4158:                                         } elsif (@secs > 0) {
                   4159:                                             $singlesec = $secs[0];
                   4160:                                         }
1.27      raeburn  4161:                                     }
                   4162:                                 }
                   4163:                             }
                   4164:                             if (!$multiple) {
1.28      raeburn  4165:                                 ($userresult,$authresult,$roleresult,$idresult) = 
1.27      raeburn  4166:                                     &modifyuserrole($context,$setting,
1.57      raeburn  4167:                                                     $changeauth,$cid,$userdomain,$username, 
1.42      raeburn  4168:                                                     $id,$amode,$password,$fname,
                   4169:                                                     $mname,$lname,$gen,$singlesec,
                   4170:                                                     $env{'form.forceid'},$desiredhost,
1.84      raeburn  4171:                                                     $email,$role,$enddate,$startdate,
                   4172:                                                     $checkid,$inststatus);
1.27      raeburn  4173:                             }
                   4174:                         }
                   4175:                         if ($multiple) {
                   4176:                             foreach my $sec (sort(keys(%userres))) {
1.42      raeburn  4177:                                 $flushc =
1.27      raeburn  4178:                                 &user_change_result($r,$userres{$sec},$authres{$sec},
                   4179:                                                     $roleres{$sec},$idres{$sec},\%counts,$flushc,
1.57      raeburn  4180:                                                     $username,$userdomain,\%userchg);
1.27      raeburn  4181: 
                   4182:                             }
                   4183:                         } else {
                   4184:                             $flushc = 
                   4185:                                 &user_change_result($r,$userresult,$authresult,
1.28      raeburn  4186:                                                     $roleresult,$idresult,\%counts,$flushc,
1.57      raeburn  4187:                                                     $username,$userdomain,\%userchg);
1.1       raeburn  4188:                         }
                   4189:                     } else {
                   4190:                         if ($context eq 'course') {
                   4191:                             $r->print('<br />'. 
1.74      bisitz   4192:       &mt('[_1]: Unable to enroll. No password specified.','<b>'.$username.'</b>')
1.1       raeburn  4193:                                      );
1.13      raeburn  4194:                         } elsif ($context eq 'author') {
1.1       raeburn  4195:                             $r->print('<br />'.
1.74      bisitz   4196:       &mt('[_1]: Unable to add co-author. No password specified.','<b>'.$username.'</b>')
1.1       raeburn  4197:                                      );
                   4198:                         } else {
                   4199:                             $r->print('<br />'.
1.74      bisitz   4200:       &mt('[_1]: Unable to add user. No password specified.','<b>'.$username.'</b>')
1.1       raeburn  4201:                                      );
                   4202:                         }
                   4203:                     }
                   4204:                 }
                   4205:             }
                   4206:         } # end of foreach (@userdata)
                   4207:         # Flush the course logs so reverse user roles immediately updated
1.5       raeburn  4208:         &Apache::lonnet::flushcourselogs();
1.29      raeburn  4209:         $r->print("</p>\n<p>\n".&mt('Processed [quant,_1,user].',$counts{'user'}).
1.1       raeburn  4210:                   "</p>\n");
                   4211:         if ($counts{'role'} > 0) {
                   4212:             $r->print("<p>\n".
1.29      raeburn  4213:                       &mt('Roles added for [quant,_1,user].',$counts{'role'}).' '.&mt('If a user is currently logged-in to LON-CAPA, any new roles which are active will be available when the user next logs in.')."</p>\n");
                   4214:         } else {
                   4215:             $r->print('<p>'.&mt('No roles added').'</p>');
1.1       raeburn  4216:         }
                   4217:         if ($counts{'auth'} > 0) {
                   4218:             $r->print("<p>\n".
                   4219:                       &mt('Authentication changed for [_1] existing users.',
                   4220:                           $counts{'auth'})."</p>\n");
                   4221:         }
1.13      raeburn  4222:         $r->print(&print_namespacing_alerts($domain,\%alerts,\%curr_rules));
1.1       raeburn  4223:         #####################################
1.29      raeburn  4224:         # Display list of students to drop  #
1.1       raeburn  4225:         #####################################
                   4226:         if ($env{'form.fullup'} eq 'yes') {
1.29      raeburn  4227:             $r->print('<h3>'.&mt('Students to Drop')."</h3>\n");
1.1       raeburn  4228:             #  Get current classlist
1.30      raeburn  4229:             my $classlist = &Apache::loncoursedata::get_classlist();
1.1       raeburn  4230:             if (! defined($classlist)) {
1.29      raeburn  4231:                 $r->print('<form name="studentform" method="post" action="/adm/createuser" />'.
                   4232:                           '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
                   4233:                           &mt('There are no students with current/future access to the course.').
                   4234:                           '</form>'."\n");
1.66      raeburn  4235:             } elsif (ref($classlist) eq 'HASH') {
1.1       raeburn  4236:                 # Remove the students we just added from the list of students.
1.30      raeburn  4237:                 foreach my $line (@userdata) {
                   4238:                     my %entries=&Apache::loncommon::record_sep($line);
1.1       raeburn  4239:                     unless (($entries{$fields{'username'}} eq '') ||
                   4240:                             (!defined($entries{$fields{'username'}}))) {
                   4241:                         delete($classlist->{$entries{$fields{'username'}}.
                   4242:                                                 ':'.$domain});
                   4243:                     }
                   4244:                 }
                   4245:                 # Print out list of dropped students.
1.30      raeburn  4246:                 &show_drop_list($r,$classlist,'nosort',$permission);
1.1       raeburn  4247:             }
                   4248:         }
                   4249:     } # end of unless
1.29      raeburn  4250:     if ($env{'form.fullup'} ne 'yes') {
                   4251:         $r->print('</form>');
                   4252:     }
1.1       raeburn  4253: }
                   4254: 
1.13      raeburn  4255: sub print_namespacing_alerts {
                   4256:     my ($domain,$alerts,$curr_rules) = @_;
                   4257:     my $output;
                   4258:     if (ref($alerts) eq 'HASH') {
                   4259:         if (keys(%{$alerts}) > 0) {
                   4260:             if (ref($alerts->{'username'}) eq 'HASH') {
                   4261:                 foreach my $dom (sort(keys(%{$alerts->{'username'}}))) {
                   4262:                     my $count;
                   4263:                     if (ref($alerts->{'username'}{$dom}) eq 'HASH') {
                   4264:                         $count = keys(%{$alerts->{'username'}{$dom}});
                   4265:                     }
                   4266:                     my $domdesc = &Apache::lonnet::domain($domain,'description');
                   4267:                     if (ref($curr_rules->{$dom}) eq 'HASH') {
                   4268:                         $output .= &Apache::loncommon::instrule_disallow_msg(
                   4269:                                         'username',$domdesc,$count,'upload');
                   4270:                     }
                   4271:                     $output .= &Apache::loncommon::user_rule_formats($dom,
                   4272:                                    $domdesc,$curr_rules->{$dom}{'username'},
                   4273:                                    'username');
                   4274:                 }
                   4275:             }
                   4276:             if (ref($alerts->{'id'}) eq 'HASH') {
                   4277:                 foreach my $dom (sort(keys(%{$alerts->{'id'}}))) {
                   4278:                     my $count;
                   4279:                     if (ref($alerts->{'id'}{$dom}) eq 'HASH') {
                   4280:                         $count = keys(%{$alerts->{'id'}{$dom}});
                   4281:                     }
                   4282:                     my $domdesc = &Apache::lonnet::domain($domain,'description');
                   4283:                     if (ref($curr_rules->{$dom}) eq 'HASH') {
                   4284:                         $output .= &Apache::loncommon::instrule_disallow_msg(
                   4285:                                               'id',$domdesc,$count,'upload');
                   4286:                     }
                   4287:                     $output .= &Apache::loncommon::user_rule_formats($dom,
                   4288:                                     $domdesc,$curr_rules->{$dom}{'id'},'id');
                   4289:                 }
                   4290:             }
                   4291:         }
                   4292:     }
                   4293: }
                   4294: 
1.1       raeburn  4295: sub user_change_result {
1.29      raeburn  4296:     my ($r,$userresult,$authresult,$roleresult,$idresult,$counts,$flushc,
1.57      raeburn  4297:         $username,$userdomain,$userchg) = @_;
1.1       raeburn  4298:     my $okresult = 0;
                   4299:     if ($userresult ne 'ok') {
                   4300:         if ($userresult =~ /^error:(.+)$/) {
                   4301:             my $error = $1;
                   4302:             $r->print('<br />'.
1.74      bisitz   4303:                   &mt('[_1]: Unable to add/modify: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
1.1       raeburn  4304:         }
                   4305:     } else {
                   4306:         $counts->{'user'} ++;
                   4307:         $okresult = 1;
                   4308:     }
                   4309:     if ($authresult ne 'ok') {
                   4310:         if ($authresult =~ /^error:(.+)$/) {
                   4311:             my $error = $1;
                   4312:             $r->print('<br />'.
1.74      bisitz   4313:                   &mt('[_1]: Unable to modify authentication: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
1.1       raeburn  4314:         } 
                   4315:     } else {
                   4316:         $counts->{'auth'} ++;
                   4317:         $okresult = 1;
                   4318:     }
                   4319:     if ($roleresult ne 'ok') {
                   4320:         if ($roleresult =~ /^error:(.+)$/) {
                   4321:             my $error = $1;
                   4322:             $r->print('<br />'.
1.74      bisitz   4323:                   &mt('[_1]: Unable to add role: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
1.1       raeburn  4324:         }
                   4325:     } else {
                   4326:         $counts->{'role'} ++;
                   4327:         $okresult = 1;
                   4328:     }
                   4329:     if ($okresult) {
                   4330:         $flushc++;
1.57      raeburn  4331:         $userchg->{$username.':'.$userdomain}=1;
1.1       raeburn  4332:         $r->print('. ');
                   4333:         if ($flushc>15) {
                   4334:             $r->rflush;
                   4335:             $flushc=0;
                   4336:         }
                   4337:     }
1.29      raeburn  4338:     if ($idresult) {
                   4339:         $r->print($idresult);
                   4340:     }
1.1       raeburn  4341:     return $flushc;
                   4342: }
                   4343: 
                   4344: # ========================================================= Menu Phase Two Drop
1.17      raeburn  4345: sub print_drop_menu {
1.97.2.4  raeburn  4346:     my ($r,$context,$permission,$crstype) = @_;
                   4347:     my $heading;
                   4348:     if ($crstype eq 'Community') {
                   4349:         $heading = &mt("Drop Members");
                   4350:     } else {
                   4351:         $heading = &mt("Drop Students");
                   4352:     }
                   4353:     $r->print('<h3>'.$heading.'</h3>'."\n".
1.17      raeburn  4354:               '<form name="studentform" method="post">'."\n");
1.30      raeburn  4355:     my $classlist = &Apache::loncoursedata::get_classlist();
1.1       raeburn  4356:     if (! defined($classlist)) {
1.97.2.4  raeburn  4357:         if ($crstype eq 'Community') {
                   4358:             $r->print(&mt('There are no members currently enrolled.')."\n");
                   4359:         } else {
                   4360:             $r->print(&mt('There are no students currently enrolled.')."\n");
                   4361:         }
1.30      raeburn  4362:     } else {
1.97.2.4  raeburn  4363:         &show_drop_list($r,$classlist,'nosort',$permission,$crstype);
1.1       raeburn  4364:     }
1.17      raeburn  4365:     $r->print('</form>'. &Apache::loncommon::end_page());
1.1       raeburn  4366:     return;
                   4367: }
                   4368: 
                   4369: # ================================================================== Phase four
                   4370: 
1.11      raeburn  4371: sub update_user_list {
                   4372:     my ($r,$context,$setting,$choice) = @_;
                   4373:     my $now = time;
1.1       raeburn  4374:     my $count=0;
1.97.2.4  raeburn  4375:     my $crstype;
                   4376:     if ($context eq 'course') {
                   4377:         $crstype = &Apache::loncommon::course_type();
                   4378:     }
1.11      raeburn  4379:     my @changelist;
1.29      raeburn  4380:     if ($choice eq 'drop') {
                   4381:         @changelist = &Apache::loncommon::get_env_multiple('form.droplist');
                   4382:     } else {
1.11      raeburn  4383:         @changelist = &Apache::loncommon::get_env_multiple('form.actionlist');
                   4384:     }
                   4385:     my %result_text = ( ok    => { 'revoke'   => 'Revoked',
                   4386:                                    'delete'   => 'Deleted',
                   4387:                                    'reenable' => 'Re-enabled',
1.17      raeburn  4388:                                    'activate' => 'Activated',
                   4389:                                    'chgdates' => 'Changed Access Dates for',
                   4390:                                    'chgsec'   => 'Changed section for',
                   4391:                                    'drop'     => 'Dropped',
1.11      raeburn  4392:                                  },
                   4393:                         error => {'revoke'    => 'revoking',
                   4394:                                   'delete'    => 'deleting',
                   4395:                                   'reenable'  => 're-enabling',
                   4396:                                   'activate'  => 'activating',
1.17      raeburn  4397:                                   'chgdates'  => 'changing access dates for',
                   4398:                                   'chgsec'    => 'changing section for',
                   4399:                                   'drop'      => 'dropping',
1.11      raeburn  4400:                                  },
                   4401:                       );
                   4402:     my ($startdate,$enddate);
                   4403:     if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
                   4404:         ($startdate,$enddate) = &get_dates_from_form();
                   4405:     }
                   4406:     foreach my $item (@changelist) {
                   4407:         my ($role,$uname,$udom,$cid,$sec,$scope,$result,$type,$locktype,@sections,
                   4408:             $scopestem);
1.17      raeburn  4409:         if ($choice eq 'drop') {
                   4410:             ($uname,$udom,$sec) = split(/:/,$item,-1);
                   4411:             $role = 'st';
                   4412:             $cid = $env{'request.course.id'};
                   4413:             $scopestem = '/'.$cid;
                   4414:             $scopestem =~s/\_/\//g;
                   4415:             if ($sec eq '') {
                   4416:                 $scope = $scopestem;
                   4417:             } else {
                   4418:                 $scope = $scopestem.'/'.$sec;
                   4419:             }
                   4420:         } elsif ($context eq 'course') {
1.11      raeburn  4421:             ($uname,$udom,$role,$sec,$type,$locktype) = split(/\:/,$item,-1);
                   4422:             $cid = $env{'request.course.id'};
                   4423:             $scopestem = '/'.$cid;
                   4424:             $scopestem =~s/\_/\//g;
                   4425:             if ($sec eq '') {
                   4426:                 $scope = $scopestem;
                   4427:             } else {
                   4428:                 $scope = $scopestem.'/'.$sec;
                   4429:             }
1.13      raeburn  4430:         } elsif ($context eq 'author') {
1.11      raeburn  4431:             ($uname,$udom,$role) = split(/\:/,$item,-1);
                   4432:             $scope = '/'.$env{'user.domain'}.'/'.$env{'user.name'};
                   4433:         } elsif ($context eq 'domain') {
                   4434:             if ($setting eq 'domain') {
                   4435:                 ($role,$uname,$udom) = split(/\:/,$item,-1);
                   4436:                 $scope = '/'.$env{'request.role.domain'}.'/';
1.13      raeburn  4437:             } elsif ($setting eq 'author') { 
1.11      raeburn  4438:                 ($uname,$udom,$role,$scope) = split(/\:/,$item);
                   4439:             } elsif ($setting eq 'course') {
                   4440:                 ($uname,$udom,$role,$cid,$sec,$type,$locktype) = 
                   4441:                     split(/\:/,$item);
                   4442:                 $scope = '/'.$cid;
                   4443:                 $scope =~s/\_/\//g;
                   4444:                 if ($sec ne '') {
                   4445:                     $scope .= '/'.$sec;
                   4446:                 }
                   4447:             }
                   4448:         }
1.97.2.4  raeburn  4449:         my $plrole = &Apache::lonnet::plaintext($role,$crstype);
1.11      raeburn  4450:         my $start = $env{'form.'.$item.'_start'};
                   4451:         my $end = $env{'form.'.$item.'_end'};
1.17      raeburn  4452:         if ($choice eq 'drop') {
                   4453:             # drop students
                   4454:             $end = $now;
                   4455:             $type = 'manual';
                   4456:             $result =
1.52      raeburn  4457:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.17      raeburn  4458:         } elsif ($choice eq 'revoke') {
                   4459:             # revoke or delete user role
1.11      raeburn  4460:             $end = $now; 
                   4461:             if ($role eq 'st') {
                   4462:                 $result = 
1.52      raeburn  4463:                     &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.11      raeburn  4464:             } else {
                   4465:                 $result = 
1.52      raeburn  4466:                     &Apache::lonnet::revokerole($udom,$uname,$scope,$role,
                   4467:                                                 '','',$context);
1.11      raeburn  4468:             }
                   4469:         } elsif ($choice eq 'delete') {
                   4470:             if ($role eq 'st') {
1.52      raeburn  4471:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$now,$start,$type,$locktype,$cid,'',$context);
1.29      raeburn  4472:             }
                   4473:             $result =
                   4474:                 &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$now,
1.52      raeburn  4475:                                             $start,1,'',$context);
1.11      raeburn  4476:         } else {
                   4477:             #reenable, activate, change access dates or change section
                   4478:             if ($choice ne 'chgsec') {
                   4479:                 $start = $startdate; 
                   4480:                 $end = $enddate;
                   4481:             }
                   4482:             if ($choice eq 'reenable') {
                   4483:                 if ($role eq 'st') {
1.52      raeburn  4484:                     $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.11      raeburn  4485:                 } else {
                   4486:                     $result = 
                   4487:                         &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
1.52      raeburn  4488:                                                     $now,'','',$context);
1.11      raeburn  4489:                 }
                   4490:             } elsif ($choice eq 'activate') {
                   4491:                 if ($role eq 'st') {
1.52      raeburn  4492:                     $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.11      raeburn  4493:                 } else {
                   4494:                     $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
1.52      raeburn  4495:                                             $now,'','',$context);
1.11      raeburn  4496:                 }
                   4497:             } elsif ($choice eq 'chgdates') {
                   4498:                 if ($role eq 'st') {
1.52      raeburn  4499:                     $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.11      raeburn  4500:                 } else {
                   4501:                     $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
1.52      raeburn  4502:                                                 $start,'','',$context);
1.11      raeburn  4503:                 }
                   4504:             } elsif ($choice eq 'chgsec') {
                   4505:                 my (@newsecs,$revresult,$nochg,@retained);
                   4506:                 if ($role ne 'cc') {
                   4507:                     @newsecs = split(/,/,$env{'form.newsecs'});
                   4508:                 }
                   4509:                 # remove existing section if not to be retained.   
                   4510:                 if (!$env{'form.retainsec'}) {
                   4511:                     if ($sec eq '') {
                   4512:                         if (@newsecs == 0) {
                   4513:                             $result = &mt('No change in section assignment (none)');
                   4514:                             $nochg = 1;
1.40      raeburn  4515:                         } else {
                   4516:                             $revresult =
                   4517:                                 &Apache::lonnet::revokerole($udom,$uname,
1.52      raeburn  4518:                                                             $scope,$role,
                   4519:                                                             '','',$context);
1.40      raeburn  4520:                         } 
1.11      raeburn  4521:                     } else {
1.28      raeburn  4522:                         if (@newsecs > 0) {
                   4523:                             if (grep(/^\Q$sec\E$/,@newsecs)) {
                   4524:                                 push(@retained,$sec);
                   4525:                             } else {
                   4526:                                 $revresult =
                   4527:                                     &Apache::lonnet::revokerole($udom,$uname,
1.52      raeburn  4528:                                                                 $scope,$role,
                   4529:                                                                 '','',$context);
1.28      raeburn  4530:                             }
                   4531:                         } else {
1.11      raeburn  4532:                             $revresult =
1.28      raeburn  4533:                                 &Apache::lonnet::revokerole($udom,$uname,
1.52      raeburn  4534:                                                             $scope,$role,
                   4535:                                                             '','',$context);
1.11      raeburn  4536:                         }
                   4537:                     }
                   4538:                 } else {
1.28      raeburn  4539:                     if ($sec eq '') {
                   4540:                         $nochg = 1;
                   4541:                     } else { 
                   4542:                         push(@retained,$sec);
                   4543:                     }
1.11      raeburn  4544:                 }
                   4545:                 # add new sections
                   4546:                 if (@newsecs == 0) {
                   4547:                     if (!$nochg) {
1.28      raeburn  4548:                         if ($role eq 'st') {
                   4549:                             $result = 
1.52      raeburn  4550:                                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,undef,$end,$start,$type,$locktype,$cid,'',$context);
1.28      raeburn  4551:                         } else {
                   4552:                             my $newscope = $scopestem;
1.52      raeburn  4553:                             $result = &Apache::lonnet::assignrole($udom,$uname,$newscope,$role,$end,$start,'','',$context);
1.11      raeburn  4554:                         }
                   4555:                     }
                   4556:                 } else {
                   4557:                     foreach my $newsec (@newsecs) { 
                   4558:                         if (!grep(/^\Q$newsec\E$/,@retained)) {
                   4559:                             if ($role eq 'st') {
1.52      raeburn  4560:                                 $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$newsec,$end,$start,$type,$locktype,$cid,'',$context);
1.11      raeburn  4561:                             } else {
                   4562:                                 my $newscope = $scopestem;
                   4563:                                 if ($newsec ne '') {
                   4564:                                    $newscope .= '/'.$newsec;
                   4565:                                 }
                   4566:                                 $result = &Apache::lonnet::assignrole($udom,$uname,
                   4567:                                                         $newscope,$role,$end,$start);
                   4568:                             }
                   4569:                         }
                   4570:                     }
                   4571:                 }
                   4572:             }
                   4573:         }
1.17      raeburn  4574:         my $extent = $scope;
                   4575:         if ($choice eq 'drop' || $context eq 'course') {
                   4576:             my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
                   4577:             if ($cdesc) {
                   4578:                 $extent = $cdesc;
                   4579:             }
                   4580:         }
1.1       raeburn  4581:         if ($result eq 'ok' || $result eq 'ok:') {
1.11      raeburn  4582:             $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for [_3]",
1.17      raeburn  4583:                           $plrole,$extent,$uname.':'.$udom).'<br />');
1.1       raeburn  4584:             $count++;
                   4585:         } else {
                   4586:             $r->print(
1.29      raeburn  4587:                 &mt("Error $result_text{'error'}{$choice} [_1] in [_2] for [_3]: [_4].",
1.17      raeburn  4588:                     $plrole,$extent,$uname.':'.$udom,$result).'<br />');
1.11      raeburn  4589:         }
                   4590:     }
1.32      raeburn  4591:     $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n");
1.33      raeburn  4592:     if ($choice eq 'drop') {
                   4593:         $r->print('<input type="hidden" name="action" value="listusers" />'."\n".
                   4594:                   '<input type="hidden" name="Status" value="Active" />'."\n".
                   4595:                   '<input type="hidden" name="showrole" value="st" />'."\n");
                   4596:     } else {
                   4597:         foreach my $item ('action','sortby','roletype','showrole','Status','secfilter','grpfilter') {
                   4598:             if ($env{'form.'.$item} ne '') {
                   4599:                 $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.
                   4600:                           '" />'."\n");
                   4601:             }
1.32      raeburn  4602:         }
                   4603:     }
1.29      raeburn  4604:     $r->print('<p><b>'.&mt("$result_text{'ok'}{$choice} role(s) for [quant,_1,user,users,no users].",$count).'</b></p>');
1.11      raeburn  4605:     if ($count > 0) {
1.17      raeburn  4606:         if ($choice eq 'revoke' || $choice eq 'drop') {
1.74      bisitz   4607:             $r->print('<p>'.&mt('Re-enabling will re-activate data for the role.').'</p>');
1.11      raeburn  4608:         }
                   4609:         # Flush the course logs so reverse user roles immediately updated
                   4610:         &Apache::lonnet::flushcourselogs();
                   4611:     }
                   4612:     if ($env{'form.makedatesdefault'}) {
                   4613:         if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
1.97.2.4  raeburn  4614:             $r->print(&make_dates_default($startdate,$enddate,$context,$crstype));
1.1       raeburn  4615:         }
                   4616:     }
1.33      raeburn  4617:     my $linktext = &mt('Display User Lists');
                   4618:     if ($choice eq 'drop') {
                   4619:         $linktext = &mt('Display current class roster');
                   4620:     }
                   4621:     $r->print('<a href="javascript:document.studentform.submit()">'.$linktext.'</a></form>'."\n");
1.1       raeburn  4622: }
                   4623: 
1.8       raeburn  4624: sub classlist_drop {
1.29      raeburn  4625:     my ($scope,$uname,$udom,$now) = @_;
1.8       raeburn  4626:     my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)});
1.29      raeburn  4627:     if (&Apache::lonnet::is_course($cdom,$cnum)) {
1.8       raeburn  4628:         if (!&active_student_roles($cnum,$cdom,$uname,$udom)) {
1.63      raeburn  4629:             my %user;
                   4630:             my $result = &update_classlist($cdom,$cnum,$udom,$uname,\%user,$now);
1.8       raeburn  4631:             return &mt('Drop from classlist: [_1]',
                   4632:                        '<b>'.$result.'</b>').'<br />';
                   4633:         }
                   4634:     }
                   4635: }
                   4636: 
                   4637: sub active_student_roles {
                   4638:     my ($cnum,$cdom,$uname,$udom) = @_;
                   4639:     my %roles =
                   4640:         &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
                   4641:                                       ['future','active'],['st']);
                   4642:     return exists($roles{"$cnum:$cdom:st"});
                   4643: }
                   4644: 
1.1       raeburn  4645: sub section_check_js {
1.8       raeburn  4646:     my $groupslist= &get_groupslist();
1.1       raeburn  4647:     return <<"END";
                   4648: function validate(caller) {
1.9       raeburn  4649:     var groups = new Array($groupslist);
1.1       raeburn  4650:     var secname = caller.value;
                   4651:     if ((secname == 'all') || (secname == 'none')) {
                   4652:         alert("'"+secname+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name.");
                   4653:         return 'error';
                   4654:     }
                   4655:     if (secname != '') {
                   4656:         for (var k=0; k<groups.length; k++) {
                   4657:             if (secname == groups[k]) {
                   4658:                 alert("'"+secname+"' may not be used as the name for a section, as it is the name of a course group.\\nSection names and group names must be distinct. Please choose a different section name.");
                   4659:                 return 'error';
                   4660:             }
                   4661:         }
                   4662:     }
                   4663:     return 'ok';
                   4664: }
                   4665: END
                   4666: }
                   4667: 
                   4668: sub set_login {
                   4669:     my ($dom,$authformkrb,$authformint,$authformloc) = @_;
                   4670:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   4671:     my $response;
                   4672:     my ($authnum,%can_assign) =
                   4673:         &Apache::loncommon::get_assignable_auth($dom);
                   4674:     if ($authnum) {
                   4675:         $response = &Apache::loncommon::start_data_table();
                   4676:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   4677:             $response .= &Apache::loncommon::start_data_table_row().
                   4678:                          '<td>'.$authformkrb.'</td>'.
                   4679:                          &Apache::loncommon::end_data_table_row()."\n";
                   4680:         }
                   4681:         if ($can_assign{'int'}) {
                   4682:             $response .= &Apache::loncommon::start_data_table_row().
                   4683:                          '<td>'.$authformint.'</td>'.
                   4684:                          &Apache::loncommon::end_data_table_row()."\n"
                   4685:         }
                   4686:         if ($can_assign{'loc'}) {
                   4687:             $response .= &Apache::loncommon::start_data_table_row().
                   4688:                          '<td>'.$authformloc.'</td>'.
                   4689:                          &Apache::loncommon::end_data_table_row()."\n";
                   4690:         }
                   4691:         $response .= &Apache::loncommon::end_data_table();
                   4692:     }
                   4693:     return $response;
                   4694: }
                   4695: 
1.8       raeburn  4696: sub course_sections {
1.51      raeburn  4697:     my ($sections_count,$role,$current_sec) = @_;
1.8       raeburn  4698:     my $output = '';
                   4699:     my @sections = (sort {$a <=> $b} keys %{$sections_count});
1.29      raeburn  4700:     my $numsec = scalar(@sections);
1.92      bisitz   4701:     my $is_selected = ' selected="selected"';
1.29      raeburn  4702:     if ($numsec <= 1) {
1.8       raeburn  4703:         $output = '<select name="currsec_'.$role.'" >'."\n".
1.51      raeburn  4704:                   '  <option value="">'.&mt('Select').'</option>'."\n";
                   4705:         if ($current_sec eq 'none') {
                   4706:             $output .=       
                   4707:                   '  <option value=""'.$is_selected.'>'.&mt('No section').'</option>'."\n";
                   4708:         } else {
                   4709:             $output .=
1.29      raeburn  4710:                   '  <option value="">'.&mt('No section').'</option>'."\n";
1.51      raeburn  4711:         }
1.29      raeburn  4712:         if ($numsec == 1) {
1.51      raeburn  4713:             if ($current_sec eq $sections[0]) {
                   4714:                 $output .=
                   4715:                   '  <option value="'.$sections[0].'"'.$is_selected.'>'.$sections[0].'</option>'."\n";
                   4716:             } else {
                   4717:                 $output .=  
1.8       raeburn  4718:                   '  <option value="'.$sections[0].'" >'.$sections[0].'</option>'."\n";
1.51      raeburn  4719:             }
1.29      raeburn  4720:         }
1.8       raeburn  4721:     } else {
                   4722:         $output = '<select name="currsec_'.$role.'" ';
                   4723:         my $multiple = 4;
                   4724:         if (scalar(@sections) < 4) { $multiple = scalar(@sections); }
1.29      raeburn  4725:         if ($role eq 'st') {
                   4726:             $output .= '>'."\n".
1.51      raeburn  4727:                        '  <option value="">'.&mt('Select').'</option>'."\n";
                   4728:             if ($current_sec eq 'none') {
                   4729:                 $output .= 
                   4730:                        '  <option value=""'.$is_selected.'>'.&mt('No section')."</option>\n";
                   4731:             } else {
                   4732:                 $output .=
1.29      raeburn  4733:                        '  <option value="">'.&mt('No section')."</option>\n";
1.51      raeburn  4734:             }
1.29      raeburn  4735:         } else {
                   4736:             $output .= 'multiple="multiple" size="'.$multiple.'">'."\n";
                   4737:         }
1.8       raeburn  4738:         foreach my $sec (@sections) {
1.51      raeburn  4739:             if ($current_sec eq $sec) {
                   4740:                 $output .= '<option value="'.$sec.'"'.$is_selected.'>'.$sec."</option>\n";
                   4741:             } else {
                   4742:                 $output .= '<option value="'.$sec.'">'.$sec."</option>\n";
                   4743:             }
1.8       raeburn  4744:         }
                   4745:     }
                   4746:     $output .= '</select>';
                   4747:     return $output;
                   4748: }
                   4749: 
                   4750: sub get_groupslist {
                   4751:     my $groupslist;
                   4752:     my %curr_groups = &Apache::longroup::coursegroups();
                   4753:     if (%curr_groups) {
                   4754:         $groupslist = join('","',sort(keys(%curr_groups)));
                   4755:         $groupslist = '"'.$groupslist.'"';
                   4756:     }
1.11      raeburn  4757:     return $groupslist; 
1.8       raeburn  4758: }
                   4759: 
                   4760: sub setsections_javascript {
1.37      raeburn  4761:     my ($formname,$groupslist,$mode,$checkauth) = @_;
1.28      raeburn  4762:     my ($checkincluded,$finish,$rolecode,$setsection_js);
                   4763:     if ($mode eq 'upload') {
                   4764:         $checkincluded = 'formname.name == "'.$formname.'"';
                   4765:         $finish = "return 'ok';";
                   4766:         $rolecode = "var role = formname.defaultrole.options[formname.defaultrole.selectedIndex].value;\n";
                   4767:     } elsif ($formname eq 'cu') {
1.8       raeburn  4768:         $checkincluded = 'formname.elements[i-1].checked == true';
1.37      raeburn  4769:         if ($checkauth) {
                   4770:             $finish = "var authcheck = auth_check();\n".
                   4771:                       "   if (authcheck == 'ok') {\n".
                   4772:                       "       formname.submit();\n".
                   4773:                       "   }\n";
                   4774:         } else {
                   4775:             $finish = 'formname.submit()';
                   4776:         }
1.28      raeburn  4777:         $rolecode = "var match = str.split('_');
                   4778:                 var role = match[3];\n";
                   4779:     } elsif ($formname eq 'enrollstudent') {
                   4780:         $checkincluded = 'formname.name == "'.$formname.'"';
1.37      raeburn  4781:         if ($checkauth) {
                   4782:             $finish = "var authcheck = auth_check();\n".
                   4783:                       "   if (authcheck == 'ok') {\n".
                   4784:                       "       formname.submit();\n".
                   4785:                       "   }\n";
                   4786:         } else {
                   4787:             $finish = 'formname.submit()';
                   4788:         }
1.28      raeburn  4789:         $rolecode = "var match = str.split('_');
                   4790:                 var role = match[1];\n";
1.8       raeburn  4791:     } else {
1.28      raeburn  4792:         $checkincluded = 'formname.name == "'.$formname.'"'; 
1.8       raeburn  4793:         $finish = "seccheck = 'ok';";
1.28      raeburn  4794:         $rolecode = "var match = str.split('_');
                   4795:                 var role = match[1];\n";
1.11      raeburn  4796:         $setsection_js = "var seccheck = 'alert';"; 
1.8       raeburn  4797:     }
                   4798:     my %alerts = &Apache::lonlocal::texthash(
                   4799:                     secd => 'Section designations do not apply to Course Coordinator roles.',
                   4800:                     accr => 'A course coordinator role will be added with access to all sections.',
                   4801:                     inea => 'In each course, each user may only have one student role at a time.',
                   4802:                     youh => 'You had selected ',
                   4803:                     secs => 'sections.',
                   4804:                     plmo => 'Please modify your selections so they include no more than one section.',
                   4805:                     mayn => 'may not be used as the name for a section, as it is a reserved word.',
                   4806:                     plch => 'Please choose a different section name.',
                   4807:                     mnot => 'may not be used as a section name, as it is the name of a course group.',
                   4808:                     secn => 'Section names and group names must be distinct. Please choose a different section name.',
1.11      raeburn  4809:                  );                
1.8       raeburn  4810:     $setsection_js .= <<"ENDSECCODE";
                   4811: 
                   4812: function setSections(formname) {
                   4813:     var re1 = /^currsec_/;
                   4814:     var groups = new Array($groupslist);
                   4815:     for (var i=0;i<formname.elements.length;i++) {
                   4816:         var str = formname.elements[i].name;
                   4817:         var checkcurr = str.match(re1);
                   4818:         if (checkcurr != null) {
                   4819:             if ($checkincluded) {
1.28      raeburn  4820:                 $rolecode
1.8       raeburn  4821:                 if (role == 'cc') {
                   4822:                     alert("$alerts{'secd'}\\n$alerts{'accr'}");
                   4823:                 }
                   4824:                 else {
                   4825:                     var sections = '';
                   4826:                     var numsec = 0;
                   4827:                     var sections;
                   4828:                     for (var j=0; j<formname.elements[i].length; j++) {
                   4829:                         if (formname.elements[i].options[j].selected == true ) {
                   4830:                             if (formname.elements[i].options[j].value != "") {
                   4831:                                 if (numsec == 0) {
                   4832:                                     if (formname.elements[i].options[j].value != "") {
                   4833:                                         sections = formname.elements[i].options[j].value;
                   4834:                                         numsec ++;
                   4835:                                     }
                   4836:                                 }
                   4837:                                 else {
                   4838:                                     sections = sections + "," +  formname.elements[i].options[j].value
                   4839:                                     numsec ++;
                   4840:                                 }
                   4841:                             }
                   4842:                         }
                   4843:                     }
                   4844:                     if (numsec > 0) {
                   4845:                         if (formname.elements[i+1].value != "" && formname.elements[i+1].value != null) {
                   4846:                             sections = sections + "," +  formname.elements[i+1].value;
                   4847:                         }
                   4848:                     }
                   4849:                     else {
                   4850:                         sections = formname.elements[i+1].value;
                   4851:                     }
                   4852:                     var newsecs = formname.elements[i+1].value;
                   4853:                     var numsplit;
                   4854:                     if (newsecs != null && newsecs != "") {
                   4855:                         numsplit = newsecs.split(/,/g);
                   4856:                         numsec = numsec + numsplit.length;
                   4857:                     }
                   4858: 
                   4859:                     if ((role == 'st') && (numsec > 1)) {
                   4860:                         alert("$alerts{'inea'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}")
                   4861:                         return;
                   4862:                     }
                   4863:                     else {
                   4864:                         if (numsplit != null) {
                   4865:                             for (var j=0; j<numsplit.length; j++) {
                   4866:                                 if ((numsplit[j] == 'all') ||
                   4867:                                     (numsplit[j] == 'none')) {
                   4868:                                     alert("'"+numsplit[j]+"' $alerts{'mayn'}\\n$alerts{'plch'}");
                   4869:                                     return;
                   4870:                                 }
                   4871:                                 for (var k=0; k<groups.length; k++) {
                   4872:                                     if (numsplit[j] == groups[k]) {
                   4873:                                         alert("'"+numsplit[j]+"' $alerts{'mnot'}\\n$alerts{'secn'}");
                   4874:                                         return;
                   4875:                                     }
                   4876:                                 }
                   4877:                             }
                   4878:                         }
                   4879:                         formname.elements[i+2].value = sections;
                   4880:                     }
                   4881:                 }
                   4882:             }
                   4883:         }
                   4884:     }
                   4885:     $finish
                   4886: }
                   4887: ENDSECCODE
1.11      raeburn  4888:     return $setsection_js; 
1.8       raeburn  4889: }
                   4890: 
1.15      raeburn  4891: sub can_create_user {
                   4892:     my ($dom,$context,$usertype) = @_;
                   4893:     my %domconf = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   4894:     my $cancreate = 1;
1.28      raeburn  4895:     if (&Apache::lonnet::allowed('mau',$dom)) {
                   4896:         return $cancreate;
                   4897:     }
1.15      raeburn  4898:     if (ref($domconf{'usercreation'}) eq 'HASH') {
                   4899:         if (ref($domconf{'usercreation'}{'cancreate'}) eq 'HASH') {
1.97.2.3  raeburn  4900:             if ($context eq 'course' || $context eq 'author' || $context eq 'requestcrs') {
1.15      raeburn  4901:                 my $creation = $domconf{'usercreation'}{'cancreate'}{$context};
                   4902:                 if ($creation eq 'none') {
                   4903:                     $cancreate = 0;
                   4904:                 } elsif ($creation ne 'any') {
                   4905:                     if (defined($usertype)) {
                   4906:                         if ($creation ne $usertype) {
                   4907:                             $cancreate = 0;
                   4908:                         }
                   4909:                     }
                   4910:                 }
                   4911:             }
                   4912:         }
                   4913:     }
                   4914:     return $cancreate;
                   4915: }
                   4916: 
1.20      raeburn  4917: sub can_modify_userinfo {
                   4918:     my ($context,$dom,$fields,$userroles) = @_;
                   4919:     my %domconfig =
                   4920:        &Apache::lonnet::get_dom('configuration',['usermodification'],
                   4921:                                 $dom);
                   4922:     my %canmodify;
                   4923:     if (ref($fields) eq 'ARRAY') {
                   4924:         foreach my $field (@{$fields}) {
                   4925:             $canmodify{$field}  = 0;
                   4926:             if (&Apache::lonnet::allowed('mau',$dom)) {
                   4927:                 $canmodify{$field} = 1;
                   4928:             } else {
                   4929:                 if (ref($domconfig{'usermodification'}) eq 'HASH') {
                   4930:                     if (ref($domconfig{'usermodification'}{$context}) eq 'HASH') {
                   4931:                         if (ref($userroles) eq 'ARRAY') {
                   4932:                             foreach my $role (@{$userroles}) {
                   4933:                                 my $testrole;
1.60      raeburn  4934:                                 if ($context eq 'selfcreate') {
                   4935:                                     $testrole = $role;
1.20      raeburn  4936:                                 } else {
1.60      raeburn  4937:                                     if ($role =~ /^cr\//) {
                   4938:                                         $testrole = 'cr';
                   4939:                                     } else {
                   4940:                                         $testrole = $role;
                   4941:                                     }
1.20      raeburn  4942:                                 }
                   4943:                                 if (ref($domconfig{'usermodification'}{$context}{$testrole}) eq 'HASH') {
                   4944:                                     if ($domconfig{'usermodification'}{$context}{$testrole}{$field}) {
                   4945:                                         $canmodify{$field} = 1;
                   4946:                                         last;
                   4947:                                     }
                   4948:                                 }
                   4949:                             }
                   4950:                         } else {
                   4951:                             foreach my $key (keys(%{$domconfig{'usermodification'}{$context}})) {
                   4952:                                 if (ref($domconfig{'usermodification'}{$context}{$key}) eq 'HASH') {
                   4953:                                     if ($domconfig{'usermodification'}{$context}{$key}{$field}) {
                   4954:                                         $canmodify{$field} = 1;
                   4955:                                         last;
                   4956:                                     }
                   4957:                                 }
                   4958:                             }
                   4959:                         }
                   4960:                     }
                   4961:                 } elsif ($context eq 'course') {
                   4962:                     if (ref($userroles) eq 'ARRAY') {
                   4963:                         if (grep(/^st$/,@{$userroles})) {
                   4964:                             $canmodify{$field} = 1;
                   4965:                         }
                   4966:                     } else {
                   4967:                         $canmodify{$field} = 1;
                   4968:                     }
                   4969:                 }
                   4970:             }
                   4971:         }
                   4972:     }
                   4973:     return %canmodify;
                   4974: }
                   4975: 
1.18      raeburn  4976: sub check_usertype {
                   4977:     my ($dom,$uname,$rules) = @_;
                   4978:     my $usertype;
                   4979:     if (ref($rules) eq 'HASH') {
                   4980:         my @user_rules = keys(%{$rules});
                   4981:         if (@user_rules > 0) {
                   4982:             my %rule_check = &Apache::lonnet::inst_rulecheck($dom,$uname,undef,'username',\@user_rules);
                   4983:             if (keys(%rule_check) > 0) {
                   4984:                 $usertype = 'unofficial';
                   4985:                 foreach my $item (keys(%rule_check)) {
                   4986:                     if ($rule_check{$item}) {
                   4987:                         $usertype = 'official';
                   4988:                         last;
                   4989:                     }
                   4990:                 }
                   4991:             }
                   4992:         }
                   4993:     }
                   4994:     return $usertype;
                   4995: }
                   4996: 
1.17      raeburn  4997: sub roles_by_context {
1.97.2.4  raeburn  4998:     my ($context,$custom,$crstype) = @_;
1.17      raeburn  4999:     my @allroles;
                   5000:     if ($context eq 'course') {
1.97.2.2  raeburn  5001:         @allroles = ('st');
                   5002:         if ($env{'request.role'} =~ m{^dc\./}) {
                   5003:             push(@allroles,'ad');
                   5004:         }
1.97.2.4  raeburn  5005:         push(@allroles,('ta','ep','in'));
                   5006:         if ($crstype eq 'Community') {
                   5007:             push(@allroles,'co');
                   5008:         } else {
                   5009:             push(@allroles,'cc');
                   5010:         }
1.17      raeburn  5011:         if ($custom) {
                   5012:             push(@allroles,'cr');
                   5013:         }
                   5014:     } elsif ($context eq 'author') {
                   5015:         @allroles = ('ca','aa');
                   5016:     } elsif ($context eq 'domain') {
1.97.2.2  raeburn  5017:         @allroles = ('li','ad','dg','sc','au','dc');
1.17      raeburn  5018:     }
                   5019:     return @allroles;
                   5020: }
                   5021: 
1.16      raeburn  5022: sub get_permission {
1.97.2.4  raeburn  5023:     my ($context,$crstype) = @_;
1.16      raeburn  5024:     my %permission;
                   5025:     if ($context eq 'course') {
1.17      raeburn  5026:         my $custom = 1;
1.97.2.4  raeburn  5027:         my @allroles = &roles_by_context($context,$custom,$crstype);
1.17      raeburn  5028:         foreach my $role (@allroles) {
                   5029:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   5030:                 $permission{'cusr'} = 1;
                   5031:                 last;
                   5032:             }
1.16      raeburn  5033:         }
                   5034:         if (&Apache::lonnet::allowed('ccr',$env{'request.course.id'})) {
                   5035:             $permission{'custom'} = 1;
                   5036:         }
                   5037:         if (&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) {
                   5038:             $permission{'view'} = 1;
                   5039:         }
                   5040:         if (!$permission{'view'}) {
                   5041:             my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
                   5042:             $permission{'view'} =  &Apache::lonnet::allowed('vcl',$scope);
                   5043:             if ($permission{'view'}) {
                   5044:                 $permission{'view_section'} = $env{'request.course.sec'};
                   5045:             }
                   5046:         }
1.17      raeburn  5047:         if (!$permission{'cusr'}) {
                   5048:             if ($env{'request.course.sec'} ne '') {
                   5049:                 my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
                   5050:                 $permission{'cusr'} = (&Apache::lonnet::allowed('cst',$scope));
                   5051:                 if ($permission{'cusr'}) {
                   5052:                     $permission{'cusr_section'} = $env{'request.course.sec'};
                   5053:                 }
                   5054:             }
                   5055:         }
1.16      raeburn  5056:         if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
                   5057:             $permission{'grp_manage'} = 1;
                   5058:         }
                   5059:     } elsif ($context eq 'author') {
                   5060:         $permission{'cusr'} = &authorpriv($env{'user.name'},$env{'request.role.domain'});
                   5061:         $permission{'view'} = $permission{'cusr'};
                   5062:     } else {
1.17      raeburn  5063:         my @allroles = &roles_by_context($context);
                   5064:         foreach my $role (@allroles) {
1.28      raeburn  5065:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) {
                   5066:                 $permission{'cusr'} = 1;
1.17      raeburn  5067:                 last;
                   5068:             }
                   5069:         }
                   5070:         if (!$permission{'cusr'}) {
                   5071:             if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
                   5072:                 $permission{'cusr'} = 1;
                   5073:             }
1.16      raeburn  5074:         }
                   5075:         if (&Apache::lonnet::allowed('ccr',$env{'request.role.domain'})) {
                   5076:             $permission{'custom'} = 1;
                   5077:         }
                   5078:         $permission{'view'} = $permission{'cusr'};
                   5079:     }
                   5080:     my $allowed = 0;
                   5081:     foreach my $perm (values(%permission)) {
                   5082:         if ($perm) { $allowed=1; last; }
                   5083:     }
                   5084:     return (\%permission,$allowed);
                   5085: }
                   5086: 
                   5087: # ==================================================== Figure out author access
                   5088: 
                   5089: sub authorpriv {
                   5090:     my ($auname,$audom)=@_;
                   5091:     unless ((&Apache::lonnet::allowed('cca',$audom.'/'.$auname))
                   5092:          || (&Apache::lonnet::allowed('caa',$audom.'/'.$auname))) { return ''; }    return 1;
                   5093: }
                   5094: 
1.27      raeburn  5095: sub roles_on_upload {
1.97.2.4  raeburn  5096:     my ($context,$setting,$crstype,%customroles) = @_;
1.27      raeburn  5097:     my (@possible_roles,@permitted_roles);
1.97.2.4  raeburn  5098:     @possible_roles = &curr_role_permissions($context,$setting,1,$crstype);
1.27      raeburn  5099:     foreach my $role (@possible_roles) {
                   5100:         if ($role eq 'cr') {
                   5101:             push(@permitted_roles,keys(%customroles));
                   5102:         } else {
                   5103:             push(@permitted_roles,$role);
                   5104:         }
                   5105:     }
1.42      raeburn  5106:     return @permitted_roles;
1.27      raeburn  5107: }
                   5108: 
1.17      raeburn  5109: sub get_course_identity {
                   5110:     my ($cid) = @_;
                   5111:     my ($cnum,$cdom,$cdesc);
                   5112:     if ($cid eq '') {
                   5113:         $cid = $env{'request.course.id'}
                   5114:     }
                   5115:     if ($cid ne '') {
                   5116:         $cnum = $env{'course.'.$cid.'.num'};
                   5117:         $cdom = $env{'course.'.$cid.'.domain'};
                   5118:         $cdesc = $env{'course.'.$cid.'.description'};
                   5119:         if ($cnum eq '' || $cdom eq '') {
                   5120:             my %coursehash =
                   5121:                 &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
                   5122:             $cdom = $coursehash{'domain'};
                   5123:             $cnum = $coursehash{'num'};
                   5124:             $cdesc = $coursehash{'description'};
                   5125:         }
                   5126:     }
                   5127:     return ($cnum,$cdom,$cdesc);
                   5128: }
                   5129: 
1.19      raeburn  5130: sub dc_setcourse_js {
1.37      raeburn  5131:     my ($formname,$mode,$context) = @_;
                   5132:     my ($dc_setcourse_code,$authen_check);
1.19      raeburn  5133:     my $cctext = &Apache::lonnet::plaintext('cc');
                   5134:     my %alerts = &sectioncheck_alerts();
                   5135:     my $role = 'role';
                   5136:     if ($mode eq 'upload') {
                   5137:         $role = 'courserole';
1.37      raeburn  5138:     } else {
                   5139:         $authen_check = &verify_authen($formname,$context);
1.19      raeburn  5140:     }
                   5141:     $dc_setcourse_code = (<<"SCRIPTTOP");
1.37      raeburn  5142: $authen_check
                   5143: 
1.19      raeburn  5144: function setCourse() {
                   5145:     var course = document.$formname.dccourse.value;
                   5146:     if (course != "") {
                   5147:         if (document.$formname.dcdomain.value != document.$formname.origdom.value) {
                   5148:             alert("$alerts{'curd'}");
                   5149:             return;
                   5150:         }
                   5151:         var userrole = document.$formname.$role.options[document.$formname.$role.selectedIndex].value
                   5152:         var section="";
                   5153:         var numsections = 0;
                   5154:         var newsecs = new Array();
                   5155:         for (var i=0; i<document.$formname.currsec.length; i++) {
                   5156:             if (document.$formname.currsec.options[i].selected == true ) {
                   5157:                 if (document.$formname.currsec.options[i].value != "" && document.$formname.currsec.options[i].value != null) {
                   5158:                     if (numsections == 0) {
                   5159:                         section = document.$formname.currsec.options[i].value
                   5160:                         numsections = 1;
                   5161:                     }
                   5162:                     else {
                   5163:                         section = section + "," +  document.$formname.currsec.options[i].value
                   5164:                         numsections ++;
                   5165:                     }
                   5166:                 }
                   5167:             }
                   5168:         }
                   5169:         if (document.$formname.newsec.value != "" && document.$formname.newsec.value != null) {
                   5170:             if (numsections == 0) {
                   5171:                 section = document.$formname.newsec.value
                   5172:             }
                   5173:             else {
                   5174:                 section = section + "," +  document.$formname.newsec.value
                   5175:             }
                   5176:             newsecs = document.$formname.newsec.value.split(/,/g);
                   5177:             numsections = numsections + newsecs.length;
                   5178:         }
                   5179:         if ((userrole == 'st') && (numsections > 1)) {
                   5180:             alert("$alerts{'inea'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
                   5181:             return;
                   5182:         }
                   5183:         for (var j=0; j<newsecs.length; j++) {
                   5184:             if ((newsecs[j] == 'all') || (newsecs[j] == 'none')) {
                   5185:                 alert("'"+newsecs[j]+"' $alerts{'mayn'}.\\n$alerts{'plsc'}.");
                   5186:                 return;
                   5187:             }
                   5188:             if (document.$formname.groups.value != '') {
                   5189:                 var groups = document.$formname.groups.value.split(/,/g);
                   5190:                 for (var k=0; k<groups.length; k++) {
                   5191:                     if (newsecs[j] == groups[k]) {
                   5192:                         alert("'"+newsecs[j]+"' $alerts{'mayt'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
                   5193:                         return;
                   5194:                     }
                   5195:                 }
                   5196:             }
                   5197:         }
                   5198:         if ((userrole == 'cc') && (numsections > 0)) {
                   5199:             alert("$alerts{'secd'} $cctext $alerts{'role'}.\\n$alerts{'accr'}.");
                   5200:             section = "";
                   5201:         }
                   5202: SCRIPTTOP
                   5203:     if ($mode ne 'upload') {
                   5204:         $dc_setcourse_code .= (<<"ENDSCRIPT");
                   5205:         var coursename = "_$env{'request.role.domain'}"+"_"+course+"_"+userrole
                   5206:         var numcourse = getIndex(document.$formname.dccourse);
                   5207:         if (numcourse == "-1") {
                   5208:             alert("$alerts{'thwa'}");
                   5209:             return;
                   5210:         }
                   5211:         else {
                   5212:             document.$formname.elements[numcourse].name = "act"+coursename;
                   5213:             var numnewsec = getIndex(document.$formname.newsec);
                   5214:             if (numnewsec != "-1") {
                   5215:                 document.$formname.elements[numnewsec].name = "sec"+coursename;
                   5216:                 document.$formname.elements[numnewsec].value = section;
                   5217:             }
                   5218:             var numstart = getIndex(document.$formname.start);
                   5219:             if (numstart != "-1") {
                   5220:                 document.$formname.elements[numstart].name = "start"+coursename;
                   5221:             }
                   5222:             var numend = getIndex(document.$formname.end);
                   5223:             if (numend != "-1") {
                   5224:                 document.$formname.elements[numend].name = "end"+coursename
                   5225:             }
                   5226:         }
                   5227:     }
1.37      raeburn  5228:     var authcheck = auth_check();
                   5229:     if (authcheck == 'ok') {
                   5230:         document.$formname.submit();
                   5231:     }
1.19      raeburn  5232: }
                   5233: ENDSCRIPT
                   5234:     } else {
                   5235:         $dc_setcourse_code .=  "
                   5236:         document.$formname.sections.value = section;
                   5237:     }
                   5238:     return 'ok';
                   5239: }
                   5240: ";
                   5241:     }
                   5242:     $dc_setcourse_code .= (<<"ENDSCRIPT");
                   5243: 
                   5244:     function getIndex(caller) {
                   5245:         for (var i=0;i<document.$formname.elements.length;i++) {
                   5246:             if (document.$formname.elements[i] == caller) {
                   5247:                 return i;
                   5248:             }
                   5249:         }
                   5250:         return -1;
                   5251:     }
                   5252: ENDSCRIPT
1.37      raeburn  5253:     return $dc_setcourse_code;
                   5254: }
                   5255: 
                   5256: sub verify_authen {
                   5257:     my ($formname,$context) = @_;
                   5258:     my %alerts = &authcheck_alerts();
                   5259:     my $finish = "return 'ok';";
                   5260:     if ($context eq 'author') {
                   5261:         $finish = "document.$formname.submit();";
                   5262:     }
                   5263:     my $outcome = <<"ENDSCRIPT";
                   5264: 
                   5265: function auth_check() {
                   5266:     var logintype;
                   5267:     if (document.$formname.login.length) {
                   5268:         if (document.$formname.login.length > 0) {
                   5269:             var loginpicked = 0;
                   5270:             for (var i=0; i<document.$formname.login.length; i++) {
                   5271:                 if (document.$formname.login[i].checked == true) {
                   5272:                     loginpicked = 1;
                   5273:                     logintype = document.$formname.login[i].value;
                   5274:                 }
                   5275:             }
                   5276:             if (loginpicked == 0) {
                   5277:                 alert("$alerts{'authen'}");
                   5278:                 return;
                   5279:             }
                   5280:         }
                   5281:     } else {
                   5282:         logintype = document.$formname.login.value;
                   5283:     }
                   5284:     if (logintype == 'nochange') {
                   5285:         return 'ok';
                   5286:     }
                   5287:     var argpicked = document.$formname.elements[logintype+'arg'].value;
                   5288:     if ((argpicked == null) || (argpicked == '') || (typeof argpicked == 'undefined')) {
                   5289:         var alertmsg = '';
                   5290:         switch (logintype) {
                   5291:             case 'krb':
                   5292:                 alertmsg = '$alerts{'krb'}';
                   5293:                 break;
                   5294:             case 'int':
                   5295:                 alertmsg = '$alerts{'ipass'}';
                   5296:             case 'fsys':
                   5297:                 alertmsg = '$alerts{'ipass'}';
                   5298:                 break;
                   5299:             case 'loc':
                   5300:                 alertmsg = '';
                   5301:                 break;
                   5302:             default:
                   5303:                 alertmsg = '';
                   5304:         }
                   5305:         if (alertmsg != '') {
                   5306:             alert(alertmsg);
                   5307:             return;
                   5308:         }
                   5309:     }
                   5310:     $finish
                   5311: }
                   5312: ENDSCRIPT
1.19      raeburn  5313: }
                   5314: 
                   5315: sub sectioncheck_alerts {
                   5316:     my %alerts = &Apache::lonlocal::texthash(
                   5317:                     curd => 'You must select a course in the current domain',
                   5318:                     inea => 'In each course, each user may only have one student role at a time',
                   5319:                     youh => 'You had selected',
                   5320:                     sect => 'sections',
                   5321:                     plsm => 'Please modify your selections so they include no more than one section',
                   5322:                     mayn => 'may not be used as the name for a section, as it is a reserved word',
                   5323:                     plsc => 'Please choose a different section name',
                   5324:                     mayt => 'may not be used as the name for a section, as it is the name of a course group',
                   5325:                     secn => 'Section names and group names must be distinct',
                   5326:                     secd => 'Section designations do not apply to ',
                   5327:                     role => 'roles',
                   5328:                     accr => 'role will be added with access to all sections',
                   5329:                     thwa => 'There was a problem with your course selection'
                   5330:                  );
                   5331:     return %alerts;
                   5332: }
1.17      raeburn  5333: 
1.37      raeburn  5334: sub authcheck_alerts {
                   5335:     my %alerts = 
                   5336:         &Apache::lonlocal::texthash(
                   5337:                     authen => 'You must choose an authentication type.',
                   5338:                     krb    => 'You need to specify the Kerberos domain.',
                   5339:                     ipass  => 'You need to specify the initial password.',
                   5340:         );
                   5341:     return %alerts;
                   5342: }
                   5343: 
1.1       raeburn  5344: 1;
                   5345: 

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