File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.50: download - view: text, annotated - select for diffs
Tue Sep 17 15:52:44 2002 UTC (21 years, 9 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Highlights:
Now have 'modify student data' options, although you cannot actually modify
any student data yet.
Selecting a students name in the classlist display will take you to their
'aboutme' page.

Gory details:
Many internal changes to state keeping.
The current page displayed is determined by two parameters, 'action' and
'state'.
Changed subroutine names to describe what they do.
Reformatted single student enrollment form.
Added input variable to &show_class_list to determine what sort of link
to place around user names.
Modified &print_html_classlist to take advantage of new &show_class_list
Added &print_modify_student_form, which works wonderfully.
Added &modify_single_student, which tells you what it wants to do but
cannot actually do it.
Added &get_enrollment_data, which will probably be removed soon.
Added &drop_students to do just that.
Modified &handler to implement state keeping changes and document it.

    1: # The LearningOnline Network with CAPA
    2: # Handler to drop and add students in courses 
    3: #
    4: # $Id: londropadd.pm,v 1.50 2002/09/17 15:52:44 matthew Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # (Handler to set parameters for assessments
   29: #
   30: # (Handler to resolve ambiguous file locations
   31: #
   32: # (TeX Content Handler
   33: #
   34: ###############################################################
   35: ###############################################################
   36: 
   37: package Apache::londropadd;
   38: 
   39: use strict;
   40: use Apache::lonnet();
   41: use Apache::loncommon();
   42: use Apache::lonhtmlcommon();
   43: use Apache::Constants qw(:common :http REDIRECT);
   44: 
   45: ###############################################################
   46: ###############################################################
   47: sub header {
   48:     my $bodytag=&Apache::loncommon::bodytag('Enrollment Manager');
   49:     return(<<ENDHEAD);
   50: <html>
   51: <head>
   52: <title>LON-CAPA Enrollment Manager</title>
   53: </head>
   54: $bodytag
   55: <form method="post" enctype="multipart/form-data"  
   56:       action="/adm/dropadd" name="studentform">
   57: ENDHEAD
   58: }
   59: 
   60: ###############################################################
   61: ###############################################################
   62: # Drop student from all sections of a course, except optional $csec
   63: sub modifystudent {
   64:     my ($udom,$unam,$courseid,$csec,$desiredhost)=@_;
   65:     # if $csec is undefined, drop the student from all the courses matching
   66:     # this one.  If $csec is defined, drop them from all other sections of 
   67:     # this course and add them to section $csec
   68:     $courseid=~s/\_/\//g;
   69:     $courseid=~s/^(\w)/\/$1/;
   70:     my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
   71:     my ($tmp) = keys(%roles);
   72:     # Bail out if we were unable to get the students roles
   73:     return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
   74:     # Go through the roles looking for enrollment in this course
   75:     my $result = '';
   76:     foreach my $course (keys(%roles)) {
   77:         if ($course=~/^$courseid(?:\/)*(?:\s+)*(\w+)*\_st$/) {
   78:             # We are in this course
   79:             my $section=$1;
   80:             $section='' if ($course eq $courseid.'_st');
   81:             if ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
   82:                 my (undef,$end,$start)=split(/\_/,$roles{$course});
   83:                 my $now=time;
   84:                 # if this is an active role 
   85:                 if (!($start && ($now<$start)) || !($end && ($now>$end))) {
   86:                     my $reply=&Apache::lonnet::modifystudent
   87:                         ($udom,$unam,'','','','','','','',
   88:                          $section,time,undef,undef,$desiredhost);
   89:                     $result .= $reply.':';
   90:                 }
   91:             }
   92:         }
   93:     }
   94:     if ($result eq '') {
   95:         $result eq 'Unable to find section for this student';
   96:     } else {
   97:         $result =~ s/(ok:)+/ok/g;
   98:     }
   99:     return $result;
  100: }
  101: 
  102: ###############################################################
  103: ###############################################################
  104: # build a domain and server selection form
  105: sub domain_form {
  106:     my ($defdom) = @_;
  107:     # Set up domain and server selection forms
  108:     #
  109:     # Get the domains
  110:     my @domains = &Apache::loncommon::get_domains();
  111:     # build up the menu information to be passed to 
  112:     # &Apache::loncommon::linked_select_forms
  113:     my %select_menus;
  114:     foreach my $dom (@domains) {
  115:         # set up the text for this domain
  116:         $select_menus{$dom}->{'text'}= $dom;
  117:         # we want a choice of 'default' as the default in the second menu
  118:         $select_menus{$dom}->{'default'}= 'default';
  119:         $select_menus{$dom}->{'select2'}->{'default'} = 'default';
  120:         # Now build up the other items in the second menu
  121:         my %servers = &Apache::loncommon::get_library_servers($dom);
  122:         foreach my $server (keys(%servers)) {
  123:             $select_menus{$dom}->{'select2'}->{$server} 
  124:                                             = "$server $servers{$server}";
  125:         }
  126:     }
  127:     my $result  = &Apache::loncommon::linked_select_forms
  128:         ('studentform',' with home server ',$defdom,
  129:          'lcdomain','lcserver',\%select_menus);
  130:     return $result;
  131: }
  132: 
  133: ###############################################################
  134: ###############################################################
  135: #  Menu Phase One
  136: sub print_main_menu {
  137:     my $r=shift;
  138:     $r->print(<<END);
  139: <p>
  140: <font size="+1">
  141:     <a href="/adm/dropadd?action=upload">Upload a course list</a>
  142: </font>
  143: </p><p>
  144: <font size="+1">
  145:     <a href="/adm/dropadd?action=enrollstudent">Enroll a single student</a>
  146: </font>
  147: </p><p>
  148: <font size="+1">
  149:     <a href="/adm/dropadd?action=modifystudent">Modify student data</a>
  150: </font>
  151: </p><p>
  152: <font size="+1">
  153:     <a href="/adm/dropadd?action=classlist">View Classlist</a>
  154: </font>
  155: </p><p>
  156: <font size="+1">
  157:     <a href="/adm/dropadd?action=drop">Drop Students</a>
  158: </font>
  159: </p>
  160: END
  161: }
  162: 
  163: ###############################################################
  164: ###############################################################
  165: sub print_upload_manager_header {
  166:     my ($r,$datatoken,$distotal,$krbdefdom)=@_;
  167:     my $javascript;
  168:     if (! exists($ENV{'form.upfile_associate'})) {
  169:         $ENV{'form.upfile_associate'} = 'forward';
  170:     }
  171:     if ($ENV{'form.associate'} eq 'Reverse Association') {
  172:         if ( $ENV{'form.upfile_associate'} ne 'reverse' ) {
  173:             $ENV{'form.upfile_associate'} = 'reverse';
  174:         } else {
  175:             $ENV{'form.upfile_associate'} = 'forward';
  176:         }
  177:     }
  178:     if ($ENV{'form.upfile_associate'} eq 'reverse') {
  179: 	$javascript=&upload_manager_javascript_reverse_associate();
  180:     } else {
  181: 	$javascript=&upload_manager_javascript_forward_associate();
  182:     }
  183:     my $javascript_validations=&javascript_validations($krbdefdom);
  184:     $r->print(<<ENDPICK);
  185: <h3>Uploading Class List</h3>
  186: <hr>
  187: <h3>Identify fields</h3>
  188: Total number of records found in file: $distotal <hr />
  189: Enter as many fields as you can. The system will inform you and bring you back
  190: to this page if the data selected is insufficient to run your class.<hr />
  191: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
  192: <input type="hidden" name="action"     value="upload" />
  193: <input type="hidden" name="state"      value="got_file" />
  194: <input type="hidden" name="associate"  value="" />
  195: <input type="hidden" name="datatoken"  value="$datatoken" />
  196: <input type="hidden" name="fileupload" value="$ENV{'form.fileupload'}" />
  197: <input type="hidden" name="upfiletype" value="$ENV{'form.upfiletype'}" />
  198: <input type="hidden" name="upfile_associate" 
  199:                                        value="$ENV{'form.upfile_associate'}" />
  200: <hr />
  201: <script type="text/javascript" language="Javascript">
  202: $javascript
  203: $javascript_validations
  204: </script>
  205: ENDPICK
  206: }
  207: 
  208: ###############################################################
  209: ###############################################################
  210: sub javascript_validations {
  211:     my ($krbdefdom)=@_;
  212:     my %param = ( formname => 'studentform',
  213:                   kerb_def_dom => $krbdefdom );
  214:     my $authheader = &Apache::loncommon::authform_header(%param);
  215:     return (<<ENDPICK);
  216: function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec) {
  217:     var foundatype=0;
  218:     var message='';
  219:     if (founduname==0) {
  220: 	alert('You need to specify the username field');
  221:         return;
  222:     }
  223:     if (current.radiovalue == null || current.radiovalue == 'nochange') {
  224:         // They did not check any of the login radiobuttons.
  225:         alert('You must choose an authentication type');
  226:         return;
  227:     }
  228:     foundatype=1;
  229:     if (current.argfield == null || current.argfield == '') {
  230:         var alertmsg = '';
  231:         switch (current.value) {
  232:             case 'krb': 
  233:                 alertmsg = 'You need to specify the Kerberos domain';
  234:                 break;
  235:             case 'loc':
  236:             case 'fsys':
  237:                 alertmsg = 'You need to specify the initial password';
  238:                 break;
  239:             case 'fsys':
  240:                 alertmsg = '';
  241:                 break;
  242:             default: 
  243:                 alertmsg = '';
  244:         }
  245:         if (alertmsg != '') {
  246:             alert(alertmsg);
  247:             return;
  248:         }
  249:     }
  250: 
  251:     if (foundname==0) { message='No name fields specified. '; }
  252:     if (foundid==0) { message+='No ID or student number field specified. '; }
  253:     if (foundsec==0) { message+='No section or group field specified. '; }
  254:     if (vf.startdate.value=='') {
  255: 	message+='No starting date set. ';
  256:     }
  257:     if (vf.enddate.value=='') {
  258:         message+='No ending date set. ';
  259:     }
  260:     if ((vf.enddate.value!='') && (vf.startdate.value!='')) {
  261:        if (Math.round(vf.enddate.value)<Math.round(vf.startdate.value)) {
  262:           alert('Ending date is before starting date');
  263:           return;
  264:        }
  265:     }
  266:     if (message!='') {
  267:        message+='Continue enrollment?';
  268:        if (confirm(message)) {
  269: 	  pclose();
  270:           vf.state.value='enrolling';
  271: 	  vf.submit();
  272:        }
  273:     } else {
  274:       pclose();
  275:       vf.state.value='enrolling';
  276:       vf.submit();
  277:     }
  278: }
  279: 
  280: 
  281:     function pclose() {
  282:         parmwin=window.open("/adm/rat/empty.html","LONCAPAparms",
  283:                  "height=350,width=350,scrollbars=no,menubar=no");
  284:         parmwin.close();
  285:     }
  286: 
  287:     function pjump(type,dis,value,marker,ret,call) {
  288:         parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
  289:                  +"&value="+escape(value)+"&marker="+escape(marker)
  290:                  +"&return="+escape(ret)
  291:                  +"&call="+escape(call)+"&name="+escape(dis),"LONCAPAparms",
  292:                  "height=350,width=350,scrollbars=no,menubar=no");
  293: 
  294:     }
  295: 
  296:     function dateset() {
  297:         if (document.studentform.pres_marker.value=='end') {
  298:            document.studentform.enddate.value=
  299: 	       document.studentform.pres_value.value;
  300:         }
  301:         if (document.studentform.pres_marker.value=='start') {
  302:            document.studentform.startdate.value=
  303: 	       document.studentform.pres_value.value;
  304:         }
  305:         pclose();
  306:     }
  307: 
  308: $authheader
  309: ENDPICK
  310: 
  311: }
  312: 
  313: ###############################################################
  314: ###############################################################
  315: sub upload_manager_javascript_forward_associate {
  316:     return(<<ENDPICK);
  317: function verify(vf) {
  318:     var founduname=0;
  319:     var foundpwd=0;
  320:     var foundname=0;
  321:     var foundid=0;
  322:     var foundsec=0;
  323:     var tw;
  324:     for (i=0;i<=vf.nfields.value;i++) {
  325:         tw=eval('vf.f'+i+'.selectedIndex');
  326:         if (tw==1) { founduname=1; }
  327:         if ((tw>=2) && (tw<=6)) { foundname=1; }
  328:         if (tw==7) { foundid=1; }
  329:         if (tw==8) { foundsec=1; }
  330:         if (tw==9) { foundpwd=1; }
  331:     }
  332:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
  333: }
  334: 
  335: //
  336: // vf = this.form
  337: // tf = column number
  338: //
  339: // values of nw
  340: //
  341: // 0 = none
  342: // 1 = username
  343: // 2 = names (lastname, firstnames)
  344: // 3 = fname (firstname)
  345: // 4 = mname (middlename)
  346: // 5 = lname (lastname)
  347: // 6 = gen   (generation)
  348: // 7 = id
  349: // 8 = section
  350: // 9 = ipwd  (password)
  351: //
  352: function flip(vf,tf) {
  353:    var nw=eval('vf.f'+tf+'.selectedIndex');
  354:    var i;
  355:    // make sure no other columns are labeled the same as this one
  356:    for (i=0;i<=vf.nfields.value;i++) {
  357:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
  358:           eval('vf.f'+i+'.selectedIndex=0;')
  359:       }
  360:    }
  361:    // If we set this to 'lastname, firstnames', clear out all the ones
  362:    // set to 'fname','mname','lname','gen' (3,4,5,6) currently.
  363:    if (nw==2) {
  364:       for (i=0;i<=vf.nfields.value;i++) {
  365:          if ((eval('vf.f'+i+'.selectedIndex')>=3) &&
  366:              (eval('vf.f'+i+'.selectedIndex')<=6)) {
  367:              eval('vf.f'+i+'.selectedIndex=0;')
  368:          }
  369:       }
  370:    }
  371:    // If we set this to one of 'fname','mname','lname','gen' (3,4,5,6),
  372:    // clear out any that are set to 'lastname, firstnames' (2)
  373:    if ((nw>=3) && (nw<=6)) {
  374:       for (i=0;i<=vf.nfields.value;i++) {
  375:          if (eval('vf.f'+i+'.selectedIndex')==2) {
  376:              eval('vf.f'+i+'.selectedIndex=0;')
  377:          }
  378:       }
  379:    }
  380:    // If we set the password, make the password form below correspond to 
  381:    // the new value.
  382:    if (nw==9) {
  383:        changed_radio('int',document.studentform);
  384:        set_auth_radio_buttons('int',document.studentform);
  385:        vf.intarg.value='';
  386:        vf.krbarg.value='';
  387:        vf.locarg.value='';
  388:    }
  389: }
  390: 
  391: function clearpwd(vf) {
  392:     var i;
  393:     for (i=0;i<=vf.nfields.value;i++) {
  394:         if (eval('vf.f'+i+'.selectedIndex')==9) {
  395:             eval('vf.f'+i+'.selectedIndex=0;')
  396:         }
  397:     }
  398: }
  399: 
  400: ENDPICK
  401: }
  402: 
  403: ###############################################################
  404: ###############################################################
  405: sub upload_manager_javascript_reverse_associate {
  406:     return(<<ENDPICK);
  407: function verify(vf) {
  408:     var founduname=0;
  409:     var foundpwd=0;
  410:     var foundname=0;
  411:     var foundid=0;
  412:     var foundsec=0;
  413:     var tw;
  414:     for (i=0;i<=vf.nfields.value;i++) {
  415:         tw=eval('vf.f'+i+'.selectedIndex');
  416:         if (i==0 && tw!=0) { founduname=1; }
  417:         if (((i>=1) && (i<=5)) && tw!=0 ) { foundname=1; }
  418:         if (i==6 && tw!=0) { foundid=1; }
  419:         if (i==7 && tw!=0) { foundsec=1; }
  420:         if (i==8 && tw!=0) { foundpwd=1; }
  421:     }
  422:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
  423: }
  424: 
  425: function flip(vf,tf) {
  426:    var nw=eval('vf.f'+tf+'.selectedIndex');
  427:    var i;
  428:    // picked the all one one name field, reset the other name ones to blank
  429:    if (tf==1 && nw!=0) {
  430:       for (i=2;i<=5;i++) {
  431:          eval('vf.f'+i+'.selectedIndex=0;')
  432:       }
  433:    }
  434:    //picked one of the piecewise name fields, reset the all in
  435:    //one field to blank
  436:    if ((tf>=2) && (tf<=5) && (nw!=0)) {
  437:       eval('vf.f1.selectedIndex=0;')
  438:    }
  439:    // intial password specified, pick internal authentication
  440:    if (tf==8 && nw!=0) {
  441:        changed_radio('int',document.studentform);
  442:        set_auth_radio_buttons('int',document.studentform);
  443:        vf.krbarg.value='';
  444:        vf.intarg.value='';
  445:        vf.locarg.value='';
  446:    }
  447: }
  448: 
  449: function clearpwd(vf) {
  450:     var i;
  451:     if (eval('vf.f8.selectedIndex')!=0) {
  452:         eval('vf.f8.selectedIndex=0;')
  453:     }
  454: }
  455: ENDPICK
  456: }
  457: 
  458: ###############################################################
  459: ###############################################################
  460: sub print_upload_manager_footer {
  461:     my ($r,$i,$keyfields,$defdom,$today,$halfyear)=@_;
  462:     my %param = ( formname => 'document.studentform');
  463:     my $krbform = &Apache::loncommon::authform_kerberos(%param);
  464:     my $intform = &Apache::loncommon::authform_internal(%param);
  465:     my $locform = &Apache::loncommon::authform_local(%param);
  466:     my $domform = &domain_form($defdom);
  467:     $r->print(<<ENDPICK);
  468: </table>
  469: <input type=hidden name=nfields value=$i>
  470: <input type=hidden name=keyfields value="$keyfields">
  471: <h3>Login Type</h3>
  472: <p>Note: this will not take effect if the user already exists</p>
  473: <p>
  474: $krbform
  475: </p>
  476: <p>
  477: $intform
  478: </p>
  479: <p>
  480: $locform
  481: </p>
  482: <h3>LON-CAPA Domain for Students</h3>
  483: LON-CAPA domain: $domform <p>
  484: <h3>Starting and Ending Dates</h3>
  485: <input type="hidden" value=''          name="pres_value"  >
  486: <input type="hidden" value=''          name="pres_type"   >
  487: <input type="hidden" value=''          name="pres_marker" >
  488: <input type="hidden" value='$today'    name="startdate"   >
  489: <input type="hidden" value='$halfyear' name="enddate"     >
  490: <a 
  491:  href="javascript:pjump('date_start','Enrollment Starting Date',document.studentform.startdate.value,'start','studentform.pres','dateset');"
  492: >Set Starting Date</a><p>
  493: 
  494: <a 
  495:  href="javascript:pjump('date_end','Enrollment Ending Date',document.studentform.enddate.value,'end','studentform.pres','dateset');"
  496: >Set Ending Date</a><p>
  497: <h3>Full Update</h3>
  498: <input type=checkbox name=fullup value=yes> Full update 
  499: (also print list of users not enrolled anymore)<p>
  500: <h3>ID/Student Number</h3>
  501: <input type=checkbox name=forceid value=yes> 
  502: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
  503: (only do if you know what you are doing)<p>
  504: <input type="button" onClick="javascript:verify(this.form)" value="Update Courselist" /><br />
  505: Note: for large courses, this operation may be time consuming.
  506: ENDPICK
  507: }
  508: 
  509: # ======================================================= Menu Phase Two Upload
  510: sub print_upload_manager_form {
  511:     my $r=shift;
  512: 
  513:     my $datatoken;
  514:     if (!$ENV{'form.datatoken'}) {
  515:       $datatoken=&Apache::loncommon::upfile_store($r);
  516:     } else {
  517:       $datatoken=$ENV{'form.datatoken'};
  518:       &Apache::loncommon::load_tmp_file($r);
  519:     }
  520:     my @records=&Apache::loncommon::upfile_record_sep();
  521:     my $total=$#records;
  522:     my $distotal=$total+1;
  523:     $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
  524:     my $krbdefdom=$1;
  525:     $krbdefdom=~tr/a-z/A-Z/;
  526:     my $today=time;
  527:     my $halfyear=$today+15552000;
  528:     my $defdom=$r->dir_config('lonDefDomain');
  529:     &print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom);
  530:     my $i;
  531:     my $keyfields;
  532:     if ($total>=0) {
  533: 	my @d=(['username','Username'],
  534:                ['names','Last Name, First Names'],
  535: 	       ['fname','First Name'],
  536:                ['mname','Middle Names/Initials'],
  537: 	       ['lname','Last Name'],
  538:                ['gen','Generation'],
  539: 	       ['id','ID/Student Number'],
  540:                ['sec','Group/Section'],
  541: 	       ['ipwd','Initial Password']);
  542: 	if ($ENV{'form.upfile_associate'} eq 'reverse') {	
  543: 	    &Apache::loncommon::csv_print_samples($r,\@records);
  544: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,\@d);
  545: 	    foreach (@d) { $keyfields.=$_->[0].','; }
  546: 	    chop($keyfields);
  547: 	} else {
  548: 	    unshift(@d,['none','']);
  549: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,\@d);
  550: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
  551: 	    $keyfields=join(',',sort(keys(%sone)));
  552: 	}
  553:     }
  554:     &print_upload_manager_footer($r,$i,$keyfields,$defdom,$today,$halfyear);
  555: }
  556: 
  557: # ======================================================= Enroll single student
  558: sub enroll_single_student {
  559:     my $r=shift;
  560:     $r->print('<h3>Enrolling Student</h3>');
  561:     $r->print('<p>Enrolling '.$ENV{'form.cuname'}." in domain ".
  562:               $ENV{'form.lcdomain'}.'</p>');
  563:     if (($ENV{'form.cuname'})&&($ENV{'form.cuname'}!~/\W/)&&
  564:         ($ENV{'form.lcdomain'})&&($ENV{'form.lcdomain'}!~/\W/)) {
  565:         # Deal with home server selection
  566:         my $domain=$ENV{'form.lcdomain'};
  567:         my $desiredhost = $ENV{'form.lcserver'};
  568:         if (lc($desiredhost) eq 'default') {
  569:             $desiredhost = undef;
  570:         } else {
  571:             my %home_servers =&Apache::loncommon::get_library_servers($domain);
  572:             if (! exists($home_servers{$desiredhost})) {
  573:                 $r->print('<font color="#ff0000">Error:</font>'.
  574:                           'Invalid home server specified');
  575:                 return;
  576:             }
  577:         }
  578:         $r->print(" with server $desiredhost :") if (defined($desiredhost));
  579:         # End of home server selection logic
  580: 	my $amode='';
  581:         my $genpwd='';
  582:         if ($ENV{'form.login'} eq 'krb') {
  583:            $amode='krb';
  584: 	   $amode.=$ENV{'form.krbver'};
  585:            $genpwd=$ENV{'form.krbarg'};
  586:         } elsif ($ENV{'form.login'} eq 'int') {
  587:            $amode='internal';
  588:            $genpwd=$ENV{'form.intarg'};
  589:         }  elsif ($ENV{'form.login'} eq 'loc') {
  590: 	    $amode='localauth';
  591: 	    $genpwd=$ENV{'form.locarg'};
  592: 	    if (!$genpwd) { $genpwd=" "; }
  593: 	}
  594:         my $home = &Apache::lonnet::homeserver($ENV{'form.cuname'},
  595:                                                    $ENV{'form.lcdomain'});
  596:         if ((($amode) && ($genpwd)) || ($home ne 'no_host')) {
  597:             &modifystudent($ENV{'form.lcdomain'},$ENV{'form.cuname'},
  598:                            $ENV{'request.course.id'},$ENV{'form.csec'},
  599:                             $desiredhost);
  600:           $r->print(&Apache::lonnet::modifystudent(
  601:                       $ENV{'form.lcdomain'},$ENV{'form.cuname'},
  602:                       $ENV{'form.cstid'},$amode,$genpwd,
  603:  	              $ENV{'form.cfirst'},$ENV{'form.cmiddle'},
  604:                       $ENV{'form.clast'},$ENV{'form.cgen'},
  605:                       $ENV{'form.csec'},$ENV{'form.enddate'},
  606:                       $ENV{'form.startdate'},$ENV{'form.forceid'},
  607:                     $desiredhost));
  608: 	} else {
  609:             $r->print('<p><font color="#ff0000">ERROR</font>&nbsp;'.
  610:                       'Invalid login mode or password.  '.
  611:                       'Unable to enroll '.$ENV{'form.cuname'}.'.</p>');
  612:         }          
  613:     } else {
  614:         $r->print('Invalid username or domain');
  615:     }    
  616: }
  617: 
  618: # ======================================================= Menu Phase Two Enroll
  619: sub print_enroll_single_student_form {
  620:     my $r=shift;
  621:     $r->print("<h3>Enroll One Student</h3>");
  622:     my ($krbdefdom) = $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
  623:     $krbdefdom=~tr/a-z/A-Z/;
  624:     my $today    = time;
  625:     my $halfyear = $today+15552000;
  626:     my $defdom=$r->dir_config('lonDefDomain');
  627:     my $javascript_validations=&javascript_validations($krbdefdom);
  628:     # Set up authentication forms
  629:     my %param = ( formname => 'document.studentform');
  630:     my $krbform = &Apache::loncommon::authform_kerberos(%param);
  631:     my $intform = &Apache::loncommon::authform_internal(%param);
  632:     my $locform = &Apache::loncommon::authform_local(%param);
  633:     # Set up domain selection form
  634:     my $domform = &domain_form($defdom);
  635:     # Print it all out
  636:     $r->print(<<END);
  637: <input type="hidden" name="action" value="enrollstudent">
  638: <input type="hidden" name="state"  value="done">
  639: 
  640: <script type="text/javascript" language="Javascript">
  641: function verify(vf) {
  642:     var founduname=0;
  643:     var foundpwd=0;
  644:     var foundname=0;
  645:     var foundid=0;
  646:     var foundsec=0;
  647:     var tw;
  648:     if ((typeof(vf.cuname.value) !="undefined") && (vf.cuname.value!='') && 
  649: 	(typeof(vf.lcdomain.value)!="undefined") && (vf.lcdomain.value!='')) {
  650:         founduname=1;
  651:     }
  652:     if ((typeof(vf.cfirst.value)!="undefined") && (vf.cfirst.value!='') &&
  653: 	(typeof(vf.clast.value) !="undefined") && (vf.clast.value!='')) {
  654:         foundname=1;
  655:     }
  656:     if ((typeof(vf.csec.value)!="undefined") && (vf.csec.value!='')) {
  657:         foundsec=1;
  658:     }
  659:     if ((typeof(vf.cstid.value)!="undefined") && (vf.cstid.value!='')) {
  660: 	foundid=1;
  661:     }
  662:     if (founduname==0) {
  663: 	alert('You need to specify at least the username and domain fields');
  664:         return;
  665:     }
  666:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
  667: }
  668: 
  669: $javascript_validations
  670: 
  671: function clearpwd(vf) {
  672:     //nothing else needs clearing
  673: }
  674: 
  675: </script>
  676: <h3>Personal Data</h3>
  677: <table>
  678: <tr><td>First Name:</td><td> <input type="text" name="cfirst"  size="15"></td></tr>
  679: <tr><td>Middle Name:</td><td> <input type="text" name="cmiddle" size="15"></td></tr>
  680: <tr><td>Last Name: </td><td><input type="text" name="clast"   size="15"></td></tr>
  681: <tr><td>Generation: </td><td><input type="text" name="cgen"    size="5"> </td></tr>
  682: </table>
  683: 
  684: <h3>Login Data</h3>
  685: <p>Username: <input type="text" name="cuname"  size="15"></p>
  686: <p>Domain:   $domform</p>
  687: <p>Note: login settings below  will not take effect if the user already exists
  688: </p><p>
  689: $krbform
  690: </p><p>
  691: $intform
  692: </p><p>
  693: $locform
  694: </p><p>
  695: 
  696: <h3>Course Data</h3>
  697: 
  698: <p>Group/Section: <input type="text" name="csec" size="5" />
  699: <p>
  700: <!-- Date setting form elements -->
  701: <input type="hidden" name="pres_value"  value='' />
  702: <input type="hidden" name="pres_type"   value='' />
  703: <input type="hidden" name="pres_marker" value='' />
  704: <input type="hidden" name="startdate"   value='$today'    />
  705: <input type="hidden" name="enddate"     value='$halfyear' />
  706: </p><p>
  707: <a 
  708:  href="javascript:pjump('date_start','Enrollment Starting Date',document.studentform.startdate.value,'start','studentform.pres','dateset');"
  709: >Set Starting Date</a>
  710: </p><p>
  711: <a 
  712:  href="javascript:pjump('date_end','Enrollment Ending Date',document.studentform.enddate.value,'end','studentform.pres','dateset');"
  713: >Set Ending Date</a>
  714: </p>
  715: <h3>ID/Student Number</h3>
  716: <p>
  717: ID/Student Number: <input type="text" name="cstid" size="10">
  718: </p><p>
  719: <input type="checkbox" name="forceid" value="yes"> 
  720: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
  721: (only do if you know what you are doing)
  722: </p><p>
  723: <input type="button" onClick="verify(this.form)" value="Enroll as student">
  724: </p>
  725: END
  726:     return;
  727: }
  728: 
  729: # =================================================== get the current classlist
  730: sub get_current_classlist {
  731:     my ($domain,$identifier) = @_;
  732:     # domain is the domain the class is being run in
  733:     # identifier is the internal, unique identifier for the class.
  734:     my %currentlist=();
  735:     my $now=time;
  736:     my %results=&Apache::lonnet::dump('classlist',$domain,$identifier);
  737:     my ($tmp) = keys(%results);
  738:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
  739:         foreach my $student (keys(%results)) {
  740:             # Extract the start and end dates
  741:             my ($end,$start)=split(/\:/,$results{$student});
  742:             # If the class isn't over, put it in the list
  743:             unless (($end) && ($now>$end)) { 
  744:                 $currentlist{$student}=1;
  745:             }
  746:         }
  747:         return (undef,%currentlist);
  748:     } else {
  749:         $tmp =~ s/^error://;
  750:         return ($tmp,undef);
  751:     }
  752: }
  753: 
  754: # ========================================================= Menu Phase Two Drop
  755: sub menu_phase_two_drop {
  756:     my $r=shift;
  757:     $r->print("<h3>Drop Students</h3>");
  758:     my $cid=$ENV{'request.course.id'};
  759:     my ($error,%currentlist)=&get_current_classlist
  760:         ($ENV{'course.'.$cid.'.domain'},$ENV{'course.'.$cid.'.num'});
  761:     if (defined($error)) {
  762:         if ($error =~ /^No such file or directory/) {
  763:             $r->print("There are no students currently enrolled.\n");
  764:         } else {
  765:             $r->print("<pre>ERROR:$error</pre>");
  766:         }
  767:     } elsif (!defined(%currentlist)) { 
  768:         $r->print("There are no students currently enrolled.\n");
  769:     } else {
  770:         # Print out the available choices
  771:         &show_drop_list($r,%currentlist);
  772:     }
  773: }
  774: 
  775: # ============================================== view classlist
  776: sub print_html_classlist {
  777:     my $r=shift;
  778:     $r->print(<<END);
  779: <p>
  780: <font size="+1">Current Classlist</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  781: <font size="+1"><a href="/adm/dropadd?action=classlist&state=csv">CSV format</a></font>
  782: </p>
  783: END
  784:     my $cid=$ENV{'request.course.id'};
  785:     my ($error,%currentlist)=&get_current_classlist
  786:         ($ENV{'course.'.$cid.'.domain'},$ENV{'course.'.$cid.'.num'});
  787:     if (defined($error)) {
  788:         if ($error =~ /^No such file or directory/) {
  789:             $r->print("There are no students currently enrolled.\n");
  790:         } else {
  791:             $r->print("<pre>ERROR:$error</pre>");
  792:         }
  793:     } elsif (!defined(%currentlist)) { 
  794:         $r->print("There are no students currently enrolled.\n");
  795:     } else {
  796:         # Print out the available choices
  797:         if ($ENV{'form.action'} eq 'modifystudent') {
  798:             &show_class_list($r,'view','modify',%currentlist);
  799:         } else {
  800:             &show_class_list($r,'view','aboutme',%currentlist);
  801:         }
  802:     }
  803: }
  804: 
  805: # ============================================== view classlist
  806: sub print_csv_classlist {
  807:     my $r=shift;
  808:     my $cid=$ENV{'request.course.id'};
  809:     my ($error,%currentlist)=&get_current_classlist
  810:         ($ENV{'course.'.$cid.'.domain'},$ENV{'course.'.$cid.'.num'});
  811:     if (defined($error)) {
  812:         if ($error =~ /^No such file or directory/) {
  813:             $r->print("There are no students currently enrolled.\n");
  814:         } else {
  815:             $r->print("<pre>ERROR:$error</pre>");
  816:         }
  817:     } elsif (!defined(%currentlist)) { 
  818:         $r->print("There are no students currently enrolled.\n");
  819:     } else {
  820:         &show_class_list($r,'csv','nolink',%currentlist);
  821:     }
  822: }
  823: 
  824: # =================================================== Show student list to drop
  825: sub show_class_list {
  826:     my ($r,$mode,$linkto,%currentlist)=@_;
  827:     my $cid=$ENV{'request.course.id'};
  828:     # Print out header 
  829:     if ($mode eq 'view') {
  830:         if ($linkto eq 'aboutme') {
  831:             $r->print('Select a user name to view the users page.');
  832:         } elsif ($linkto eq 'modify') {
  833:             $r->print('Select a user name to modify the students information');
  834:         }
  835:         $r->print(<<END);
  836: <p>
  837: <table border=2>
  838: <tr><th>username</th><th>domain</th><th>ID</th>
  839:     <th>student name</th><th>generation</th><th>section</th></tr>
  840: END
  841:     } elsif ($mode eq 'csv') {
  842:         $r->print('"'.join('","',("username","domain","ID","last name",
  843:                                   "first name","middle name","generation",
  844:                                   "section")).'"'."\n");
  845:     }
  846:     foreach (sort keys %currentlist) {
  847:         my ($sname,$sdom)=split(/\:/,$_);
  848:         my %reply=&Apache::lonnet::idrget($sdom,$sname);
  849:         my $ssec=&Apache::lonnet::usection($sdom,$sname,$cid);
  850:         my %info=&Apache::lonnet::get('environment',
  851:                                       ['firstname','middlename',
  852:                                        'lastname','generation'],
  853:                                       $sdom, $sname);
  854:         my ($tmp) = keys(%info);
  855:         if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
  856:             if ($mode eq 'view') {
  857:                 $r->print('<tr><td colspan="6"><font color="red">'.
  858:                     'Internal error: unable to get environment '.
  859:                         'for '.$sname.' in domain '.$sdom.'</font></td></tr>');
  860:             } else {
  861:                 $r->print('"Internal error: unable to get environment for '.
  862:                           $sname.' in domain '.$sdom.'"');
  863:             }
  864:         } else {
  865:             if ($mode eq 'view') {
  866:                 $r->print("<tr>\n    <td>\n        ");
  867:                 if ($linkto eq 'nothing') {
  868:                     $r->print($sname);
  869:                 } elsif ($linkto eq 'aboutme') {
  870:                     $r->print(&Apache::loncommon::aboutmewrapper($sname,$sname,
  871:                                                                  $sdom));
  872:                 } elsif ($linkto eq 'modify') {
  873:                     $r->print('<a href="/adm/dropadd?action=modifystudent'.
  874:                               '&state=selected'.'&sname='.$sname.
  875:                               '&sdom='.$sdom.'">'.$sname."</a>\n");
  876:                 }
  877:                 $r->print(<<"END");
  878:     </td>
  879:     <td>$sdom</td>
  880:     <td>$reply{$sname}</td>
  881:     <td>$info{'lastname'}, $info{'firstname'} $info{'middlename'}</td>
  882:     <td>$info{'generation'}</td>
  883:     <td>$ssec</td>
  884: </tr>
  885: END
  886:             } elsif ($mode eq 'csv') {
  887:                 # no need to bother with $linkto
  888:                 my @line = ();
  889:                 foreach ($sname,$sdom,$reply{$sname},
  890:                          $info{'lastname'},$info{'firstname'},
  891:                          $info{'middlename'},$info{'generation'},$ssec) {
  892:                     push @line,&Apache::loncommon::csv_translate($_);
  893:                 }
  894:                 my $tmp = $";
  895:                 $" = '","';
  896:                 $r->print("\"@line\"\n");
  897:                 $" = $tmp;
  898:             }
  899:         }
  900:     }
  901:     $r->print('</table><br>') if ($mode eq 'view');
  902: }
  903: 
  904: 
  905: #
  906: # print out form for modification of a single students data
  907: #
  908: sub print_modify_student_form {
  909:     my $r = shift();
  910:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  911:                                             ['sdom','sname']);
  912:     
  913:     my $sname = $ENV{'form.sname'};
  914:     my $sdom  = $ENV{'form.sdom'};
  915:     # determine the students name information
  916:     my %info=&Apache::lonnet::get('environment',
  917:                                   ['firstname','middlename',
  918:                                    'lastname','generation'],
  919:                                   $sdom, $sname);
  920:     my ($tmp) = keys(%info);
  921:     if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
  922:         $r->print('<font color="#ff0000" size="+2">Error</font>'.
  923:                   '<p>'.
  924:                   'Unable to retrieve environment data for '.$sname.
  925:                   'in domain '.$sdom.'</p><p>'.
  926:                   'Please contact your LON-CAPA administrator '.
  927:                   'regarding this situation.</p></body></html>');
  928:         return;
  929:     }
  930:     # determine the students starting and ending times and section
  931:     my ($starttime,$endtime,$section) = &get_enrollment_data($sname,$sdom);
  932:     # Deal with date forms
  933:     my $startdateform = &Apache::lonhtmlcommon::date_setter('studentform',
  934:                                                             'startdate',
  935:                                                             $starttime);
  936:     my $enddateform = &Apache::lonhtmlcommon::date_setter('studentform',
  937:                                                           'enddate',
  938:                                                           $endtime);
  939:     # Make sure student is enrolled in course    
  940:     $r->print(<<END);
  941: <input type="hidden" name="slogin"  value="$sname"  />
  942: <input type="hidden" name="sdomain" value="$sdom" />
  943: <input type="hidden" name="action"  value="modifystudent" />
  944: <input type="hidden" name="state"  value="done" />
  945: <h2>Modify Enrollment for $info{'firstname'} $info{'middlename'} 
  946: $info{'lastname'} $info{'generation'}, $sname\@$sdom</h2>
  947: <p>
  948: <b>Student Name</b>
  949: <table>
  950: <tr><th>First</th><th>Middle</th><th>Last</th><th>Generation</th></tr>
  951: <tr><td>
  952: <input type="text" name="firstname"  value="$info{'firstname'}"  /></td><td>
  953: <input type="text" name="middlename" value="$info{'middlename'}" /></td><td>
  954: <input type="text" name="lastname"   value="$info{'lastname'}"   /></td><td>
  955: <input type="text" name="generation" value="$info{'generation'}" /></td></tr>
  956: </table>
  957: </p><p>
  958: <b>Section</b>: <input type="text" name="section" value="$section" size="4"/>
  959: </p><p>
  960: <table>
  961: <tr><td align="right"><b>Starting Date:</b></td><td>$startdateform</td></tr>
  962: <tr><td align="right"><b>Ending Date:</b></td><td>$enddateform</td></tr>
  963: </table>
  964: </p>
  965: <input type="submit" value="Submit Modifications" />
  966: </body></html>
  967: END
  968:     return;
  969: }
  970: 
  971: #
  972: # modify a single students section 
  973: #
  974: sub modify_single_student {
  975:     my $r = shift;
  976:     # make sure user can modify student data?
  977:     my $firstname  = $ENV{'form.firstname'};
  978:     my $middlename = $ENV{'form.middlename'};
  979:     my $lastname   = $ENV{'form.lastname'};
  980:     my $generation = $ENV{'form.generation'};
  981:     my $section    = $ENV{'form.section'};
  982:     my $courseid   = $ENV{'request.course.id'};
  983:     my $sname      = $ENV{'form.slogin'};
  984:     my $sdom       = $ENV{'form.sdomain'};
  985:     my $starttime = &Apache::lonhtmlcommon::get_date_from_form('startdate',
  986:                                                                time);
  987:     my $endtime   = &Apache::lonhtmlcommon::get_date_from_form('enddate',
  988:                                                                time);
  989:     my $displayable_starttime = localtime($starttime);
  990:     my $displayable_endtime   = localtime($endtime);
  991:     # talk to the user about what we are going to do
  992:     $r->print(<<END);
  993: Modifying data for user: $sname \@ $sdom <br />
  994: <h3>Student Information</h3>
  995: <table>
  996: <tr><td>First name  </td><td> $firstname  </td></tr>
  997: <tr><td>Middle name </td><td> $middlename </td></tr>
  998: <tr><td>Last name   </td><td> $lastname   </td></tr>
  999: <tr><td>Generation  </td><td> $generation </td></tr>
 1000: </table>
 1001: <h3>Role Information</h3>
 1002: <table>
 1003: <tr><td>Section     </td><td> $section    </td></tr>
 1004: <tr><td>Start Time  </td><td> $displayable_starttime </td></tr>
 1005: <tr><td>End Time    </td><td> $displayable_endtime   </td></tr>
 1006: </table>
 1007: END
 1008:     # send request(s) to modify data
 1009:     my $roleresults = 'refused';
 1010:     #my $roleresults = &Apache::lonnet::assignrole($sdom,$sname,
 1011:     #                                              $courseid.'/'.$section,
 1012:     #                                              'st',
 1013:     #                                              $endtime,$starttime);
 1014:     if ($roleresults =~/refused/) {
 1015:         $r->print("Your request to change the role information for this ".
 1016:                   "student was refused.");
 1017:     } elsif ($roleresults !~ /ok/) {
 1018:         $r->print("An error occurred during the attempt to change the role".
 1019:                   " information for this student.  The error reported was ".
 1020:                   $roleresults);
 1021:     } else { # everything is okay!
 1022:         $r->print("Student role updated successfully.");
 1023:     }
 1024:     # 
 1025:     $r->print(<<END);
 1026: </body></html>
 1027: END
 1028:     return;
 1029: }
 1030: 
 1031: sub get_enrollment_data {
 1032:     my ($sname,$sdomain) = @_;
 1033:     my $courseid = $ENV{'request.course.id'};
 1034:     $courseid =~ s:_:/:g;
 1035:     my %roles = &Apache::lonnet::dump('roles',$sdomain,$sname);
 1036:     my ($tmp) = keys(%roles);
 1037:     # Bail out if we were unable to get the students roles
 1038:     return "666" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
 1039:     # Go through the roles looking for enrollment in this course
 1040:     my ($end,$start) = (undef,undef);
 1041:     my $section = '';
 1042:     my $count = scalar(keys(%roles));
 1043:     while (my ($course,$role) = each(%roles)) {
 1044:         &Apache::lonnet::logthis('course = '.$course.' role = '.$role);
 1045:         if ($course=~ /^\/$courseid\/*\s*(\w+)*_st$/ ) {
 1046:             #
 1047:             # Get active role
 1048:             $section=$1;
 1049:             (undef,$end,$start)=split(/\_/,$role);
 1050:             my $now=time;
 1051:             my $notactive=0;
 1052:             if ($start) {
 1053:                 if ($now<$start) { $notactive=1; }
 1054:             }
 1055:             if ($end) {
 1056:                 if ($now>$end) { $notactive=1; }
 1057:             } 
 1058:             unless ($notactive) { return ($start,$end,$section); }
 1059:         }
 1060:     }
 1061:     return ($start,$end,$section);
 1062: }
 1063: 
 1064: # =================================================== Show student list to drop
 1065: sub show_drop_list {
 1066:     my ($r,%currentlist)=@_;
 1067:     my $cid=$ENV{'request.course.id'};
 1068:     $r->print(<<'END');
 1069: <input type="hidden" name="action" value="drop" />
 1070: <input type="hidden" name="state"  value="done" />
 1071: <script>
 1072: function checkAll(field)
 1073: {
 1074:     for (i = 0; i < field.length; i++)
 1075:         field[i].checked = true ;
 1076: }
 1077: 
 1078: function uncheckAll(field)
 1079: {
 1080:     for (i = 0; i < field.length; i++)
 1081:         field[i].checked = false ;
 1082: }
 1083: </script>
 1084: <p>
 1085: <input type="hidden" name="phase" value="four">
 1086: <table border=2>
 1087: <tr><th>&nbsp;</th><th>username</th><th>domain</th>
 1088: <th>ID</th><th>student name</th><th>generation</th>
 1089: <th>section</th></tr>
 1090: END
 1091:     foreach (sort keys %currentlist) {
 1092:         my ($sname,$sdom)=split(/\:/,$_);
 1093:         my %reply=&Apache::lonnet::idrget($sdom,$sname);
 1094:         my $ssec=&Apache::lonnet::usection($sdom,$sname,$cid);
 1095:         my %info=&Apache::lonnet::get('environment',
 1096:                                       ['firstname','middlename',
 1097:                                        'lastname','generation'],
 1098:                                       $sdom, $sname);
 1099:         my ($tmp) = keys(%info);
 1100:         if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
 1101:             $r->print('<tr><td colspan="7"><font color="red">'.
 1102:                       'Internal error: unable to get environment '.
 1103:                       'for '.$sname.' in domain '.$sdom.'</font></td></tr>');
 1104:         } else {
 1105:             $r->print(<<"END");
 1106: <tr>
 1107:     <td><input type="checkbox" name="droplist" value="$_"></td>
 1108:     <td>$sname</td>
 1109:     <td>$sdom</td>
 1110:     <td>$reply{$sname}</td>
 1111:     <td>$info{'lastname'}, $info{'firstname'} $info{'middlename'}</td>
 1112:     <td>$info{'generation'}</td>
 1113:     <td>$ssec</td>
 1114: </tr>
 1115: END
 1116:         }
 1117:     }
 1118:     $r->print('</table><br>');
 1119:     $r->print(<<"END");
 1120: </p><p>
 1121: <input type="button" value="check all" onclick="javascript:checkAll(document.studentform.droplist)"> &nbsp;
 1122: <input type="button" value="uncheck all" onclick="javascript:uncheckAll(document.studentform.droplist)"> 
 1123: <p><input type=submit value="Drop Students"></p>
 1124: END
 1125: }
 1126: 
 1127: #
 1128: # Print out the initial form to get the courselist file
 1129: #
 1130: sub print_first_courselist_upload_form {
 1131:     my $r=shift;
 1132:     my $upfile_select=&Apache::loncommon::upfile_select_html();
 1133:     my $create_classlist_help = 
 1134: 	&Apache::loncommon::help_open_topic("Course_Create_Class_List",
 1135:            "How do I create a class list from a spreadsheet");
 1136:     my $create_csv_help =
 1137: 	&Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
 1138:            "How do I create a CSV file from a spreadsheet");
 1139:     $r->print(<<ENDUPFORM);
 1140: <input type=hidden name=phase value=two>
 1141: <h3>Upload a courselist</h3>
 1142: $upfile_select
 1143: <p>
 1144: <input type=submit name="fileupload" value="Upload Courselist">
 1145: <input type="hidden" name="action" value="upload" />
 1146: <input type="hidden" name="state"  value="got_file" />
 1147: </p>
 1148: $create_classlist_help <br />
 1149: $create_csv_help
 1150: </body></html>
 1151: ENDUPFORM
 1152:     return;
 1153: }
 1154: 
 1155: # ================================================= Drop/Add from uploaded file
 1156: sub upfile_drop_add {
 1157:     my $r=shift;
 1158:     &Apache::loncommon::load_tmp_file($r);
 1159:     my @studentdata=&Apache::loncommon::upfile_record_sep();
 1160:     my @keyfields = split(/\,/,$ENV{'form.keyfields'});
 1161:     my $cid = $ENV{'request.course.id'};
 1162:     my %fields=();
 1163:     for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
 1164:         if ($ENV{'form.upfile_associate'} eq 'reverse') {
 1165:             if ($ENV{'form.f'.$i} ne 'none') {
 1166:                 $fields{$keyfields[$i]}=$ENV{'form.f'.$i};
 1167:             }
 1168:         } else {
 1169:             $fields{$ENV{'form.f'.$i}}=$keyfields[$i];
 1170:         }
 1171:     }
 1172:     #
 1173:     my $startdate = $ENV{'form.startdate'};
 1174:     my $enddate   = $ENV{'form.enddate'};
 1175:     if ($startdate=~/\D/) { $startdate=''; }
 1176:     if ($enddate=~/\D/)   { $enddate=''; }
 1177:     # Determine domain and desired host (home server)
 1178:     my $domain=$ENV{'form.lcdomain'};
 1179:     my $desiredhost = $ENV{'form.lcserver'};
 1180:     if (lc($desiredhost) eq 'default') {
 1181:         $desiredhost = undef;
 1182:     } else {
 1183:         my %home_servers = &Apache::loncommon::get_library_servers($domain);
 1184:         if (! exists($home_servers{$desiredhost})) {
 1185:             $r->print('<font color="#ff0000">Error:</font>'.
 1186:                       'Invalid home server specified');
 1187:             return;
 1188:         }
 1189:     }
 1190:     # Determine authentication mechanism
 1191:     my $amode  = '';
 1192:     my $genpwd = '';
 1193:     if ($ENV{'form.login'} eq 'krb') {
 1194:         $amode='krb';
 1195: 	$amode.=$ENV{'form.krbver'};
 1196:         $genpwd=$ENV{'form.krbarg'};
 1197:     } elsif ($ENV{'form.login'} eq 'int') {
 1198:         $amode='internal';
 1199:         if ((defined($ENV{'form.intarg'})) && ($ENV{'form.intarg'})) {
 1200:             $genpwd=$ENV{'form.intarg'};
 1201:         }
 1202:     } elsif ($ENV{'form.login'} eq 'loc') {
 1203:         $amode='localauth';
 1204:         if ((defined($ENV{'form.locarg'})) && ($ENV{'form.locarg'})) {
 1205:             $genpwd=$ENV{'form.locarg'};
 1206:         }
 1207:     }
 1208:     unless (($domain=~/\W/) || ($amode eq '')) {
 1209:         #######################################
 1210:         ##         Enroll Students           ##
 1211:         #######################################
 1212:         $r->print('<h3>Enrolling Students</h3>');
 1213:         my $count=0;
 1214:         my $flushc=0;
 1215:         my %student=();
 1216:         # Get new classlist
 1217:         foreach (@studentdata) {
 1218:             my %entries=&Apache::loncommon::record_sep($_);
 1219:             # Determine student name
 1220:             unless (($entries{$fields{'username'}} eq '') ||
 1221:                     (!defined($entries{$fields{'username'}}))) {
 1222:                 my ($fname, $mname, $lname,$gen) = ('','','','');
 1223:                 if (defined($fields{'names'})) {
 1224:                     ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
 1225:                                             /([^\,]+)\,\s*(\w+)\s*(.*)$/);
 1226:                 } else {
 1227:                     if (defined($fields{'fname'})) {
 1228:                         $fname=$entries{$fields{'fname'}};
 1229:                     }
 1230:                     if (defined($fields{'mname'})) {
 1231:                         $mname=$entries{$fields{'mname'}};
 1232:                     }
 1233:                     if (defined($fields{'lname'})) {
 1234:                         $lname=$entries{$fields{'lname'}};
 1235:                     }
 1236:                     if (defined($fields{'gen'})) {
 1237:                         $gen=$entries{$fields{'gen'}};
 1238:                     }
 1239:                 }
 1240:                 if ($entries{$fields{'username'}}=~/\W/) {
 1241:                     $r->print('<p><b>Unacceptable username: '.
 1242:                               $entries{$fields{'username'}}.' for user '.
 1243:                               $fname.' '.$mname.' '.$lname.' '.$gen.'</b><p>');
 1244:                 } else {
 1245:                     # determine section number
 1246:                     my $sec='';
 1247:                     my $username=$entries{$fields{'username'}};
 1248:                     if (defined($fields{'sec'})) {
 1249:                         if (defined($entries{$fields{'sec'}})) {
 1250:                             $sec=$entries{$fields{'sec'}};
 1251:                         }
 1252:                     }
 1253:                     # determine student id number
 1254:                     my $id='';
 1255:                     if (defined($fields{'id'})) {
 1256:                         if (defined($entries{$fields{'id'}})) {
 1257:                             $id=$entries{$fields{'id'}};
 1258:                         }
 1259:                         $id=~tr/A-Z/a-z/;
 1260:                     }
 1261:                     # determine student password
 1262:                     my $password='';
 1263:                     if ($genpwd) { 
 1264:                         $password=$genpwd; 
 1265:                     } else {
 1266:                         if (defined($fields{'ipwd'})) {
 1267:                             if ($entries{$fields{'ipwd'}}) {
 1268:                                 $password=$entries{$fields{'ipwd'}};
 1269:                             }
 1270:                         }
 1271:                     }
 1272:                     if ($password) {
 1273:                         &modifystudent($domain,$username,$cid,$sec,
 1274:                                        $desiredhost);
 1275:                         my $reply=&Apache::lonnet::modifystudent
 1276:                             ($domain,$username,$id,$amode,$password,
 1277:                              $fname,$mname,$lname,$gen,$sec,$enddate,
 1278:                              $startdate,$ENV{'form.forceid'},$desiredhost);
 1279:                         if ($reply ne 'ok') {
 1280:                             $r->print('<p><b>'.
 1281:                                       'Error enrolling '.$username.': '.
 1282:                                       $reply.'</b></p>');
 1283:          		} else {
 1284:                             $count++; $flushc++;
 1285:                             $student{$username}=1;
 1286:                             $r->print('. ');
 1287:                             if ($flushc>15) {
 1288: 				$r->rflush;
 1289:                                 $flushc=0;
 1290:                             }
 1291:                         }
 1292:                     } else {
 1293:                         $r->print("<p><b>No password for $username</b><p>");
 1294:                     }
 1295:                 }
 1296:             }
 1297:         } # end of foreach (@studentdata)
 1298:         $r->print('<p>Processed Students: '.$count);
 1299:         #####################################
 1300:         #           Drop students           #
 1301:         #####################################
 1302:         if ($ENV{'form.fullup'} eq 'yes') {
 1303:             $r->print('<h3>Dropping Students</h3>');
 1304:             #  Get current classlist
 1305:             my ($error,%currentlist)=&get_current_classlist
 1306:                 ($ENV{'course.'.$cid.'.domain'},
 1307:                  $ENV{'course.'.$cid.'.num'});
 1308:             if (defined($error)) {
 1309:                 $r->print('<pre>ERROR:$error</pre>');
 1310:             }
 1311:             if (defined(%currentlist)) {
 1312:                 # Drop the students
 1313:                 foreach (@studentdata) {
 1314:                     my %entries=&Apache::loncommon::record_sep($_);
 1315:                     unless (($entries{$fields{'username'}} eq '') ||
 1316:                             (!defined($entries{$fields{'username'}}))) {
 1317:                         delete($currentlist{$entries{$fields{'username'}}.
 1318:                                                 ':'.$domain});
 1319:                     }
 1320:                 }
 1321:                 # Print out list of dropped students
 1322:                 &show_drop_list($r,%currentlist);
 1323:             } else {
 1324:                 $r->print("There are no students currently enrolled.\n");
 1325:             }
 1326:         }
 1327:     } # end of unless
 1328: }
 1329: 
 1330: ###################################################################
 1331: ###################################################################
 1332: 
 1333: =pod
 1334: 
 1335: =item &drop_students
 1336: 
 1337: Inputs: \@droplist, a pointer to an array of students to drop.
 1338: Students should be in format of studentname:studentdomain
 1339: 
 1340: Returns: $errors, a string describing any errors encountered.
 1341: $successes, a string describing the successful dropping of students.
 1342: 
 1343: =cut
 1344: 
 1345: ###################################################################
 1346: ###################################################################
 1347: sub drop_students {
 1348:     my @droplist = @{shift()};
 1349:     my $courseid = $ENV{'request.course.id'};
 1350:     my $successes = '';
 1351:     my $errors = '';
 1352:     foreach (@droplist) {
 1353:         my ($sname,$sdom)=split(/:/,$_);
 1354:         my $result = &drop_student($sname,$sdom,$courseid);
 1355:         if ($result !~ /ok/) {
 1356:             $errors .= "Error dropping $sname\@$sdom: $result\n";
 1357:         } else {
 1358:             $successes .= "Dropped $sname\@$sdom\n";
 1359:         }
 1360:     }
 1361:     return ($errors,$successes);
 1362: }
 1363: ###################################################################
 1364: ###################################################################
 1365: 
 1366: 
 1367: # ================================================================== Phase four
 1368: sub drop_student_list {
 1369:     my $r=shift;
 1370:     my $count=0;
 1371:     my @droplist;
 1372:     if (ref($ENV{'form.droplist'})) {
 1373:         @droplist = @{$ENV{'form.droplist'}};
 1374:     } else {
 1375:         @droplist = ($ENV{'form.droplist'});
 1376:     }
 1377:     foreach (@droplist) {
 1378:         my ($uname,$udom)=split(/\:/,$_);
 1379:         my $result = &modifystudent($udom,$uname,$ENV{'request.course.id'});
 1380:         if ($result eq 'ok' || $result eq 'ok:') {
 1381:             $r->print('Dropped '.$uname.' at '.$udom.'<br>');
 1382:         } else {
 1383:             $r->print('Error dropping '.$uname.' at '.$udom.': '.$result.
 1384:                       '<br />');
 1385:         }
 1386:         $count++;
 1387:     }
 1388:     $r->print('<p><b>Dropped '.$count.' student(s).</b>');
 1389:     $r->print('<p>Re-enrollment will re-activate data.');
 1390: }
 1391: 
 1392: ###################################################################
 1393: ###################################################################
 1394: 
 1395: =pod
 1396: 
 1397: =item &handler
 1398: 
 1399: The typical handler you see in all these modules.  Takes $r, the
 1400: http request, as an argument.  
 1401: 
 1402: The response to the request is governed by two form variables
 1403: 
 1404:  form.action      form.state     response
 1405:  ---------------------------------------------------
 1406:  undefined        undefined      print main menu
 1407:  upload           undefined      print courselist upload menu
 1408:  upload           got_file       deal with uploaded file,
 1409:                                  print the upload managing menu
 1410:  upload           enrolling      enroll students based on upload
 1411:  drop             undefined      print the classlist ready to drop
 1412:  drop             done           drop the selected students
 1413:  enrollstudent    undefined      print single student enroll menu
 1414:  enrollstudent    enrolling      enroll student
 1415:  classlist        undefined      print html classlist
 1416:  classlist        csv            print csv classlist
 1417:  modifystudent    undefined      print classlist to select student to modify
 1418:  modifystudent    selected       print modify student menu
 1419:  modifystudent    done           make modifications to student record
 1420: 
 1421: =cut
 1422: 
 1423: ###################################################################
 1424: ###################################################################
 1425: sub handler {
 1426:     my $r=shift;
 1427:     if ($r->header_only) {
 1428:         $r->content_type('text/html');
 1429:         $r->send_http_header;
 1430:         return OK;
 1431:     }
 1432:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1433:                                             ['action','state']);
 1434:     #  Needs to be in a course
 1435:     if (! (($ENV{'request.course.fn'}) &&
 1436:           (&Apache::lonnet::allowed('cst',$ENV{'request.course.id'})))) {
 1437:         # Not in a course, or not allowed to modify parms
 1438:         $ENV{'user.error.msg'}=
 1439:             "/adm/dropadd:cst:0:0:Cannot drop or add students";
 1440:         return HTTP_NOT_ACCEPTABLE; 
 1441:     }
 1442:     #
 1443:     # Only output the header information if they did not request csv format
 1444:     #
 1445:     if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) {
 1446:         $r->content_type('text/csv');
 1447:     } else {
 1448:         # Start page
 1449:         $r->content_type('text/html');
 1450:         $r->send_http_header;
 1451:         $r->print(&header());
 1452:     }
 1453:     #
 1454:     # Main switch on form.action and form.state, as appropriate
 1455:     if (! exists($ENV{'form.action'})) {
 1456:         &print_main_menu($r);
 1457:     } elsif ($ENV{'form.action'} eq 'upload') {
 1458:         if (! exists($ENV{'form.state'})) {
 1459:             &print_first_courselist_upload_form($r);            
 1460:         } elsif ($ENV{'form.state'} eq 'got_file') {
 1461:             &print_upload_manager_form($r);
 1462:         } elsif ($ENV{'form.state'} eq 'enrolling') {
 1463:             if ($ENV{'form.datatoken'}) {
 1464:                 &upfile_drop_add($r);
 1465:             } else {
 1466:                 # Hmmm, this is an error
 1467:             }
 1468:         } else {
 1469:             &print_first_courselist_upload_form($r);            
 1470:         }
 1471:     } elsif ($ENV{'form.action'} eq 'drop') {
 1472:         if (! exists($ENV{'form.state'})) {
 1473:             &menu_phase_two_drop($r);
 1474:         } elsif ($ENV{'form.state'} eq 'done') {
 1475:             &drop_student_list($r);
 1476:         } else {
 1477:             &menu_phase_two_drop($r);
 1478:         }
 1479:     } elsif ($ENV{'form.action'} eq 'enrollstudent') {
 1480:         if (! exists($ENV{'form.state'})) {
 1481:             &print_enroll_single_student_form($r);
 1482:         } elsif ($ENV{'form.state'} eq 'enrolling') {
 1483:             &enroll_single_student($r);
 1484:         } else {
 1485:             &print_enroll_single_student_form($r);
 1486:         }
 1487:     } elsif ($ENV{'form.action'} eq 'classlist') {
 1488:         if (! exists($ENV{'form.state'})) {
 1489:             &print_html_classlist($r);
 1490:         } elsif ($ENV{'form.state'} eq 'csv') {
 1491:             &print_csv_classlist($r);
 1492:         } else {
 1493:             &print_html_classlist($r);
 1494:         }
 1495:     } elsif ($ENV{'form.action'} eq 'modifystudent') {
 1496:         if (! exists($ENV{'form.state'})) {
 1497:             &print_html_classlist($r);
 1498:         } elsif ($ENV{'form.state'} eq 'selected') {
 1499:             &print_modify_student_form($r);
 1500:         } elsif ($ENV{'form.state'} eq 'done') {
 1501:             &modify_single_student($r);
 1502:         } else {
 1503:             &print_html_classlist($r);
 1504:         }        
 1505:     } else {
 1506:         # We should not end up here, but I guess it is possible
 1507:         &Apache::lonnet::logthis("Undetermined state in londropadd.pm.  ".
 1508:                                  "form.action = ".$ENV{'form.action'}.
 1509:                                  "Someone should fix this.");
 1510:         &print_main_menu($r);
 1511:     }
 1512:     #
 1513:     # Finish up
 1514:     if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) {
 1515:         $r->print("\n");
 1516:     } else {
 1517:         $r->print('</form></body></html>');
 1518:     }
 1519:     return OK;
 1520: }
 1521: 
 1522: ###################################################################
 1523: ###################################################################
 1524: 
 1525: 1;
 1526: __END__
 1527: 
 1528: 

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