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

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

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