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