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