Annotation of loncom/interface/lonuserutils.pm, revision 1.26
1.1 raeburn 1: # The LearningOnline Network with CAPA
2: # Utility functions for managing LON-CAPA user accounts
3: #
1.26 ! raeburn 4: # $Id: lonuserutils.pm,v 1.25 2007/12/22 04:23:05 raeburn Exp $
1.1 raeburn 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA#
23: # /home/httpd/html/adm/gpl.txt
24: #
25: # http://www.lon-capa.org/
26: #
27: #
28: ###############################################################
29: ###############################################################
30:
31: package Apache::lonuserutils;
32:
33: use strict;
34: use Apache::lonnet;
35: use Apache::loncommon();
36: use Apache::lonhtmlcommon;
37: use Apache::lonlocal;
1.8 raeburn 38: use Apache::longroup;
39: use LONCAPA qw(:DEFAULT :match);
1.1 raeburn 40:
41: ###############################################################
42: ###############################################################
43: # Drop student from all sections of a course, except optional $csec
44: sub modifystudent {
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
1.17 raeburn 49: my ($cnum,$cdom) = &get_course_identity($courseid);
1.1 raeburn 50: my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
51: my ($tmp) = keys(%roles);
52: # Bail out if we were unable to get the students roles
53: return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
54: # Go through the roles looking for enrollment in this course
55: my $result = '';
56: foreach my $course (keys(%roles)) {
57: if ($course=~m{^/\Q$cdom\E/\Q$cnum\E(?:\/)*(?:\s+)*(\w+)*\_st$}) {
58: # We are in this course
59: my $section=$1;
60: $section='' if ($course eq "/$cdom/$cnum".'_st');
61: if (defined($csec) && $section eq $csec) {
62: $result .= 'ok:';
63: } elsif ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
64: my (undef,$end,$start)=split(/\_/,$roles{$course});
65: my $now=time;
66: # if this is an active role
67: if (!($start && ($now<$start)) || !($end && ($now>$end))) {
68: my $reply=&Apache::lonnet::modifystudent
69: # dom name id mode pass f m l g
70: ($udom,$unam,'', '', '',undef,undef,undef,undef,
1.22 raeburn 71: $section,time,undef,undef,$desiredhost,'','manual',
72: '',$courseid);
1.1 raeburn 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,
1.5 raeburn 89: $end,$start,$checkid) = @_;
90: my ($scope,$userresult,$authresult,$roleresult,$idresult);
1.1 raeburn 91: if ($setting eq 'course' || $context eq 'course') {
92: $scope = '/'.$cid;
93: $scope =~ s/\_/\//g;
94: if ($role ne 'cc' && $sec ne '') {
95: $scope .='/'.$sec;
96: }
1.5 raeburn 97: } elsif ($context eq 'domain') {
1.1 raeburn 98: $scope = '/'.$env{'request.role.domain'}.'/';
1.13 raeburn 99: } elsif ($context eq 'author') {
1.1 raeburn 100: $scope = '/'.$env{'user.domain'}.'/'.$env{'user.name'};
101: }
102: if ($context eq 'domain') {
103: my $uhome = &Apache::lonnet::homeserver($uname,$udom);
104: if ($uhome ne 'no_host') {
1.5 raeburn 105: if (($changeauth eq 'Yes') && (&Apache::lonnet::allowed('mau',$udom))) {
1.1 raeburn 106: if ((($umode =~ /^krb4|krb5|internal$/) && $upass ne '') ||
107: ($umode eq 'localauth')) {
108: $authresult = &Apache::lonnet::modifyuserauth($udom,$uname,$umode,$upass);
109: }
110: }
1.5 raeburn 111: if (($forceid) && (&Apache::lonnet::allowed('mau',$udom)) &&
112: ($env{'form.recurseid'}) && ($checkid)) {
113: my %userupdate = (
114: lastname => $last,
115: middlename => $middle,
116: firstname => $first,
117: generation => $gene,
118: id => $uid,
119: );
120: $idresult = &propagate_id_change($uname,$udom,\%userupdate);
121: }
1.1 raeburn 122: }
123: }
124: $userresult =
125: &Apache::lonnet::modifyuser($udom,$uname,$uid,$umode,$upass,$first,
126: $middle,$last,$gene,$forceid,$desiredhome,
127: $email,$role,$start,$end);
128: if ($userresult eq 'ok') {
1.5 raeburn 129: if ($role ne '') {
1.22 raeburn 130: $role =~ s/_/\//g;
1.1 raeburn 131: $roleresult = &Apache::lonnet::assignrole($udom,$uname,$scope,
132: $role,$end,$start);
133: }
134: }
1.5 raeburn 135: return ($userresult,$authresult,$roleresult,$idresult);
1.1 raeburn 136: }
137:
1.5 raeburn 138: sub propagate_id_change {
139: my ($uname,$udom,$user) = @_;
1.12 raeburn 140: my (@types,@roles);
1.5 raeburn 141: @types = ('active','future');
142: @roles = ('st');
143: my $idresult;
144: my %roleshash = &Apache::lonnet::get_my_roles($uname,
1.12 raeburn 145: $udom,'userroles',\@types,\@roles);
146: my %args = (
147: one_time => 1,
148: );
1.5 raeburn 149: foreach my $item (keys(%roleshash)) {
1.22 raeburn 150: my ($cnum,$cdom,$role) = split(/:/,$item,-1);
1.5 raeburn 151: my ($start,$end) = split(/:/,$roleshash{$item});
152: if (&Apache::lonnet::is_course($cdom,$cnum)) {
1.12 raeburn 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: }
1.5 raeburn 160: if ($result eq 'ok') {
1.12 raeburn 161: $idresult .= &mt('Classlist update for "[_1]" in "[_2]".',$uname.':'.$udom,$cdesc).'<br />'."\n";
1.5 raeburn 162: } else {
1.12 raeburn 163: $idresult .= &mt('Error: "[_1]" during classlist update for "[_2]" in "[_3]".',$result,$uname.':'.$udom,$cdesc).'<br />'."\n";
1.5 raeburn 164: }
165: }
166: }
167: return $idresult;
168: }
169:
170: sub update_classlist {
171: my ($cdom,$cnum,$udom,$uname,$user) = @_;
1.6 albertel 172: my ($uid,$classlistentry);
1.5 raeburn 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:
1.1 raeburn 207: ###############################################################
208: ###############################################################
1.2 raeburn 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
1.13 raeburn 215: my @roletypes = ('domain','author','course');
1.2 raeburn 216: my %lt = &role_type_names();
1.1 raeburn 217: #
218: # build up the menu information to be passed to
219: # &Apache::loncommon::linked_select_forms
220: my %select_menus;
1.2 raeburn 221: if ($env{'form.roletype'} eq '') {
222: $env{'form.roletype'} = 'domain';
223: }
224: foreach my $roletype (@roletypes) {
1.1 raeburn 225: # set up the text for this domain
1.2 raeburn 226: $select_menus{$roletype}->{'text'}= $lt{$roletype};
1.1 raeburn 227: # we want a choice of 'default' as the default in the second menu
1.2 raeburn 228: if ($env{'form.roletype'} ne '') {
229: $select_menus{$roletype}->{'default'} = $env{'form.showrole'};
230: } else {
231: $select_menus{$roletype}->{'default'} = 'Any';
232: }
1.1 raeburn 233: # Now build up the other items in the second menu
1.2 raeburn 234: my @roles;
235: if ($roletype eq 'domain') {
236: @roles = &domain_roles();
1.13 raeburn 237: } elsif ($roletype eq 'author') {
1.2 raeburn 238: @roles = &construction_space_roles();
239: } else {
1.17 raeburn 240: my $custom = 1;
241: @roles = &course_roles('domain',undef,$custom);
1.1 raeburn 242: }
1.2 raeburn 243: my $order = ['Any',@roles];
244: $select_menus{$roletype}->{'order'} = $order;
245: foreach my $role (@roles) {
1.5 raeburn 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: }
1.2 raeburn 253: }
254: $select_menus{$roletype}->{'select2'}->{'Any'} = &mt('Any');
1.1 raeburn 255: }
1.2 raeburn 256: my $result = &Apache::loncommon::linked_select_forms
257: ('studentform',(' 'x3).&mt('Role: '),$env{'form.roletype'},
1.13 raeburn 258: 'roletype','showrole',\%select_menus,['domain','author','course']);
1.1 raeburn 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 {
1.22 raeburn 270: my ($r,$datatoken,$distotal,$krbdefdom,$context,$permission)=@_;
1.1 raeburn 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: #
1.22 raeburn 300: my $groupslist;
301: if ($context eq 'course') {
302: $groupslist = &get_groupslist();
303: }
1.1 raeburn 304: my $javascript_validations =
1.22 raeburn 305: &javascript_validations('upload',$krbdefdom,$password_choice,undef,
306: $env{'request.role.domain'},$context,
307: $permission,$groupslist);
1.1 raeburn 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 {
1.22 raeburn 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';
1.1 raeburn 347: } elsif ($mode eq 'createcourse') {
1.22 raeburn 348: $param{'formname'} = 'ccrs';
1.1 raeburn 349: } elsif ($mode eq 'modifycourse') {
1.22 raeburn 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: }
1.1 raeburn 377: }
1.22 raeburn 378: my $authheader = &Apache::loncommon::authform_header(%param);
1.1 raeburn 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);
1.22 raeburn 394: $setsections_js
395:
1.1 raeburn 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';
1.22 raeburn 515: $finish
1.1 raeburn 516: }
517: } else {
518: vf.state.value='enrolling';
1.22 raeburn 519: $finish
1.1 raeburn 520: }
521: }
522: END
523: }
524: my $result = $function_name;
1.22 raeburn 525: if ( ($mode eq 'upload') || ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
1.1 raeburn 526: $result .= $auth_checks;
527: }
528: $result .= $optional_checks."\n".$section_checks;
1.22 raeburn 529: if ( ($mode eq 'upload') || ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
1.1 raeburn 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 {
1.22 raeburn 690: my ($r,$i,$keyfields,$defdom,$today,$halfyear,$context,$permission) = @_;
691: my $form = 'document.studentform';
692: my $formname = 'studentform';
1.1 raeburn 693: my ($krbdef,$krbdefdom) =
694: &Apache::loncommon::get_kerberos_defaults($defdom);
1.22 raeburn 695: my %param = ( formname => $form,
1.1 raeburn 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);
1.22 raeburn 707: my $date_table = &date_setting_table(undef,undef,$context,undef,
708: $formname,$permission);
1.1 raeburn 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: }
1.13 raeburn 739: if ($context eq 'author') {
1.1 raeburn 740: $Str .= '<h3>'.&mt('Default role')."</h3>\n".
1.25 raeburn 741: &mt('Choose the role to assign to users without a value specified in the uploaded file');
1.1 raeburn 742: } elsif ($context eq 'course') {
743: $Str .= '<h3>'.&mt('Default role and section')."</h3>\n".
1.25 raeburn 744: &mt('Choose the role and/or section(s) to assign to users without values specified in the uploaded file');
1.1 raeburn 745: } else {
1.25 raeburn 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.');
1.1 raeburn 748: }
1.22 raeburn 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: }
1.1 raeburn 757: } else {
1.22 raeburn 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.');
1.1 raeburn 765: "</label></p>\n";
766: }
1.5 raeburn 767: if ($context eq 'course' || $context eq 'domain') {
768: $Str .= &forceid_change($context);
769: }
1.1 raeburn 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:
1.5 raeburn 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".
1.25 raeburn 789: &mt('(only do if you know what you are doing.)')."\n";
1.5 raeburn 790: if ($context eq 'domain') {
1.25 raeburn 791: $output .= '<br /><label><input type="checkbox" name="recurseid"'.
1.5 raeburn 792: ' value="yes">'.
1.13 raeburn 793: &mt('Update ID/Student Number in courses in which user is Active/Future student,<br />(if forcing change).').
1.25 raeburn 794: '</label>'."\n";
1.5 raeburn 795: }
1.25 raeburn 796: $output .= '</p>';
1.5 raeburn 797: return $output;
798: }
799:
1.1 raeburn 800: ###############################################################
801: ###############################################################
802: sub print_upload_manager_form {
1.21 raeburn 803: my ($r,$context,$permission) = @_;
1.1 raeburn 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: #
1.22 raeburn 847: &print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom,$context,
848: $permission);
1.1 raeburn 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,
1.22 raeburn 882: $context,$permission);
1.1 raeburn 883: }
884:
885: sub setup_date_selectors {
1.22 raeburn 886: my ($starttime,$endtime,$mode,$nolink,$formname) = @_;
887: if ($formname eq '') {
888: $formname = 'studentform';
889: }
1.1 raeburn 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: }
1.11 raeburn 910:
911: my $startdateform =
1.22 raeburn 912: &Apache::lonhtmlcommon::date_setter($formname,'startdate',$starttime,
1.11 raeburn 913: undef,undef,undef,undef,undef,undef,undef,$nolink);
914:
915: my $enddateform =
1.22 raeburn 916: &Apache::lonhtmlcommon::date_setter($formname,'enddate',$endtime,
1.11 raeburn 917: undef,undef,undef,undef,undef,undef,undef,$nolink);
918:
1.1 raeburn 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 {
1.22 raeburn 949: my ($starttime,$endtime,$mode,$bulkaction,$formname,$permission) = @_;
1.11 raeburn 950: my $nolink;
951: if ($bulkaction) {
952: $nolink = 1;
953: }
954: my ($startform,$endform) =
1.22 raeburn 955: &setup_date_selectors($starttime,$endtime,$mode,$nolink,$formname);
1.1 raeburn 956: my $dateDefault;
957: if ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
958: $dateDefault = ' ';
1.13 raeburn 959: } elsif ($mode ne 'author' && $mode ne 'domain') {
1.11 raeburn 960: if (($bulkaction eq 'reenable') ||
961: ($bulkaction eq 'activate') ||
1.22 raeburn 962: ($bulkaction eq 'chgdates') ||
963: ($env{'form.action'} eq 'upload')) {
964: if ($env{'request.course.sec'} eq '') {
965: $dateDefault = '<span class="LC_nobreak">'.
1.25 raeburn 966: '<label><input type="checkbox" name="makedatesdefault" value="1" /> '.
1.22 raeburn 967: &mt('make these dates the default access dates for future student enrollment').
968: '</label></span>';
969: }
1.11 raeburn 970: }
1.1 raeburn 971: }
1.11 raeburn 972: my $perpetual = '<span class="LC_nobreak"><label><input type="checkbox" name="no_end_date"';
1.1 raeburn 973: if (defined($endtime) && $endtime == 0) {
974: $perpetual .= ' checked';
975: }
1.11 raeburn 976: $perpetual.= ' /> '.&mt('no ending date').'</label></span>';
1.1 raeburn 977: if ($mode eq 'create_enrolldates') {
978: $perpetual = ' ';
979: }
1.11 raeburn 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).
1.22 raeburn 989: &Apache::lonhtmlcommon::end_pick_box();
1.1 raeburn 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') {
1.17 raeburn 1000: my ($cnum,$cdom) = &get_course_identity();
1.1 raeburn 1001: my $put_result = &Apache::lonnet::put('environment',
1002: {'default_enrollment_start_date'=>$startdate,
1.17 raeburn 1003: 'default_enrollment_end_date' =>$enddate},$cdom,$cnum);
1.1 raeburn 1004: if ($put_result eq 'ok') {
1.25 raeburn 1005: $result .= &mt('Set default start and end access dates for course.').
1.11 raeburn 1006: '<br />'."\n";
1.1 raeburn 1007: #
1008: # Refresh the course environment
1009: &Apache::lonnet::coursedescription($env{'request.course.id'},
1010: {'freshen_cache' => 1});
1011: } else {
1.25 raeburn 1012: $result .= &mt('Unable to set default access dates for course.').":".$put_result.
1.1 raeburn 1013: '<br />';
1014: }
1015: }
1016: return $result;
1017: }
1018:
1019: sub default_role_selector {
1.2 raeburn 1020: my ($context,$checkpriv) = @_;
1.1 raeburn 1021: my %customroles;
1022: my ($options,$coursepick,$cb_jscript);
1.13 raeburn 1023: if ($context ne 'author') {
1.1 raeburn 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') {
1.2 raeburn 1036: $options .= &default_course_roles($context,$checkpriv,%customroles);
1.13 raeburn 1037: } elsif ($context eq 'author') {
1.2 raeburn 1038: my @roles = &construction_space_roles($checkpriv);
1.1 raeburn 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') {
1.2 raeburn 1044: my @roles = &domain_roles($checkpriv);
1.1 raeburn 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
1.22 raeburn 1050: ('studentform','dccourse','dcdomain','coursedesc',"$env{'request.role.domain'}",undef,'Course');
1.1 raeburn 1051: $cb_jscript =
1.22 raeburn 1052: &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'},'currsec','studentform');
1.1 raeburn 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".
1.22 raeburn 1059: '<td><input type="text" name="coursedesc" value="" onFocus="this.blur();opencrsbrowser('."'studentform','dccourse','dcdomain','coursedesc',''".')" /></td>'."\n".
1.1 raeburn 1060: '<td><select name="courserole">'."\n".
1.2 raeburn 1061: &default_course_roles($context,$checkpriv,%customroles)."\n".
1.1 raeburn 1062: '</select></td><td>'.
1063: '<table class="LC_createuser">'.
1064: '<tr class="LC_section_row"><td valign"top">'.
1.22 raeburn 1065: $lt{'exs'}.'<br /><select name="currsec">'.
1.1 raeburn 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" />'.
1.22 raeburn 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>'.
1.1 raeburn 1078: &Apache::loncommon::end_data_table_row().
1.22 raeburn 1079: &Apache::loncommon::end_data_table()."\n";
1.1 raeburn 1080: }
1081: $options .= '</select>';
1082: return ($options,$cb_jscript,$coursepick);
1083: }
1084:
1085: sub default_course_roles {
1.2 raeburn 1086: my ($context,$checkpriv,%customroles) = @_;
1.1 raeburn 1087: my $output;
1.17 raeburn 1088: my $custom = 1;
1089: my @roles = &course_roles($context,$checkpriv,$custom);
1.1 raeburn 1090: foreach my $role (@roles) {
1.22 raeburn 1091: if ($role ne 'cr') {
1092: my $plrole=&Apache::lonnet::plaintext($role);
1093: $output .= ' <option value="'.$role.'">'.$plrole.'</option>';
1094: }
1.1 raeburn 1095: }
1096: if (keys(%customroles) > 0) {
1.22 raeburn 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: }
1.1 raeburn 1103: }
1104: }
1105: return $output;
1106: }
1107:
1108: sub construction_space_roles {
1.2 raeburn 1109: my ($checkpriv) = @_;
1.17 raeburn 1110: my @allroles = &roles_by_context('author');
1.1 raeburn 1111: my @roles;
1.2 raeburn 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: }
1.1 raeburn 1117: }
1.2 raeburn 1118: return @roles;
1119: } else {
1120: return @allroles;
1.1 raeburn 1121: }
1122: }
1123:
1124: sub domain_roles {
1.2 raeburn 1125: my ($checkpriv) = @_;
1.17 raeburn 1126: my @allroles = &roles_by_context('domain');
1.1 raeburn 1127: my @roles;
1.2 raeburn 1128: if ($checkpriv) {
1129: foreach my $role (@allroles) {
1130: if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) {
1131: push(@roles,$role);
1132: }
1.1 raeburn 1133: }
1.2 raeburn 1134: return @roles;
1135: } else {
1136: return @allroles;
1.1 raeburn 1137: }
1138: }
1139:
1140: sub course_roles {
1.17 raeburn 1141: my ($context,$checkpriv,$custom) = @_;
1142: my @allroles = &roles_by_context('course',$custom);
1.1 raeburn 1143: my @roles;
1144: if ($context eq 'domain') {
1145: @roles = @allroles;
1146: } elsif ($context eq 'course') {
1147: if ($env{'request.course.id'}) {
1.2 raeburn 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 {
1.22 raeburn 1153: if ($role ne 'cc' && $env{'request.course.sec'} ne '') {
1154: if (&Apache::lonnet::allowed('c'.$role,
1.2 raeburn 1155: $env{'request.course.id'}.'/'.
1.22 raeburn 1156: $env{'request.course.sec'})) {
1.2 raeburn 1157: push(@roles,$role);
1158: }
1.1 raeburn 1159: }
1160: }
1161: }
1.2 raeburn 1162: } else {
1163: @roles = @allroles;
1.1 raeburn 1164: }
1165: }
1166: }
1167: return @roles;
1168: }
1169:
1170: sub curr_role_permissions {
1.2 raeburn 1171: my ($context,$setting,$checkpriv) = @_;
1.17 raeburn 1172: my $custom = 1;
1.1 raeburn 1173: my @roles;
1.13 raeburn 1174: if ($context eq 'author') {
1.2 raeburn 1175: @roles = &construction_space_roles($checkpriv);
1.1 raeburn 1176: } elsif ($context eq 'domain') {
1177: if ($setting eq 'course') {
1.17 raeburn 1178: @roles = &course_roles($context,$checkpriv,$custom);
1.1 raeburn 1179: } else {
1.2 raeburn 1180: @roles = &domain_roles($checkpriv);
1.1 raeburn 1181: }
1182: } elsif ($context eq 'course') {
1.17 raeburn 1183: @roles = &course_roles($context,$checkpriv,$custom);
1.1 raeburn 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:
1.2 raeburn 1201: sub print_userlist {
1202: my ($r,$mode,$permission,$context,$formname,$totcodes,$codetitles,
1203: $idlist,$idlist_titles) = @_;
1204: my $format = $env{'form.output'};
1.1 raeburn 1205: if (! exists($env{'form.sortby'})) {
1206: $env{'form.sortby'} = 'username';
1207: }
1.2 raeburn 1208: if ($env{'form.Status'} !~ /^(Any|Expired|Active|Future)$/) {
1209: $env{'form.Status'} = 'Active';
1.1 raeburn 1210: }
1211: my $status_select = &Apache::lonhtmlcommon::StatusOptions
1.2 raeburn 1212: ($env{'form.Status'});
1.1 raeburn 1213:
1.2 raeburn 1214: if ($env{'form.showrole'} eq '') {
1.13 raeburn 1215: if ($context eq 'course') {
1216: $env{'form.showrole'} = 'st';
1217: } else {
1218: $env{'form.showrole'} = 'Any';
1219: }
1.2 raeburn 1220: }
1.1 raeburn 1221: if (! defined($env{'form.output'}) ||
1222: $env{'form.output'} !~ /^(csv|excel|html)$/ ) {
1223: $env{'form.output'} = 'html';
1224: }
1225:
1.2 raeburn 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: }
1.1 raeburn 1236:
1.2 raeburn 1237: # if ($context eq 'course') {
1238: # $r->print(&display_adv_courseroles());
1239: # }
1.1 raeburn 1240: #
1241: # Interface output
1.2 raeburn 1242: $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n".
1243: '<input type="hidden" name="action" value="'.
1.1 raeburn 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: }
1.2 raeburn 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: }
1.22 raeburn 1281: my $plrole;
1282: if ($role eq 'cr') {
1283: $plrole = &mt('Custom role');
1284: } else {
1285: $plrole=&Apache::lonnet::plaintext($role);
1286: }
1.2 raeburn 1287: $role_select .= '<option value="'.$role.'"'.$roleselected.'>'.$plrole.'</option>';
1288: }
1.22 raeburn 1289: $role_select .= '</select>';
1.2 raeburn 1290: $r->print('<label>'.&mt('Role: [_1]',$role_select).'</label>');
1291: }
1292: if (!(($context eq 'domain') && ($env{'form.roletype'} eq 'course'))) {
1.25 raeburn 1293: $r->print(' '.&list_submit_button(&mt('Update Display')).
1294: "\n</p>\n");
1.2 raeburn 1295: }
1296: my ($indexhash,$keylist) = &make_keylist_array();
1297: my (%userlist,%userinfo);
1.3 raeburn 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");
1.11 raeburn 1311: if ($env{'form.coursepick'}) {
1312: $r->print('<hr />'.&mt('Searching').' ...<br /> <br />');
1313: }
1314: } else {
1315: $r->print('<hr />'.&mt('Searching').' ...<br /> <br />');
1.3 raeburn 1316: }
1317: $r->rflush();
1.1 raeburn 1318: if ($context eq 'course') {
1.3 raeburn 1319: my $classlist = &Apache::loncoursedata::get_classlist();
1320: my $secidx = &Apache::loncoursedata::CL_SECTION();
1.22 raeburn 1321: my $viewablesec = &viewable_section($permission);
1.3 raeburn 1322: foreach my $student (keys(%{$classlist})) {
1.22 raeburn 1323: my $section = $classlist->{$student}[$secidx];
1324: if ($viewablesec ne '') {
1325: if ($section ne $viewablesec) {
1.3 raeburn 1326: next;
1327: } else {
1328: $userlist{$student} = $classlist->{$student};
1.1 raeburn 1329: }
1.3 raeburn 1330: } else {
1331: $userlist{$student} = $classlist->{$student};
1.1 raeburn 1332: }
1333: }
1.22 raeburn 1334: my $cid = $env{'request.course.id'};
1.17 raeburn 1335: my ($cnum,$cdom) = &get_course_identity($cid);
1.2 raeburn 1336: my $showroles;
1337: if ($env{'form.showrole'} ne 'Any') {
1338: $showroles = [$env{'form.showrole'}];
1.1 raeburn 1339: } else {
1.2 raeburn 1340: $showroles = undef;
1341: }
1.22 raeburn 1342: my $withsec = 1;
1.2 raeburn 1343: my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
1.22 raeburn 1344: \@statuses,$showroles,undef,$withsec);
1.2 raeburn 1345: &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
1.11 raeburn 1346: \%advrolehash,$permission);
1.2 raeburn 1347: } else {
1348: my (%cstr_roles,%dom_roles);
1.13 raeburn 1349: if ($context eq 'author') {
1.2 raeburn 1350: # List co-authors and assistant co-authors
1.17 raeburn 1351: my @possroles = &roles_by_context($context);
1.2 raeburn 1352: %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
1353: \@statuses,\@possroles);
1354: &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
1.11 raeburn 1355: \%cstr_roles,$permission);
1.2 raeburn 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,
1.11 raeburn 1362: \%userinfo,$dom_roles{$key},$permission);
1.2 raeburn 1363: }
1364: }
1.13 raeburn 1365: } elsif ($env{'form.roletype'} eq 'author') {
1.2 raeburn 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,
1.11 raeburn 1372: \%userinfo,$dom_roles{$key},$permission);
1.2 raeburn 1373: } else {
1374: my @possroles;
1375: if ($env{'form.showrole'} eq 'Any') {
1.22 raeburn 1376: @possroles = &roles_by_context('author');
1.2 raeburn 1377: } else {
1378: @possroles = ($env{'form.showrole'});
1379: }
1380: foreach my $author (sort(keys(%{$dom_roles{$key}}))) {
1.22 raeburn 1381: my ($role,$authorname,$authordom) = split(/:/,$author,-1);
1.2 raeburn 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,
1.11 raeburn 1388: $indexhash,\%userinfo,\%coauthors,$permission);
1.2 raeburn 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)) {
1.17 raeburn 1397: my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
1.11 raeburn 1398: next if ($cnum eq '' || $cdom eq '');
1.17 raeburn 1399: my $custom = 1;
1.2 raeburn 1400: my (@roles,@sections,%access,%users,%userdata,
1.6 albertel 1401: %statushash);
1.2 raeburn 1402: if ($env{'form.showrole'} eq 'Any') {
1.17 raeburn 1403: @roles = &course_roles($context,undef,$custom);
1.2 raeburn 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,
1.11 raeburn 1430: \%userinfo,\%allusers,$permission);
1.2 raeburn 1431: } else {
1.10 raeburn 1432: $r->print('<input type="hidden" name="phase" value="'.
1433: $env{'form.phase'}.'" /></form>');
1.2 raeburn 1434: return;
1435: }
1.1 raeburn 1436: }
1437: }
1.3 raeburn 1438: }
1439: if (keys(%userlist) == 0) {
1.13 raeburn 1440: if ($context eq 'author') {
1.3 raeburn 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");
1.13 raeburn 1445: } elsif ($env{'form.roletype'} eq 'author') {
1.3 raeburn 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");
1.2 raeburn 1449: }
1.3 raeburn 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
1.4 raeburn 1455: my $usercount;
1.3 raeburn 1456: if ($env{'form.action'} eq 'modifystudent') {
1.10 raeburn 1457: ($usercount) = &show_users_list($r,$context,'view',$permission,
1.4 raeburn 1458: $env{'form.Status'},\%userlist,$keylist);
1.1 raeburn 1459: } else {
1.4 raeburn 1460: ($usercount) = &show_users_list($r,$context,$env{'form.output'},
1.10 raeburn 1461: $permission,$env{'form.Status'},\%userlist,$keylist);
1.4 raeburn 1462: }
1463: if (!$usercount) {
1464: $r->print('<br />'.&mt('There are no users matching the search criteria.'));
1.2 raeburn 1465: }
1466: }
1.10 raeburn 1467: $r->print('<input type="hidden" name="phase" value="'.
1468: $env{'form.phase'}.'" /></form>');
1.2 raeburn 1469: }
1470:
1471: sub list_submit_button {
1472: my ($text) = @_;
1.11 raeburn 1473: return '<input type="button" name="updatedisplay" value="'.$text.'" onclick="javascript:display_update()" />';
1.2 raeburn 1474: }
1475:
1476: sub gather_userinfo {
1.11 raeburn 1477: my ($context,$format,$userlist,$indexhash,$userinfo,$rolehash,$permission) = @_;
1.2 raeburn 1478: foreach my $item (keys(%{$rolehash})) {
1479: my %userdata;
1.22 raeburn 1480: if ($context eq 'author') {
1.2 raeburn 1481: ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
1482: split(/:/,$item);
1483: ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
1.24 raeburn 1484: &build_user_record($context,\%userdata,$userinfo,$indexhash,
1485: $item,$userlist);
1.22 raeburn 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: }
1.24 raeburn 1494: &build_user_record($context,\%userdata,$userinfo,$indexhash,
1495: $item,$userlist);
1.2 raeburn 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});
1.24 raeburn 1501: &build_user_record($context,\%userdata,$userinfo,$indexhash,
1502: $item,$userlist);
1.13 raeburn 1503: } elsif ($env{'form.roletype'} eq 'author') {
1.2 raeburn 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;
1.25 raeburn 1511: &build_user_record($context,\%userdata,$userinfo,
1512: $indexhash,$uniqid,$userlist);
1.2 raeburn 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') {
1.11 raeburn 1519: my $numcids = keys(%{$rolehash->{$item}});
1.2 raeburn 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">';
1.23 raeburn 1527: # FIXME: actions on courses disabled for now
1528: # if ($permission->{'cusr'}) {
1529: # if ($numcids > 1) {
1.25 raeburn 1530: # $spanstart .= '<input type="radio" name="'.$item.'" value="'.$cid.'" /> ';
1.23 raeburn 1531: # } else {
1.25 raeburn 1532: # $spanstart .= '<input type="hidden" name="'.$item.'" value="'.$cid.'" /> ';
1.23 raeburn 1533: # }
1534: # }
1.2 raeburn 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'}}))) {
1.25 raeburn 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: }
1.2 raeburn 1547: }
1548: }
1549: }
1550: }
1551: }
1.25 raeburn 1552: if ($userdata{'status'} ne '') {
1553: &build_user_record($context,\%userdata,$userinfo,
1554: $indexhash,$item,$userlist);
1555: }
1.2 raeburn 1556: }
1557: }
1558: }
1559: return;
1560: }
1561:
1562: sub build_user_record {
1.24 raeburn 1563: my ($context,$userdata,$userinfo,$indexhash,$record_key,$userlist) = @_;
1.11 raeburn 1564: next if ($userdata->{'start'} eq '-1' && $userdata->{'end'} eq '-1');
1.24 raeburn 1565: if (!(($context eq 'domain') && ($env{'form.roletype'} eq 'course'))) {
1566: &process_date_info($userdata);
1567: }
1.2 raeburn 1568: my $username = $userdata->{'username'};
1569: my $domain = $userdata->{'domain'};
1570: if (ref($userinfo->{$username.':'.$domain}) eq 'HASH') {
1.24 raeburn 1571: $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
1.2 raeburn 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:
1.7 albertel 1600: my $totcodes = 0;
1601: $totcodes =
1.2 raeburn 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} = '';
1.1 raeburn 1692: }
1.2 raeburn 1693: } else {
1694: $courses{$env{'form.courselist'}} = '';
1.1 raeburn 1695: }
1.2 raeburn 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);
1.1 raeburn 1774: } else {
1.2 raeburn 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: }
1.1 raeburn 1784:
1.2 raeburn 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: }
1.1 raeburn 1793: }
1.2 raeburn 1794: if ($userdata->{'end'} > 0) {
1795: if ($now > $userdata->{'end'}) {
1796: $userdata->{'status'} = 'Expired';
1797: }
1798: }
1799: return;
1.1 raeburn 1800: }
1801:
1802: sub show_users_list {
1.10 raeburn 1803: my ($r,$context,$mode,$permission,$statusmode,$userlist,$keylist)=@_;
1.1 raeburn 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'};
1.3 raeburn 1812: my @sortable = ('username','domain','id','fullname','start','end','email','role');
1.2 raeburn 1813: if ($context eq 'course') {
1.3 raeburn 1814: push(@sortable,('section','groups','type'));
1.2 raeburn 1815: } else {
1.3 raeburn 1816: push(@sortable,'extent');
1817: }
1818: if (!grep(/^\Q$sortby\E$/,@sortable)) {
1819: $sortby = 'username';
1.1 raeburn 1820: }
1.22 raeburn 1821: my $setting = $env{'form.roletype'};
1.2 raeburn 1822: my ($cid,$cdom,$cnum,$classgroups,$displayphotos,$displayclickers);
1.1 raeburn 1823: if ($context eq 'course') {
1.22 raeburn 1824: $cid = $env{'request.course.id'};
1.17 raeburn 1825: ($cnum,$cdom) = &get_course_identity($cid);
1.2 raeburn 1826: ($classgroups) = &Apache::loncoursedata::get_group_memberships(
1827: $userlist,$keylist,$cdom,$cnum);
1.16 raeburn 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('
1.1 raeburn 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>
1.16 raeburn 1850: ');
1851: }
1852: $r->print(<<END);
1.1 raeburn 1853: <input type="hidden" name="displayphotos" value="$displayphotos" />
1854: <input type="hidden" name="displayclickers" value="$displayclickers" />
1855: END
1.16 raeburn 1856: }
1.1 raeburn 1857: }
1.16 raeburn 1858: if ($mode ne 'autoenroll') {
1.11 raeburn 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");
1.25 raeburn 1861: my $singconfirm = &mt(' for a single user?');
1862: my $multconfirm = &mt(' for multiple users?');
1.11 raeburn 1863: my $date_sec_selector = &date_section_javascript($context,$setting,$statusmode);
1.1 raeburn 1864: $r->print(<<END);
1.10 raeburn 1865:
1866: <script type="text/javascript" language="Javascript">
1.11 raeburn 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: }
1.10 raeburn 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>
1.11 raeburn 1921: $date_sec_selector
1.1 raeburn 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",
1.2 raeburn 1939: 'role' => "role",
1.1 raeburn 1940: 'type' => "enroll type/action",
1941: 'email' => "email address",
1942: 'clicker' => "clicker id",
1943: 'photo' => "photo",
1.2 raeburn 1944: 'extent' => "extent",
1.11 raeburn 1945: 'pr' => "Proceed",
1946: 'ca' => "check all",
1947: 'ua' => "uncheck all",
1948: 'ac' => "Action to take for selected users",
1.10 raeburn 1949: 'link' => "Behavior of username links",
1950: 'aboutme' => "Display a user's personal page",
1951: 'modify' => "Modify a user's information",
1.1 raeburn 1952: );
1.2 raeburn 1953: if ($context eq 'domain' && $env{'form.roletype'} eq 'course') {
1954: $lt{'extent'} = &mt('Course(s): description, section(s), status');
1.13 raeburn 1955: } elsif ($context eq 'author') {
1.2 raeburn 1956: $lt{'extent'} = &mt('Author');
1957: }
1.1 raeburn 1958: my @cols = ('username','domain','id','fullname');
1959: if ($context eq 'course') {
1960: push(@cols,'section');
1961: }
1.2 raeburn 1962: if (!($context eq 'domain' && $env{'form.roletype'} eq 'course')) {
1963: push(@cols,('start','end'));
1964: }
1.3 raeburn 1965: if ($env{'form.showrole'} eq 'Any' || $env{'form.showrole'} eq 'cr') {
1.2 raeburn 1966: push(@cols,'role');
1967: }
1.13 raeburn 1968: if ($context eq 'domain' && ($env{'form.roletype'} eq 'author' ||
1.2 raeburn 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'))) {
1.1 raeburn 1974: push(@cols,'status');
1975: }
1976: if ($context eq 'course') {
1977: push(@cols,'groups');
1978: }
1979: push(@cols,'email');
1980:
1.4 raeburn 1981: my $rolefilter = $env{'form.showrole'};
1.5 raeburn 1982: if ($env{'form.showrole'} eq 'cr') {
1983: $rolefilter = &mt('custom');
1984: } elsif ($env{'form.showrole'} ne 'Any') {
1.2 raeburn 1985: $rolefilter = &Apache::lonnet::plaintext($env{'form.showrole'});
1986: }
1.16 raeburn 1987: my $results_description;
1988: if ($mode ne 'autoenroll') {
1989: $results_description = &results_header_row($rolefilter,$statusmode,
1.24 raeburn 1990: $context,$permission,$mode);
1.16 raeburn 1991: $r->print('<b>'.$results_description.'</b><br />');
1992: }
1.26 ! raeburn 1993: my ($output,$actionselect,%canchange,%canchangesec);
1.16 raeburn 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);
1.10 raeburn 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="" />
1.11 raeburn 2004: <input type="hidden" name="srchdomain" value="" />
1.1 raeburn 2005: END
1.16 raeburn 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";
1.11 raeburn 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
1.26 ! raeburn 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: }
1.16 raeburn 2066: }
1.4 raeburn 2067: }
2068: $output .= "\n<p>\n".
1.1 raeburn 2069: &Apache::loncommon::start_data_table().
1.4 raeburn 2070: &Apache::loncommon::start_data_table_header_row();
1.1 raeburn 2071: if ($mode eq 'autoenroll') {
1.4 raeburn 2072: $output .= "
1.1 raeburn 2073: <th><a href=\"javascript:document.studentform.sortby.value='type';document.studentform.submit();\">$lt{'type'}</a></th>
1.4 raeburn 2074: ";
1.1 raeburn 2075: } else {
1.11 raeburn 2076: $output .= "\n".'<th>'.&mt('Count').'</th>'."\n";
2077: if ($actionselect) {
2078: $output .= '<th>'.&mt('Select').'</th>'."\n";
2079: }
1.1 raeburn 2080: }
2081: foreach my $item (@cols) {
1.4 raeburn 2082: $output .= "<th><a href=\"javascript:document.studentform.sortby.value='$item';document.studentform.submit();\">$lt{$item}</a></th>\n";
1.1 raeburn 2083: }
1.2 raeburn 2084: my %role_types = &role_type_names();
1.16 raeburn 2085: if ($context eq 'course' && $mode ne 'autoenroll') {
1.4 raeburn 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='.
1.1 raeburn 2098: "'".$clickerchg."'".';document.studentform.submit();">'.
2099: $clicker_options{$clickerchg}.'</a> '.$lt{'clicker'}."\n".
1.4 raeburn 2100: ' </th>'."\n";
1.1 raeburn 2101:
1.4 raeburn 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='.
1.1 raeburn 2114: "'".$photochg."'".';document.studentform.submit();">'.
2115: $photo_options{$photochg}.'</a> '.$lt{'photo'}."\n".
1.4 raeburn 2116: ' </th>'."\n";
2117: }
1.1 raeburn 2118: }
1.4 raeburn 2119: }
1.16 raeburn 2120: $output .= &Apache::loncommon::end_data_table_header_row();
1.1 raeburn 2121: # Done with the HTML header line
2122: } elsif ($mode eq 'csv') {
2123: #
2124: # Open a file
2125: $CSVfilename = '/prtspool/'.
1.2 raeburn 2126: $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
2127: time.'_'.rand(1000000000).'.csv';
1.1 raeburn 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
1.2 raeburn 2137: print $CSVfile '"'.$results_description.'"'."\n";
1.1 raeburn 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');
1.2 raeburn 2147: $excel_sheet->write($row++,0,$results_description,$format->{'h2'});
1.1 raeburn 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;
1.2 raeburn 2157: foreach my $idx (@$keylist) {
2158: $index{$idx} = $i++;
2159: }
1.4 raeburn 2160: my $usercount = 0;
1.2 raeburn 2161: # Get groups, role, permanent e-mail so we can sort on them if
2162: # necessary.
2163: foreach my $user (keys(%{$userlist})) {
1.11 raeburn 2164: if ($context eq 'domain' && $user eq $env{'request.role.domain'}.'-domainconfig:'.$env{'request.role.domain'}) {
2165: delete($userlist->{$user});
2166: next;
2167: }
1.2 raeburn 2168: my ($uname,$udom,$role,$groups,$email);
1.5 raeburn 2169: if (($statusmode ne 'Any') &&
2170: ($userlist->{$user}->[$index{'status'}] ne $statusmode)) {
2171: delete($userlist->{$user});
2172: next;
2173: }
1.2 raeburn 2174: if ($context eq 'domain') {
2175: if ($env{'form.roletype'} eq 'domain') {
2176: ($role,$uname,$udom) = split(/:/,$user);
1.11 raeburn 2177: if (($uname eq $env{'request.role.domain'}.'-domainconfig') &&
2178: ($udom eq $env{'request.role.domain'})) {
2179: delete($userlist->{$user});
2180: next;
2181: }
1.13 raeburn 2182: } elsif ($env{'form.roletype'} eq 'author') {
1.2 raeburn 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: }
1.4 raeburn 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: }
1.1 raeburn 2222: }
1.2 raeburn 2223: #
2224: # Sort the users
1.1 raeburn 2225: my $index = $index{$sortby};
2226: my $second = $index{'username'};
2227: my $third = $index{'domain'};
1.2 raeburn 2228: my @sorted_users = sort {
2229: lc($userlist->{$a}->[$index]) cmp lc($userlist->{$b}->[$index])
1.1 raeburn 2230: ||
1.2 raeburn 2231: lc($userlist->{$a}->[$second]) cmp lc($userlist->{$b}->[$second]) ||
2232: lc($userlist->{$a}->[$third]) cmp lc($userlist->{$b}->[$third])
2233: } (keys(%$userlist));
1.4 raeburn 2234: my $rowcount = 0;
1.2 raeburn 2235: foreach my $user (@sorted_users) {
1.4 raeburn 2236: my %in;
1.2 raeburn 2237: my $sdata = $userlist->{$user};
1.4 raeburn 2238: $rowcount ++;
1.2 raeburn 2239: foreach my $item (@{$keylist}) {
2240: $in{$item} = $sdata->[$index{$item}];
2241: }
1.11 raeburn 2242: my $role = $in{'role'};
1.2 raeburn 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'});
1.1 raeburn 2248: }
1.2 raeburn 2249: if (! defined($in{'end'}) || $in{'end'} == 0) {
2250: $in{'end'} = &mt('none');
1.1 raeburn 2251: } else {
1.2 raeburn 2252: $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
1.1 raeburn 2253: }
1.2 raeburn 2254: if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll') {
2255: $r->print(&Apache::loncommon::start_data_table_row());
1.11 raeburn 2256: my $checkval;
1.16 raeburn 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 ++;
1.11 raeburn 2271: }
1.16 raeburn 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) {
1.26 ! raeburn 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: }
1.16 raeburn 2292: }
1.26 ! raeburn 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: }
1.16 raeburn 2305: }
1.26 ! raeburn 2306: $r->print('<td><input type="checkbox" name="'.
! 2307: 'actionlist" value="'.$checkval.'"></td>');
! 2308: } else {
! 2309: $r->print('<td> </td>');
1.16 raeburn 2310: }
1.11 raeburn 2311: }
2312: }
1.2 raeburn 2313: foreach my $item (@cols) {
1.10 raeburn 2314: if ($item eq 'username') {
1.16 raeburn 2315: $r->print('<td>'.&print_username_link($mode,$permission,
2316: \%in).'</td>');
2317: } elsif (($item eq 'start' || $item eq 'end') && ($actionselect)) {
1.11 raeburn 2318: $r->print('<td>'.$in{$item}.'<input type="hidden" name="'.$checkval.'_'.$item.'" value="'.$sdata->[$index{$item}].'" /></td>'."\n");
1.10 raeburn 2319: } else {
2320: $r->print('<td>'.$in{$item}.'</td>'."\n");
2321: }
1.2 raeburn 2322: }
1.16 raeburn 2323: if (($context eq 'course') && ($mode ne 'autoenroll')) {
1.4 raeburn 2324: if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
2325: if ($displayclickers eq 'on') {
2326: my $clickers =
1.2 raeburn 2327: (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
1.4 raeburn 2328: if ($clickers!~/\w/) { $clickers='-'; }
2329: $r->print('<td>'.$clickers.'</td>');
1.2 raeburn 2330: } else {
2331: $r->print(' <td> </td> ');
2332: }
1.4 raeburn 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: }
1.2 raeburn 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++;
1.1 raeburn 2380: }
2381: }
1.2 raeburn 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);
1.4 raeburn 2397: } else {
2398: return ($usercount);
1.2 raeburn 2399: }
1.1 raeburn 2400: }
2401:
1.10 raeburn 2402: sub print_username_link {
1.16 raeburn 2403: my ($mode,$permission,$in) = @_;
1.10 raeburn 2404: my $output;
1.16 raeburn 2405: if ($mode eq 'autoenroll') {
2406: $output = $in->{'username'};
2407: } elsif (!$permission->{'cusr'}) {
1.10 raeburn 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:
1.2 raeburn 2419: sub role_type_names {
2420: my %lt = &Apache::lonlocal::texthash (
1.13 raeburn 2421: 'domain' => 'Domain Roles',
2422: 'author' => 'Co-Author Roles',
2423: 'course' => 'Course Roles',
1.2 raeburn 2424: );
2425: return %lt;
2426: }
2427:
1.11 raeburn 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);
1.23 raeburn 2439: # FIXME Disable actions for now for roletype=course in domain context
2440: if ($context eq 'domain' && $setting eq 'course') {
2441: return;
2442: }
1.26 ! raeburn 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: }
1.11 raeburn 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: }
1.13 raeburn 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: }
1.11 raeburn 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')) {
1.26 ! raeburn 2483: if (($statusmode ne 'Expired') && ($env{'request.course.sec'} eq '')) {
1.11 raeburn 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 {
1.21 raeburn 2562: my ($context,$permission) = @_;
1.11 raeburn 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')) {
1.26 ! raeburn 2590: if ($env{'request.course.sec'} eq '') {
! 2591: $output .= <<"END";
1.11 raeburn 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
1.26 ! raeburn 2601: }
1.11 raeburn 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,
1.21 raeburn 2643: $env{'form.bulkaction'},$formname,
2644: $permission);
1.11 raeburn 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') {
1.17 raeburn 2650: my ($cnum,$cdom) = &get_course_identity();
1.11 raeburn 2651: my $info;
2652: if ($env{'form.showrole'} eq 'st') {
2653: $output .= '<p>'.$lt{'fors'}.'</p>';
1.26 ! raeburn 2654: } elsif ($env{'form.showrole'} eq 'Any') {
1.11 raeburn 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: }
1.21 raeburn 2675: my $rowtitle = &mt('New section to assign');
2676: my $secbox = §ion_picker($cdom,$cnum,$env{'form.showrole'},$rowtitle,$permission,$context);
1.11 raeburn 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:
1.17 raeburn 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:
1.2 raeburn 2716: sub results_header_row {
1.24 raeburn 2717: my ($rolefilter,$statusmode,$context,$permission,$mode) = @_;
1.5 raeburn 2718: my ($description,$showfilter);
2719: if ($rolefilter ne 'Any') {
2720: $showfilter = $rolefilter;
2721: }
1.2 raeburn 2722: if ($context eq 'course') {
1.24 raeburn 2723: if ($mode eq 'csv' || $mode eq 'excel') {
2724: $description = &mt('Course - ').$env{'course.'.$env{'request.course.id'}.'.description'}.': ';
2725: }
1.2 raeburn 2726: if ($statusmode eq 'Expired') {
1.5 raeburn 2727: $description .= &mt('Users in course with expired [_1] roles',$showfilter);
1.11 raeburn 2728: } elsif ($statusmode eq 'Future') {
1.5 raeburn 2729: $description .= &mt('Users in course with future [_1] roles',$showfilter);
1.2 raeburn 2730: } elsif ($statusmode eq 'Active') {
1.5 raeburn 2731: $description .= &mt('Users in course with active [_1] roles',$showfilter);
1.2 raeburn 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: }
1.26 ! raeburn 2739: my $viewablesec = &viewable_section($permission);
! 2740: if ($viewablesec ne '') {
1.15 raeburn 2741: if ($env{'form.showrole'} eq 'st') {
1.26 ! raeburn 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);
1.15 raeburn 2745: }
2746: }
1.13 raeburn 2747: } elsif ($context eq 'author') {
1.14 raeburn 2748: $description =
2749: &mt('Author space for <span class="LC_cusr_emph">[_1]</span>',
2750: &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'})).': ';
1.2 raeburn 2751: if ($statusmode eq 'Expired') {
1.5 raeburn 2752: $description .= &mt('Co-authors with expired [_1] roles',$showfilter);
1.2 raeburn 2753: } elsif ($statusmode eq 'Future') {
1.5 raeburn 2754: $description .= &mt('Co-authors with future [_1] roles',$showfilter);
1.2 raeburn 2755: } elsif ($statusmode eq 'Active') {
1.5 raeburn 2756: $description .= &mt('Co-authors with active [_1] roles',$showfilter);
1.2 raeburn 2757: } else {
2758: if ($rolefilter eq 'Any') {
1.5 raeburn 2759: $description .= &mt('All co-authors');
1.2 raeburn 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') {
1.5 raeburn 2769: $description .= &mt('Users in domain with expired [_1] roles',$showfilter);
1.2 raeburn 2770: } elsif ($statusmode eq 'Future') {
1.5 raeburn 2771: $description .= &mt('Users in domain with future [_1] roles',$showfilter);
1.2 raeburn 2772: } elsif ($statusmode eq 'Active') {
1.5 raeburn 2773: $description .= &mt('Users in domain with active [_1] roles',$showfilter);
1.2 raeburn 2774: } else {
2775: if ($rolefilter eq 'Any') {
1.5 raeburn 2776: $description .= &mt('All users in domain');
1.2 raeburn 2777: } else {
2778: $description .= &mt('All users in domain with [_1] roles',$rolefilter);
2779: }
2780: }
1.13 raeburn 2781: } elsif ($env{'form.roletype'} eq 'author') {
1.2 raeburn 2782: if ($statusmode eq 'Expired') {
1.5 raeburn 2783: $description .= &mt('Co-authors in domain with expired [_1] roles',$showfilter);
1.2 raeburn 2784: } elsif ($statusmode eq 'Future') {
1.5 raeburn 2785: $description .= &mt('Co-authors in domain with future [_1] roles',$showfilter);
1.2 raeburn 2786: } elsif ($statusmode eq 'Active') {
1.5 raeburn 2787: $description .= &mt('Co-authors in domain with active [_1] roles',$showfilter);
1.2 raeburn 2788: } else {
2789: if ($rolefilter eq 'Any') {
1.5 raeburn 2790: $description .= &mt('All users with co-author roles in domain',$showfilter);
1.2 raeburn 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') {
1.5 raeburn 2810: $description .= &mt('users with expired [_1] roles',$showfilter);
1.2 raeburn 2811: } elsif ($statusmode eq 'Future') {
1.5 raeburn 2812: $description .= &mt('users with future [_1] roles',$showfilter);
1.2 raeburn 2813: } elsif ($statusmode eq 'Active') {
1.5 raeburn 2814: $description .= &mt('users with active [_1] roles',$showfilter);
1.2 raeburn 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: }
1.22 raeburn 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:
1.2 raeburn 2840:
1.1 raeburn 2841: #################################################
2842: #################################################
2843: sub show_drop_list {
1.17 raeburn 2844: my ($r,$classlist,$keylist,$nosort,$permission)=@_;
1.1 raeburn 2845: my $cid=$env{'request.course.id'};
1.17 raeburn 2846: my ($cnum,$cdom) = &get_course_identity($cid);
1.1 raeburn 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 ($classgroups) = &Apache::loncoursedata::get_group_memberships(
2856: $classlist,$keylist,$cdom,$cnum);
2857: #
2858: my $action = "drop";
1.17 raeburn 2859: my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
1.1 raeburn 2860: $r->print(<<END);
2861: <input type="hidden" name="sortby" value="$sortby" />
2862: <input type="hidden" name="action" value="$action" />
2863: <input type="hidden" name="state" value="done" />
1.17 raeburn 2864: <script type="text/javascript" language="Javascript">
2865: $check_uncheck_js
1.1 raeburn 2866: </script>
2867: <p>
2868: <input type="hidden" name="phase" value="four">
2869: END
2870:
2871: my %lt=&Apache::lonlocal::texthash('usrn' => "username",
2872: 'dom' => "domain",
2873: 'sn' => "student name",
2874: 'sec' => "section",
2875: 'start' => "start date",
2876: 'end' => "end date",
2877: 'groups' => "active groups",
2878: );
2879: if ($nosort) {
1.17 raeburn 2880: $r->print(&Apache::loncommon::start_data_table().
2881: &Apache::loncommon::start_data_table_header_row());
1.1 raeburn 2882: $r->print(<<END);
2883: <th> </th>
2884: <th>$lt{'usrn'}</th>
2885: <th>$lt{'dom'}</th>
2886: <th>ID</th>
2887: <th>$lt{'sn'}</th>
2888: <th>$lt{'sec'}</th>
2889: <th>$lt{'start'}</th>
2890: <th>$lt{'end'}</th>
2891: <th>$lt{'groups'}</th>
2892: END
1.17 raeburn 2893: $r->print(&Apache::loncommon::end_data_table_header_row());
1.1 raeburn 2894: } else {
1.17 raeburn 2895: $r->print(&Apache::loncommon::start_data_table().
2896: &Apache::loncommon::start_data_table_header_row());
1.1 raeburn 2897: $r->print(<<END);
1.17 raeburn 2898: <th> </th>
1.1 raeburn 2899: <th>
1.17 raeburn 2900: <a href="/adm/createuser?action=$action&sortby=username">$lt{'usrn'}</a>
1.1 raeburn 2901: </th><th>
1.17 raeburn 2902: <a href="/adm/createuser?action=$action&sortby=domain">$lt{'dom'}</a>
1.1 raeburn 2903: </th><th>
1.17 raeburn 2904: <a href="/adm/createuser?action=$action&sortby=id">ID</a>
1.1 raeburn 2905: </th><th>
1.17 raeburn 2906: <a href="/adm/createuser?action=$action&sortby=fullname">$lt{'sn'}</a>
1.1 raeburn 2907: </th><th>
1.17 raeburn 2908: <a href="/adm/createuser?action=$action&sortby=section">$lt{'sec'}</a>
1.1 raeburn 2909: </th><th>
1.17 raeburn 2910: <a href="/adm/createuser?action=$action&sortby=start">$lt{'start'}</a>
1.1 raeburn 2911: </th><th>
1.17 raeburn 2912: <a href="/adm/createuser?action=$action&sortby=end">$lt{'end'}</a>
1.1 raeburn 2913: </th><th>
1.17 raeburn 2914: <a href="/adm/createuser?action=$action&sortby=groups">$lt{'groups'}</a>
1.1 raeburn 2915: </th>
2916: END
1.17 raeburn 2917: $r->print(&Apache::loncommon::end_data_table_header_row());
1.1 raeburn 2918: }
2919: #
2920: # Sort the students
2921: my %index;
2922: my $i;
2923: foreach (@$keylist) {
2924: $index{$_} = $i++;
2925: }
2926: $index{'groups'} = scalar(@$keylist);
2927: my $index = $index{$sortby};
2928: my $second = $index{'username'};
2929: my $third = $index{'domain'};
2930: my @Sorted_Students = sort {
2931: lc($classlist->{$a}->[$index]) cmp lc($classlist->{$b}->[$index])
2932: ||
2933: lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
2934: ||
2935: lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
2936: } (keys(%$classlist));
2937: foreach my $student (@Sorted_Students) {
2938: my $error;
2939: my $sdata = $classlist->{$student};
2940: my $username = $sdata->[$index{'username'}];
2941: my $domain = $sdata->[$index{'domain'}];
2942: my $section = $sdata->[$index{'section'}];
2943: my $name = $sdata->[$index{'fullname'}];
2944: my $id = $sdata->[$index{'id'}];
2945: my $start = $sdata->[$index{'start'}];
2946: my $end = $sdata->[$index{'end'}];
2947: my $groups = $classgroups->{$student};
2948: my $active_groups;
2949: if (ref($groups->{active}) eq 'HASH') {
2950: $active_groups = join(', ',keys(%{$groups->{'active'}}));
2951: }
2952: if (! defined($start) || $start == 0) {
2953: $start = &mt('none');
2954: } else {
2955: $start = &Apache::lonlocal::locallocaltime($start);
2956: }
2957: if (! defined($end) || $end == 0) {
2958: $end = &mt('none');
2959: } else {
2960: $end = &Apache::lonlocal::locallocaltime($end);
2961: }
2962: my $status = $sdata->[$index{'status'}];
2963: next if ($status ne 'Active');
1.17 raeburn 2964: if ($env{'request.course.sec'} ne '') {
2965: if ($section ne $env{'request.course.sec'}) {
2966: next;
2967: }
2968: }
2969: my $studentkey = $student.':'.$section;
2970: my $startitem = '<input type="hidden" name="'.$studentkey.'_start" value="'.$sdata->[$index{'start'}].'" />';
1.1 raeburn 2971: #
2972: $r->print(&Apache::loncommon::start_data_table_row());
2973: $r->print(<<"END");
2974: <td><input type="checkbox" name="droplist" value="$student"></td>
2975: <td>$username</td>
2976: <td>$domain</td>
2977: <td>$id</td>
2978: <td>$name</td>
2979: <td>$section</td>
2980: <td>$start</td>
2981: <td>$end</td>
2982: <td>$active_groups</td>
2983: END
2984: $r->print(&Apache::loncommon::end_data_table_row());
2985: }
2986: $r->print(&Apache::loncommon::end_data_table().'<br />');
2987: %lt=&Apache::lonlocal::texthash(
2988: 'dp' => "Expire Users' Roles",
2989: 'ca' => "check all",
2990: 'ua' => "uncheck all",
2991: );
2992: $r->print(<<"END");
2993: </p><p>
2994: <input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.studentform.droplist)">
2995: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.studentform.droplist)">
2996: <p><input type=submit value="$lt{'dp'}"></p>
2997: END
2998: return;
2999: }
3000:
3001: #
3002: # Print out the initial form to get the file containing a list of users
3003: #
3004: sub print_first_users_upload_form {
3005: my ($r,$context) = @_;
3006: my $str;
3007: $str = '<input type="hidden" name="phase" value="two">';
3008: $str .= '<input type="hidden" name="action" value="upload" />';
3009: $str .= '<input type="hidden" name="state" value="got_file" />';
1.2 raeburn 3010: $str .= "<h3>".&mt('Upload a file containing information about users')."</h3>\n";
1.1 raeburn 3011: $str .= &Apache::loncommon::upfile_select_html();
3012: $str .= "<p>\n";
3013: $str .= '<input type="submit" name="fileupload" value="'.
1.2 raeburn 3014: &mt('Upload file of users').'">'."\n";
1.1 raeburn 3015: $str .= '<label><input type="checkbox" name="noFirstLine" /> '.
3016: &mt('Ignore First Line')."</label></p>\n";
3017: $str .= &Apache::loncommon::help_open_topic("Course_Create_Class_List",
3018: &mt("How do I create a users list from a spreadsheet")).
3019: "<br />\n";
3020: $str .= &Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
3021: &mt("How do I create a CSV file from a spreadsheet")).
3022: "<br />\n";
3023: $str .= &Apache::loncommon::end_page();
3024: $r->print($str);
3025: return;
3026: }
3027:
3028: # ================================================= Drop/Add from uploaded file
3029: sub upfile_drop_add {
1.21 raeburn 3030: my ($r,$context,$permission) = @_;
1.1 raeburn 3031: &Apache::loncommon::load_tmp_file($r);
3032: my @userdata=&Apache::loncommon::upfile_record_sep();
3033: if($env{'form.noFirstLine'}){shift(@userdata);}
3034: my @keyfields = split(/\,/,$env{'form.keyfields'});
3035: my %fields=();
3036: for (my $i=0; $i<=$env{'form.nfields'}; $i++) {
3037: if ($env{'form.upfile_associate'} eq 'reverse') {
3038: if ($env{'form.f'.$i} ne 'none') {
3039: $fields{$keyfields[$i]}=$env{'form.f'.$i};
3040: }
3041: } else {
3042: $fields{$env{'form.f'.$i}}=$keyfields[$i];
3043: }
3044: }
3045: #
3046: # Store the field choices away
3047: foreach my $field (qw/username names
3048: fname mname lname gen id sec ipwd email role/) {
3049: $env{'form.'.$field.'_choice'}=$fields{$field};
3050: }
3051: &Apache::loncommon::store_course_settings('enrollment_upload',
3052: { 'username_choice' => 'scalar',
3053: 'names_choice' => 'scalar',
3054: 'fname_choice' => 'scalar',
3055: 'mname_choice' => 'scalar',
3056: 'lname_choice' => 'scalar',
3057: 'gen_choice' => 'scalar',
3058: 'id_choice' => 'scalar',
3059: 'sec_choice' => 'scalar',
3060: 'ipwd_choice' => 'scalar',
3061: 'email_choice' => 'scalar',
3062: 'role_choice' => 'scalar' });
3063: #
3064: my ($startdate,$enddate) = &get_dates_from_form();
3065: if ($env{'form.makedatesdefault'}) {
3066: $r->print(&make_dates_default($startdate,$enddate));
3067: }
3068: # Determine domain and desired host (home server)
3069: my $domain=$env{'request.role.domain'};
3070: my $desiredhost = $env{'form.lcserver'};
3071: if (lc($desiredhost) eq 'default') {
3072: $desiredhost = undef;
3073: } else {
3074: my %home_servers = &Apache::lonnet::get_servers($domain,'library');
3075: if (! exists($home_servers{$desiredhost})) {
3076: $r->print('<span class="LC_error">'.&mt('Error').
3077: &mt('Invalid home server specified').'</span>');
3078: $r->print(&Apache::loncommon::end_page());
3079: return;
3080: }
3081: }
3082: # Determine authentication mechanism
3083: my $changeauth;
3084: if ($context eq 'domain') {
3085: $changeauth = $env{'form.changeauth'};
3086: }
3087: my $amode = '';
3088: my $genpwd = '';
3089: if ($env{'form.login'} eq 'krb') {
3090: $amode='krb';
3091: $amode.=$env{'form.krbver'};
3092: $genpwd=$env{'form.krbarg'};
3093: } elsif ($env{'form.login'} eq 'int') {
3094: $amode='internal';
3095: if ((defined($env{'form.intarg'})) && ($env{'form.intarg'})) {
3096: $genpwd=$env{'form.intarg'};
3097: }
3098: } elsif ($env{'form.login'} eq 'loc') {
3099: $amode='localauth';
3100: if ((defined($env{'form.locarg'})) && ($env{'form.locarg'})) {
3101: $genpwd=$env{'form.locarg'};
3102: }
3103: }
3104: if ($amode =~ /^krb/) {
3105: if (! defined($genpwd) || $genpwd eq '') {
3106: $r->print('<span class="Error">'.
3107: &mt('Unable to enroll users').' '.
3108: &mt('No Kerberos domain was specified.').'</span></p>');
3109: $amode = ''; # This causes the loop below to be skipped
3110: }
3111: }
3112: my ($cid,$defaultsec,$defaultrole,$setting);
3113: if ($context eq 'domain') {
3114: $setting = $env{'form.roleaction'};
3115: if ($setting eq 'domain') {
3116: $defaultrole = $env{'form.defaultrole'};
3117: } elsif ($setting eq 'course') {
3118: $defaultrole = $env{'form.courserole'};
3119: }
1.13 raeburn 3120: } elsif ($context eq 'author') {
1.1 raeburn 3121: $defaultrole = $env{'form.defaultrole'};
3122: }
3123: if ($context eq 'domain' && $setting eq 'course') {
3124: if ($env{'form.newsec'} ne '') {
3125: $defaultsec = $env{'form.newsec'};
3126: } elsif ($env{'form.defaultsec'} ne '') {
3127: $defaultsec = $env{'form.defaultsec'}
3128: }
3129: }
3130: if ($env{'request.course.id'} ne '') {
3131: $cid = $env{'request.course.id'};
3132: } elsif ($env{'form.defaultdomain'} ne '' && $env{'form.defaultcourse'} ne '') {
3133: $cid = $env{'form.defaultdomain'}.'_'.
3134: $env{'form.defaultcourse'};
3135: }
3136: if ( $domain eq &LONCAPA::clean_domain($domain)
3137: && ($amode ne '')) {
3138: #######################################
3139: ## Add/Modify Users ##
3140: #######################################
3141: if ($context eq 'course') {
3142: $r->print('<h3>'.&mt('Enrolling Users')."</h3>\n<p>\n");
1.13 raeburn 3143: } elsif ($context eq 'author') {
1.1 raeburn 3144: $r->print('<h3>'.&mt('Updating Co-authors')."</h3>\n<p>\n");
3145: } else {
3146: $r->print('<h3>'.&mt('Adding/Modifying Users')."</h3>\n<p>\n");
3147: }
3148: my %counts = (
3149: user => 0,
3150: auth => 0,
3151: role => 0,
3152: );
3153: my $flushc=0;
3154: my %student=();
3155: my %curr_groups;
3156: my %userchg;
3157: if ($context eq 'course') {
3158: # Get information about course groups
3159: %curr_groups = &Apache::longroup::coursegroups();
3160: }
1.5 raeburn 3161: my (%curr_rules,%got_rules,%alerts);
1.1 raeburn 3162: # Get new users list
3163: foreach (@userdata) {
3164: my %entries=&Apache::loncommon::record_sep($_);
3165: # Determine user name
3166: unless (($entries{$fields{'username'}} eq '') ||
3167: (!defined($entries{$fields{'username'}}))) {
3168: my ($fname, $mname, $lname,$gen) = ('','','','');
3169: if (defined($fields{'names'})) {
3170: ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
3171: /([^\,]+)\,\s*(\w+)\s*(.*)$/);
3172: } else {
3173: if (defined($fields{'fname'})) {
3174: $fname=$entries{$fields{'fname'}};
3175: }
3176: if (defined($fields{'mname'})) {
3177: $mname=$entries{$fields{'mname'}};
3178: }
3179: if (defined($fields{'lname'})) {
3180: $lname=$entries{$fields{'lname'}};
3181: }
3182: if (defined($fields{'gen'})) {
3183: $gen=$entries{$fields{'gen'}};
3184: }
3185: }
3186: if ($entries{$fields{'username'}}
3187: ne &LONCAPA::clean_username($entries{$fields{'username'}})) {
3188: $r->print('<br />'.
3189: &mt('<b>[_1]</b>: Unacceptable username for user [_2] [_3] [_4] [_5]',
3190: $entries{$fields{'username'}},$fname,$mname,$lname,$gen).
3191: '</b>');
3192: } else {
1.5 raeburn 3193: my $username = $entries{$fields{'username'}};
1.1 raeburn 3194: my $sec;
3195: if ($context eq 'course' || $setting eq 'course') {
3196: # determine section number
3197: if (defined($fields{'sec'})) {
3198: if (defined($entries{$fields{'sec'}})) {
3199: $sec=$entries{$fields{'sec'}};
3200: }
3201: } else {
3202: $sec = $defaultsec;
3203: }
3204: # remove non alphanumeric values from section
3205: $sec =~ s/\W//g;
3206: if ($sec eq "none" || $sec eq 'all') {
3207: $r->print('<br />'.
3208: &mt('<b>[_1]</b>: Unable to enroll: section name "[_2]" for user [_3] [_4] [_5] [_6] is a reserved word.',
3209: $username,$sec,$fname,$mname,$lname,$gen));
3210: next;
3211: } elsif (($sec ne '') && (exists($curr_groups{$sec}))) {
3212: $r->print('<br />'.
3213: &mt('<b>[_1]</b>: Unable to enroll: section name "[_2]" for user [_3] [_4] [_5] [_6] is a course group. Section names and group names must be distinct.',
3214: $username,$sec,$fname,$mname,$lname,$gen));
3215: next;
3216: }
3217: }
3218: # determine id number
3219: my $id='';
3220: if (defined($fields{'id'})) {
3221: if (defined($entries{$fields{'id'}})) {
3222: $id=$entries{$fields{'id'}};
3223: }
3224: $id=~tr/A-Z/a-z/;
3225: }
3226: # determine email address
3227: my $email='';
3228: if (defined($fields{'email'})) {
3229: if (defined($entries{$fields{'email'}})) {
3230: $email=$entries{$fields{'email'}};
3231: unless ($email=~/^[^\@]+\@[^\@]+$/) { $email=''; } }
3232: }
3233: # determine user password
3234: my $password = $genpwd;
3235: if (defined($fields{'ipwd'})) {
3236: if ($entries{$fields{'ipwd'}}) {
3237: $password=$entries{$fields{'ipwd'}};
3238: }
3239: }
3240: # determine user role
3241: my $role = '';
3242: if (defined($fields{'role'})) {
3243: if ($entries{$fields{'role'}}) {
1.2 raeburn 3244: my @poss_roles =
1.1 raeburn 3245: &curr_role_permissions($context,$setting);
1.2 raeburn 3246: if (grep(/^\Q$entries{$fields{'role'}}\E/,@poss_roles)) {
1.1 raeburn 3247: $role=$entries{$fields{'role'}};
3248: } else {
3249: my $rolestr = join(', ',@poss_roles);
3250: $r->print('<br />'.
3251: &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");
3252: next;
3253: }
3254: }
3255: }
3256: if ($role eq '') {
3257: $role = $defaultrole;
3258: }
3259: # Clean up whitespace
3260: foreach (\$domain,\$username,\$id,\$fname,\$mname,
3261: \$lname,\$gen,\$sec,\$role) {
3262: $$_ =~ s/(\s+$|^\s+)//g;
3263: }
1.5 raeburn 3264: # check against rules
3265: my $checkid = 0;
3266: my $newuser = 0;
3267: my (%rulematch,%inst_results,%idinst_results);
3268: my $uhome=&Apache::lonnet::homeserver($username,$domain);
3269: if ($uhome eq 'no_host') {
3270: $checkid = 1;
3271: $newuser = 1;
3272: my $checkhash;
3273: my $checks = { 'username' => 1 };
3274: $checkhash->{$username.':'.$domain} = { 'newuser' => 1, };
3275: &Apache::loncommon::user_rule_check($checkhash,$checks,
3276: \%alerts,\%rulematch,\%inst_results,\%curr_rules,
3277: \%got_rules);
3278: if (ref($alerts{'username'}) eq 'HASH') {
3279: if (ref($alerts{'username'}{$domain}) eq 'HASH') {
3280: next if ($alerts{'username'}{$domain}{$username});
3281: }
3282: }
1.13 raeburn 3283: } else {
3284: # FIXME check if user info can be updated.
1.5 raeburn 3285: }
3286: if ($id ne '') {
3287: if (!$newuser) {
3288: my %idhash = &Apache::lonnet::idrget($domain,($username));
3289: if ($idhash{$username} ne $id) {
3290: $checkid = 1;
3291: }
3292: }
3293: if ($checkid) {
3294: my $checkhash;
3295: my $checks = { 'id' => 1 };
3296: $checkhash->{$username.':'.$domain} = { 'newuser' => $newuser,
3297: 'id' => $id };
3298: &Apache::loncommon::user_rule_check($checkhash,$checks,
3299: \%alerts,\%rulematch,\%idinst_results,\%curr_rules,
3300: \%got_rules);
3301: if (ref($alerts{'id'}) eq 'HASH') {
3302: if (ref($alerts{'id'}{$domain}) eq 'HASH') {
3303: next if ($alerts{'id'}{$domain}{$id});
3304: }
3305: }
3306: }
3307: }
1.1 raeburn 3308: if ($password || $env{'form.login'} eq 'loc') {
3309: my ($userresult,$authresult,$roleresult);
3310: if ($role eq 'st') {
3311: &modifystudent($domain,$username,$cid,$sec,
3312: $desiredhost);
3313: $roleresult =
3314: &Apache::lonnet::modifystudent
3315: ($domain,$username,$id,$amode,$password,
3316: $fname,$mname,$lname,$gen,$sec,$enddate,
3317: $startdate,$env{'form.forceid'},
3318: $desiredhost,$email);
3319: } else {
3320: ($userresult,$authresult,$roleresult) =
3321: &modifyuserrole($context,$setting,
3322: $changeauth,$cid,$domain,$username,
3323: $id,$amode,$password,$fname,
3324: $mname,$lname,$gen,$sec,
3325: $env{'form.forceid'},$desiredhost,
1.5 raeburn 3326: $email,$role,$enddate,$startdate,$checkid);
1.1 raeburn 3327: }
3328: $flushc =
3329: &user_change_result($r,$userresult,$authresult,
3330: $roleresult,\%counts,$flushc,
3331: $username,%userchg);
3332: } else {
3333: if ($context eq 'course') {
3334: $r->print('<br />'.
3335: &mt('<b>[_1]</b>: Unable to enroll. No password specified.',$username)
3336: );
1.13 raeburn 3337: } elsif ($context eq 'author') {
1.1 raeburn 3338: $r->print('<br />'.
3339: &mt('<b>[_1]</b>: Unable to add co-author. No password specified.',$username)
3340: );
3341: } else {
3342: $r->print('<br />'.
3343: &mt('<b>[_1]</b>: Unable to add user. No password specified.',$username)
3344: );
3345: }
3346: }
3347: }
3348: }
3349: } # end of foreach (@userdata)
3350: # Flush the course logs so reverse user roles immediately updated
1.5 raeburn 3351: &Apache::lonnet::flushcourselogs();
1.1 raeburn 3352: $r->print("</p>\n<p>\n".&mt('Processed [_1] user(s).',$counts{'user'}).
3353: "</p>\n");
3354: if ($counts{'role'} > 0) {
3355: $r->print("<p>\n".
3356: &mt('Roles added for [_1] users. If user is active, the new role will be available when the user next logs in to LON-CAPA.',$counts{'role'})."</p>\n");
3357: }
3358: if ($counts{'auth'} > 0) {
3359: $r->print("<p>\n".
3360: &mt('Authentication changed for [_1] existing users.',
3361: $counts{'auth'})."</p>\n");
3362: }
1.13 raeburn 3363: $r->print(&print_namespacing_alerts($domain,\%alerts,\%curr_rules));
1.1 raeburn 3364: $r->print('<form name="uploadresult" action="/adm/createuser">');
3365: $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','prevphase','currstate']));
3366: $r->print('</form>');
3367: #####################################
3368: # Drop students #
3369: #####################################
3370: if ($env{'form.fullup'} eq 'yes') {
3371: $r->print('<h3>'.&mt('Dropping Students')."</h3>\n");
3372: # Get current classlist
3373: my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
3374: if (! defined($classlist)) {
3375: $r->print(&mt('There are no students currently enrolled.').
3376: "\n");
3377: } else {
3378: # Remove the students we just added from the list of students.
3379: foreach (@userdata) {
3380: my %entries=&Apache::loncommon::record_sep($_);
3381: unless (($entries{$fields{'username'}} eq '') ||
3382: (!defined($entries{$fields{'username'}}))) {
3383: delete($classlist->{$entries{$fields{'username'}}.
3384: ':'.$domain});
3385: }
3386: }
3387: # Print out list of dropped students.
3388: &show_drop_list($r,$classlist,$keylist,'nosort');
3389: }
3390: }
3391: } # end of unless
3392: }
3393:
1.13 raeburn 3394: sub print_namespacing_alerts {
3395: my ($domain,$alerts,$curr_rules) = @_;
3396: my $output;
3397: if (ref($alerts) eq 'HASH') {
3398: if (keys(%{$alerts}) > 0) {
3399: if (ref($alerts->{'username'}) eq 'HASH') {
3400: foreach my $dom (sort(keys(%{$alerts->{'username'}}))) {
3401: my $count;
3402: if (ref($alerts->{'username'}{$dom}) eq 'HASH') {
3403: $count = keys(%{$alerts->{'username'}{$dom}});
3404: }
3405: my $domdesc = &Apache::lonnet::domain($domain,'description');
3406: if (ref($curr_rules->{$dom}) eq 'HASH') {
3407: $output .= &Apache::loncommon::instrule_disallow_msg(
3408: 'username',$domdesc,$count,'upload');
3409: }
3410: $output .= &Apache::loncommon::user_rule_formats($dom,
3411: $domdesc,$curr_rules->{$dom}{'username'},
3412: 'username');
3413: }
3414: }
3415: if (ref($alerts->{'id'}) eq 'HASH') {
3416: foreach my $dom (sort(keys(%{$alerts->{'id'}}))) {
3417: my $count;
3418: if (ref($alerts->{'id'}{$dom}) eq 'HASH') {
3419: $count = keys(%{$alerts->{'id'}{$dom}});
3420: }
3421: my $domdesc = &Apache::lonnet::domain($domain,'description');
3422: if (ref($curr_rules->{$dom}) eq 'HASH') {
3423: $output .= &Apache::loncommon::instrule_disallow_msg(
3424: 'id',$domdesc,$count,'upload');
3425: }
3426: $output .= &Apache::loncommon::user_rule_formats($dom,
3427: $domdesc,$curr_rules->{$dom}{'id'},'id');
3428: }
3429: }
3430: }
3431: }
3432: }
3433:
1.1 raeburn 3434: sub user_change_result {
3435: my ($r,$userresult,$authresult,$roleresult,$counts,$flushc,$username,
3436: $userchg) = @_;
3437: my $okresult = 0;
3438: if ($userresult ne 'ok') {
3439: if ($userresult =~ /^error:(.+)$/) {
3440: my $error = $1;
3441: $r->print('<br />'.
3442: &mt('<b>[_1]</b>: Unable to add/modify: [_2]',$username,$error));
3443: }
3444: } else {
3445: $counts->{'user'} ++;
3446: $okresult = 1;
3447: }
3448: if ($authresult ne 'ok') {
3449: if ($authresult =~ /^error:(.+)$/) {
3450: my $error = $1;
3451: $r->print('<br />'.
3452: &mt('<b>[_1]</b>: Unable to modify authentication: [_2]',$username,$error));
3453: }
3454: } else {
3455: $counts->{'auth'} ++;
3456: $okresult = 1;
3457: }
3458: if ($roleresult ne 'ok') {
3459: if ($roleresult =~ /^error:(.+)$/) {
3460: my $error = $1;
3461: $r->print('<br />'.
3462: &mt('<b>[_1]</b>: Unable to add role: [_2]',$username,$error));
3463: }
3464: } else {
3465: $counts->{'role'} ++;
3466: $okresult = 1;
3467: }
3468: if ($okresult) {
3469: $flushc++;
3470: $userchg->{$username}=1;
3471: $r->print('. ');
3472: if ($flushc>15) {
3473: $r->rflush;
3474: $flushc=0;
3475: }
3476: }
3477: return $flushc;
3478: }
3479:
3480: # ========================================================= Menu Phase Two Drop
1.17 raeburn 3481: sub print_drop_menu {
3482: my ($r,$context,$permission) = @_;
3483: $r->print('<h3>'.&mt("Drop Students").'</h3>'."\n".
3484: '<form name="studentform" method="post">'."\n");
1.1 raeburn 3485: my $cid=$env{'request.course.id'};
3486: my ($classlist,$keylist) = &Apache::loncoursedata::get_classlist();
3487: if (! defined($classlist)) {
3488: $r->print(&mt('There are no students currently enrolled.')."\n");
3489: return;
3490: }
3491: # Print out the available choices
1.17 raeburn 3492: &show_drop_list($r,$classlist,$keylist,$permission);
3493: $r->print('</form>'. &Apache::loncommon::end_page());
1.1 raeburn 3494: return;
3495: }
3496:
3497: # ================================================================== Phase four
3498:
1.11 raeburn 3499: sub update_user_list {
3500: my ($r,$context,$setting,$choice) = @_;
3501: my $now = time;
1.1 raeburn 3502: my $count=0;
1.11 raeburn 3503: my @changelist;
3504: if ($choice ne '') {
3505: @changelist = &Apache::loncommon::get_env_multiple('form.actionlist');
3506: } else {
3507: @changelist = &Apache::loncommon::get_env_multiple('form.droplist');
3508: }
3509: my %result_text = ( ok => { 'revoke' => 'Revoked',
3510: 'delete' => 'Deleted',
3511: 'reenable' => 'Re-enabled',
1.17 raeburn 3512: 'activate' => 'Activated',
3513: 'chgdates' => 'Changed Access Dates for',
3514: 'chgsec' => 'Changed section for',
3515: 'drop' => 'Dropped',
1.11 raeburn 3516: },
3517: error => {'revoke' => 'revoking',
3518: 'delete' => 'deleting',
3519: 'reenable' => 're-enabling',
3520: 'activate' => 'activating',
1.17 raeburn 3521: 'chgdates' => 'changing access dates for',
3522: 'chgsec' => 'changing section for',
3523: 'drop' => 'dropping',
1.11 raeburn 3524: },
3525: );
3526: my ($startdate,$enddate);
3527: if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
3528: ($startdate,$enddate) = &get_dates_from_form();
3529: }
3530: foreach my $item (@changelist) {
3531: my ($role,$uname,$udom,$cid,$sec,$scope,$result,$type,$locktype,@sections,
3532: $scopestem);
1.17 raeburn 3533: if ($choice eq 'drop') {
3534: ($uname,$udom,$sec) = split(/:/,$item,-1);
3535: $role = 'st';
3536: $cid = $env{'request.course.id'};
3537: $scopestem = '/'.$cid;
3538: $scopestem =~s/\_/\//g;
3539: if ($sec eq '') {
3540: $scope = $scopestem;
3541: } else {
3542: $scope = $scopestem.'/'.$sec;
3543: }
3544: } elsif ($context eq 'course') {
1.11 raeburn 3545: ($uname,$udom,$role,$sec,$type,$locktype) = split(/\:/,$item,-1);
3546: $cid = $env{'request.course.id'};
3547: $scopestem = '/'.$cid;
3548: $scopestem =~s/\_/\//g;
3549: if ($sec eq '') {
3550: $scope = $scopestem;
3551: } else {
3552: $scope = $scopestem.'/'.$sec;
3553: }
1.13 raeburn 3554: } elsif ($context eq 'author') {
1.11 raeburn 3555: ($uname,$udom,$role) = split(/\:/,$item,-1);
3556: $scope = '/'.$env{'user.domain'}.'/'.$env{'user.name'};
3557: } elsif ($context eq 'domain') {
3558: if ($setting eq 'domain') {
3559: ($role,$uname,$udom) = split(/\:/,$item,-1);
3560: $scope = '/'.$env{'request.role.domain'}.'/';
1.13 raeburn 3561: } elsif ($setting eq 'author') {
1.11 raeburn 3562: ($uname,$udom,$role,$scope) = split(/\:/,$item);
3563: } elsif ($setting eq 'course') {
3564: ($uname,$udom,$role,$cid,$sec,$type,$locktype) =
3565: split(/\:/,$item);
3566: $scope = '/'.$cid;
3567: $scope =~s/\_/\//g;
3568: if ($sec ne '') {
3569: $scope .= '/'.$sec;
3570: }
3571: }
3572: }
3573: my $plrole = &Apache::lonnet::plaintext($role);
3574: my ($uid,$first,$middle,$last,$gene,$sec);
3575: my $start = $env{'form.'.$item.'_start'};
3576: my $end = $env{'form.'.$item.'_end'};
1.17 raeburn 3577: if ($choice eq 'drop') {
3578: # drop students
3579: $end = $now;
3580: $type = 'manual';
3581: $result =
3582: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
3583: } elsif ($choice eq 'revoke') {
3584: # revoke or delete user role
1.11 raeburn 3585: $end = $now;
3586: if ($role eq 'st') {
3587: $result =
3588: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
3589: } else {
3590: $result =
3591: &Apache::lonnet::revokerole($udom,$uname,$scope,$role);
3592: }
3593: } elsif ($choice eq 'delete') {
3594: $start = -1;
3595: $end = -1;
3596: if ($role eq 'st') {
3597: # FIXME - how does role deletion affect classlist?
3598: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
3599: } else {
3600: $result =
3601: &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$now,
3602: 0,1);
3603: }
3604: } else {
3605: #reenable, activate, change access dates or change section
3606: if ($choice ne 'chgsec') {
3607: $start = $startdate;
3608: $end = $enddate;
3609: }
3610: if ($choice eq 'reenable') {
3611: if ($role eq 'st') {
3612: $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
3613: } else {
3614: $result =
3615: &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
3616: $now);
3617: }
3618: } elsif ($choice eq 'activate') {
3619: if ($role eq 'st') {
3620: $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
3621: } else {
3622: $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
3623: $now);
3624: }
3625: } elsif ($choice eq 'chgdates') {
3626: if ($role eq 'st') {
3627: $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
3628: } else {
3629: $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
3630: $start);
3631: }
3632: } elsif ($choice eq 'chgsec') {
3633: my (@newsecs,$revresult,$nochg,@retained);
3634: if ($role ne 'cc') {
3635: @newsecs = split(/,/,$env{'form.newsecs'});
3636: }
3637: # remove existing section if not to be retained.
3638: if (!$env{'form.retainsec'}) {
3639: if ($sec eq '') {
3640: if (@newsecs == 0) {
3641: $result = &mt('No change in section assignment (none)');
3642: $nochg = 1;
3643: }
3644: } else {
3645: if (!grep(/^\Q$sec\E$/,@newsecs)) {
3646: $revresult =
3647: &Apache::lonnet::revokerole($udom,$uname,$scope,$role);
3648: } else {
3649: push(@retained,$sec);
3650: }
3651: }
3652: } else {
3653: push(@retained,$sec);
3654: }
3655: # add new sections
3656: if (@newsecs == 0) {
3657: if (!$nochg) {
3658: if ($sec ne '') {
3659: if ($role eq 'st') {
3660: $result =
3661: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,undef,$end,$start,$type,$locktype,$cid);
3662: } else {
3663: my $newscope = $scopestem;
3664: $result = &Apache::lonnet::assignrole($udom,$uname,$newscope,$role,$end,$start);
3665: }
3666: }
3667: }
3668: } else {
3669: foreach my $newsec (@newsecs) {
3670: if (!grep(/^\Q$newsec\E$/,@retained)) {
3671: if ($role eq 'st') {
3672: $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$newsec,$end,$start,$type,$locktype,$cid);
3673: } else {
3674: my $newscope = $scopestem;
3675: if ($newsec ne '') {
3676: $newscope .= '/'.$newsec;
3677: }
3678: $result = &Apache::lonnet::assignrole($udom,$uname,
3679: $newscope,$role,$end,$start);
3680: }
3681: }
3682: }
3683: }
3684: }
3685: }
1.17 raeburn 3686: my $extent = $scope;
3687: if ($choice eq 'drop' || $context eq 'course') {
3688: my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
3689: if ($cdesc) {
3690: $extent = $cdesc;
3691: }
3692: }
1.1 raeburn 3693: if ($result eq 'ok' || $result eq 'ok:') {
1.11 raeburn 3694: $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for [_3]",
1.17 raeburn 3695: $plrole,$extent,$uname.':'.$udom).'<br />');
1.1 raeburn 3696: $count++;
3697: } else {
3698: $r->print(
1.11 raeburn 3699: &mt("Error $result_text{'error'}{$choice} [_1] in [_2] for [_3]:[_4]",
1.17 raeburn 3700: $plrole,$extent,$uname.':'.$udom,$result).'<br />');
1.11 raeburn 3701: }
3702: }
3703: $r->print('<p><b>'.&mt("$result_text{'ok'}{$choice} role(s) for [quant,_1,user,users,users].",$count).'</b></p>');
3704: if ($count > 0) {
1.17 raeburn 3705: if ($choice eq 'revoke' || $choice eq 'drop') {
1.11 raeburn 3706: $r->print('<p>'.&mt('Re-enabling will re-activate data for the role.</p>'));
3707: }
3708: # Flush the course logs so reverse user roles immediately updated
3709: &Apache::lonnet::flushcourselogs();
3710: }
3711: if ($env{'form.makedatesdefault'}) {
3712: if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
3713: $r->print(&make_dates_default($startdate,$enddate));
1.1 raeburn 3714: }
3715: }
3716: }
3717:
1.8 raeburn 3718: sub classlist_drop {
3719: my ($scope,$uname,$udom,$now,$action) = @_;
3720: my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)});
3721: my $cid=$cdom.'_'.$cnum;
3722: my $user = $uname.':'.$udom;
3723: if ($action eq 'drop') {
3724: if (!&active_student_roles($cnum,$cdom,$uname,$udom)) {
3725: my $result =
3726: &Apache::lonnet::cput('classlist',
3727: { $user => $now },
3728: $env{'course.'.$cid.'.domain'},
3729: $env{'course.'.$cid.'.num'});
3730: return &mt('Drop from classlist: [_1]',
3731: '<b>'.$result.'</b>').'<br />';
3732: }
3733: }
3734: }
3735:
3736: sub active_student_roles {
3737: my ($cnum,$cdom,$uname,$udom) = @_;
3738: my %roles =
3739: &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
3740: ['future','active'],['st']);
3741: return exists($roles{"$cnum:$cdom:st"});
3742: }
3743:
1.1 raeburn 3744: sub section_check_js {
1.8 raeburn 3745: my $groupslist= &get_groupslist();
1.1 raeburn 3746: return <<"END";
3747: function validate(caller) {
1.9 raeburn 3748: var groups = new Array($groupslist);
1.1 raeburn 3749: var secname = caller.value;
3750: if ((secname == 'all') || (secname == 'none')) {
3751: alert("'"+secname+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name.");
3752: return 'error';
3753: }
3754: if (secname != '') {
3755: for (var k=0; k<groups.length; k++) {
3756: if (secname == groups[k]) {
3757: 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.");
3758: return 'error';
3759: }
3760: }
3761: }
3762: return 'ok';
3763: }
3764: END
3765: }
3766:
3767: sub set_login {
3768: my ($dom,$authformkrb,$authformint,$authformloc) = @_;
3769: my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
3770: my $response;
3771: my ($authnum,%can_assign) =
3772: &Apache::loncommon::get_assignable_auth($dom);
3773: if ($authnum) {
3774: $response = &Apache::loncommon::start_data_table();
3775: if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
3776: $response .= &Apache::loncommon::start_data_table_row().
3777: '<td>'.$authformkrb.'</td>'.
3778: &Apache::loncommon::end_data_table_row()."\n";
3779: }
3780: if ($can_assign{'int'}) {
3781: $response .= &Apache::loncommon::start_data_table_row().
3782: '<td>'.$authformint.'</td>'.
3783: &Apache::loncommon::end_data_table_row()."\n"
3784: }
3785: if ($can_assign{'loc'}) {
3786: $response .= &Apache::loncommon::start_data_table_row().
3787: '<td>'.$authformloc.'</td>'.
3788: &Apache::loncommon::end_data_table_row()."\n";
3789: }
3790: $response .= &Apache::loncommon::end_data_table();
3791: }
3792: return $response;
3793: }
3794:
1.8 raeburn 3795: sub course_sections {
3796: my ($sections_count,$role) = @_;
3797: my $output = '';
3798: my @sections = (sort {$a <=> $b} keys %{$sections_count});
3799: if (scalar(@sections) == 1) {
3800: $output = '<select name="currsec_'.$role.'" >'."\n".
3801: ' <option value="">Select</option>'."\n".
3802: ' <option value="">No section</option>'."\n".
3803: ' <option value="'.$sections[0].'" >'.$sections[0].'</option>'."\n";
3804: } else {
3805: $output = '<select name="currsec_'.$role.'" ';
3806: my $multiple = 4;
3807: if (scalar(@sections) < 4) { $multiple = scalar(@sections); }
3808: $output .= 'multiple="multiple" size="'.$multiple.'">'."\n";
3809: foreach my $sec (@sections) {
3810: $output .= '<option value="'.$sec.'">'.$sec."</option>\n";
3811: }
3812: }
3813: $output .= '</select>';
3814: return $output;
3815: }
3816:
3817: sub get_groupslist {
3818: my $groupslist;
3819: my %curr_groups = &Apache::longroup::coursegroups();
3820: if (%curr_groups) {
3821: $groupslist = join('","',sort(keys(%curr_groups)));
3822: $groupslist = '"'.$groupslist.'"';
3823: }
1.11 raeburn 3824: return $groupslist;
1.8 raeburn 3825: }
3826:
3827: sub setsections_javascript {
3828: my ($form,$groupslist) = @_;
3829: my ($checkincluded,$finish,$roleplace,$setsection_js);
3830: if ($form eq 'cu') {
3831: $checkincluded = 'formname.elements[i-1].checked == true';
3832: $finish = 'formname.submit()';
3833: $roleplace = 3;
3834: } else {
1.11 raeburn 3835: $checkincluded = 'formname.name == "'.$form.'"';
1.8 raeburn 3836: $finish = "seccheck = 'ok';";
3837: $roleplace = 1;
1.11 raeburn 3838: $setsection_js = "var seccheck = 'alert';";
1.8 raeburn 3839: }
3840: my %alerts = &Apache::lonlocal::texthash(
3841: secd => 'Section designations do not apply to Course Coordinator roles.',
3842: accr => 'A course coordinator role will be added with access to all sections.',
3843: inea => 'In each course, each user may only have one student role at a time.',
3844: youh => 'You had selected ',
3845: secs => 'sections.',
3846: plmo => 'Please modify your selections so they include no more than one section.',
3847: mayn => 'may not be used as the name for a section, as it is a reserved word.',
3848: plch => 'Please choose a different section name.',
3849: mnot => 'may not be used as a section name, as it is the name of a course group.',
3850: secn => 'Section names and group names must be distinct. Please choose a different section name.',
1.11 raeburn 3851: );
1.8 raeburn 3852: $setsection_js .= <<"ENDSECCODE";
3853:
3854: function setSections(formname) {
3855: var re1 = /^currsec_/;
3856: var groups = new Array($groupslist);
3857: for (var i=0;i<formname.elements.length;i++) {
3858: var str = formname.elements[i].name;
3859: var checkcurr = str.match(re1);
3860: if (checkcurr != null) {
3861: if ($checkincluded) {
3862: var match = str.split('_');
3863: var role = match[$roleplace];
3864: if (role == 'cc') {
3865: alert("$alerts{'secd'}\\n$alerts{'accr'}");
3866: }
3867: else {
3868: var sections = '';
3869: var numsec = 0;
3870: var sections;
3871: for (var j=0; j<formname.elements[i].length; j++) {
3872: if (formname.elements[i].options[j].selected == true ) {
3873: if (formname.elements[i].options[j].value != "") {
3874: if (numsec == 0) {
3875: if (formname.elements[i].options[j].value != "") {
3876: sections = formname.elements[i].options[j].value;
3877: numsec ++;
3878: }
3879: }
3880: else {
3881: sections = sections + "," + formname.elements[i].options[j].value
3882: numsec ++;
3883: }
3884: }
3885: }
3886: }
3887: if (numsec > 0) {
3888: if (formname.elements[i+1].value != "" && formname.elements[i+1].value != null) {
3889: sections = sections + "," + formname.elements[i+1].value;
3890: }
3891: }
3892: else {
3893: sections = formname.elements[i+1].value;
3894: }
3895: var newsecs = formname.elements[i+1].value;
3896: var numsplit;
3897: if (newsecs != null && newsecs != "") {
3898: numsplit = newsecs.split(/,/g);
3899: numsec = numsec + numsplit.length;
3900: }
3901:
3902: if ((role == 'st') && (numsec > 1)) {
3903: alert("$alerts{'inea'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}")
3904: return;
3905: }
3906: else {
3907: if (numsplit != null) {
3908: for (var j=0; j<numsplit.length; j++) {
3909: if ((numsplit[j] == 'all') ||
3910: (numsplit[j] == 'none')) {
3911: alert("'"+numsplit[j]+"' $alerts{'mayn'}\\n$alerts{'plch'}");
3912: return;
3913: }
3914: for (var k=0; k<groups.length; k++) {
3915: if (numsplit[j] == groups[k]) {
3916: alert("'"+numsplit[j]+"' $alerts{'mnot'}\\n$alerts{'secn'}");
3917: return;
3918: }
3919: }
3920: }
3921: }
3922: formname.elements[i+2].value = sections;
3923: }
3924: }
3925: }
3926: }
3927: }
3928: $finish
3929: }
3930: ENDSECCODE
1.11 raeburn 3931: return $setsection_js;
1.8 raeburn 3932: }
3933:
1.15 raeburn 3934: sub can_create_user {
3935: my ($dom,$context,$usertype) = @_;
3936: my %domconf = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
3937: my $cancreate = 1;
3938: if (ref($domconf{'usercreation'}) eq 'HASH') {
3939: if (ref($domconf{'usercreation'}{'cancreate'}) eq 'HASH') {
3940: if ($context eq 'course' || $context eq 'author') {
3941: my $creation = $domconf{'usercreation'}{'cancreate'}{$context};
3942: if ($creation eq 'none') {
3943: $cancreate = 0;
3944: } elsif ($creation ne 'any') {
3945: if (defined($usertype)) {
3946: if ($creation ne $usertype) {
3947: $cancreate = 0;
3948: }
3949: }
3950: }
3951: }
3952: }
3953: }
3954: return $cancreate;
3955: }
3956:
1.20 raeburn 3957: sub can_modify_userinfo {
3958: my ($context,$dom,$fields,$userroles) = @_;
3959: my %domconfig =
3960: &Apache::lonnet::get_dom('configuration',['usermodification'],
3961: $dom);
3962: my %canmodify;
3963: if (ref($fields) eq 'ARRAY') {
3964: foreach my $field (@{$fields}) {
3965: $canmodify{$field} = 0;
3966: if (&Apache::lonnet::allowed('mau',$dom)) {
3967: $canmodify{$field} = 1;
3968: } else {
3969: if (ref($domconfig{'usermodification'}) eq 'HASH') {
3970: if (ref($domconfig{'usermodification'}{$context}) eq 'HASH') {
3971: if (ref($userroles) eq 'ARRAY') {
3972: foreach my $role (@{$userroles}) {
3973: my $testrole;
3974: if ($role =~ /^cr\//) {
3975: $testrole = 'cr';
3976: } else {
3977: $testrole = $role;
3978: }
3979: if (ref($domconfig{'usermodification'}{$context}{$testrole}) eq 'HASH') {
3980: if ($domconfig{'usermodification'}{$context}{$testrole}{$field}) {
3981: $canmodify{$field} = 1;
3982: last;
3983: }
3984: }
3985: }
3986: } else {
3987: foreach my $key (keys(%{$domconfig{'usermodification'}{$context}})) {
3988: if (ref($domconfig{'usermodification'}{$context}{$key}) eq 'HASH') {
3989: if ($domconfig{'usermodification'}{$context}{$key}{$field}) {
3990: $canmodify{$field} = 1;
3991: last;
3992: }
3993: }
3994: }
3995: }
3996: }
3997: } elsif ($context eq 'course') {
3998: if (ref($userroles) eq 'ARRAY') {
3999: if (grep(/^st$/,@{$userroles})) {
4000: $canmodify{$field} = 1;
4001: }
4002: } else {
4003: $canmodify{$field} = 1;
4004: }
4005: }
4006: }
4007: }
4008: }
4009: return %canmodify;
4010: }
4011:
1.18 raeburn 4012: sub check_usertype {
4013: my ($dom,$uname,$rules) = @_;
4014: my $usertype;
4015: if (ref($rules) eq 'HASH') {
4016: my @user_rules = keys(%{$rules});
4017: if (@user_rules > 0) {
4018: my %rule_check = &Apache::lonnet::inst_rulecheck($dom,$uname,undef,'username',\@user_rules);
4019: if (keys(%rule_check) > 0) {
4020: $usertype = 'unofficial';
4021: foreach my $item (keys(%rule_check)) {
4022: if ($rule_check{$item}) {
4023: $usertype = 'official';
4024: last;
4025: }
4026: }
4027: }
4028: }
4029: }
4030: return $usertype;
4031: }
4032:
1.17 raeburn 4033: sub roles_by_context {
4034: my ($context,$custom) = @_;
4035: my @allroles;
4036: if ($context eq 'course') {
4037: @allroles = ('st','ad','ta','ep','in','cc');
4038: if ($custom) {
4039: push(@allroles,'cr');
4040: }
4041: } elsif ($context eq 'author') {
4042: @allroles = ('ca','aa');
4043: } elsif ($context eq 'domain') {
4044: @allroles = ('li','dg','sc','au','dc');
4045: }
4046: return @allroles;
4047: }
4048:
1.16 raeburn 4049: sub get_permission {
1.17 raeburn 4050: my ($context,$roles) = @_;
1.16 raeburn 4051: my %permission;
4052: if ($context eq 'course') {
1.17 raeburn 4053: my $custom = 1;
4054: my @allroles = &roles_by_context($context,$custom);
4055: foreach my $role (@allroles) {
4056: if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
4057: $permission{'cusr'} = 1;
4058: last;
4059: }
1.16 raeburn 4060: }
4061: if (&Apache::lonnet::allowed('ccr',$env{'request.course.id'})) {
4062: $permission{'custom'} = 1;
4063: }
4064: if (&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) {
4065: $permission{'view'} = 1;
4066: }
4067: if (!$permission{'view'}) {
4068: my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
4069: $permission{'view'} = &Apache::lonnet::allowed('vcl',$scope);
4070: if ($permission{'view'}) {
4071: $permission{'view_section'} = $env{'request.course.sec'};
4072: }
4073: }
1.17 raeburn 4074: if (!$permission{'cusr'}) {
4075: if ($env{'request.course.sec'} ne '') {
4076: my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
4077: $permission{'cusr'} = (&Apache::lonnet::allowed('cst',$scope));
4078: if ($permission{'cusr'}) {
4079: $permission{'cusr_section'} = $env{'request.course.sec'};
4080: }
4081: }
4082: }
1.16 raeburn 4083: if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
4084: $permission{'grp_manage'} = 1;
4085: }
4086: } elsif ($context eq 'author') {
4087: $permission{'cusr'} = &authorpriv($env{'user.name'},$env{'request.role.domain'});
4088: $permission{'view'} = $permission{'cusr'};
4089: } else {
1.17 raeburn 4090: my @allroles = &roles_by_context($context);
4091: foreach my $role (@allroles) {
4092: if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) { $permission{'cusr'} = 1;
4093: last;
4094: }
4095: }
4096: if (!$permission{'cusr'}) {
4097: if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
4098: $permission{'cusr'} = 1;
4099: }
1.16 raeburn 4100: }
4101: if (&Apache::lonnet::allowed('ccr',$env{'request.role.domain'})) {
4102: $permission{'custom'} = 1;
4103: }
4104: $permission{'view'} = $permission{'cusr'};
4105: }
4106: my $allowed = 0;
4107: foreach my $perm (values(%permission)) {
4108: if ($perm) { $allowed=1; last; }
4109: }
4110: return (\%permission,$allowed);
4111: }
4112:
4113: # ==================================================== Figure out author access
4114:
4115: sub authorpriv {
4116: my ($auname,$audom)=@_;
4117: unless ((&Apache::lonnet::allowed('cca',$audom.'/'.$auname))
4118: || (&Apache::lonnet::allowed('caa',$audom.'/'.$auname))) { return ''; } return 1;
4119: }
4120:
1.17 raeburn 4121: sub get_course_identity {
4122: my ($cid) = @_;
4123: my ($cnum,$cdom,$cdesc);
4124: if ($cid eq '') {
4125: $cid = $env{'request.course.id'}
4126: }
4127: if ($cid ne '') {
4128: $cnum = $env{'course.'.$cid.'.num'};
4129: $cdom = $env{'course.'.$cid.'.domain'};
4130: $cdesc = $env{'course.'.$cid.'.description'};
4131: if ($cnum eq '' || $cdom eq '') {
4132: my %coursehash =
4133: &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
4134: $cdom = $coursehash{'domain'};
4135: $cnum = $coursehash{'num'};
4136: $cdesc = $coursehash{'description'};
4137: }
4138: }
4139: return ($cnum,$cdom,$cdesc);
4140: }
4141:
1.19 raeburn 4142: sub dc_setcourse_js {
4143: my ($formname,$mode) = @_;
4144: my $dc_setcourse_code;
4145: my $cctext = &Apache::lonnet::plaintext('cc');
4146: my %alerts = §ioncheck_alerts();
4147: my $role = 'role';
4148: if ($mode eq 'upload') {
4149: $role = 'courserole';
4150: }
4151: $dc_setcourse_code = (<<"SCRIPTTOP");
4152: function setCourse() {
4153: var course = document.$formname.dccourse.value;
4154: if (course != "") {
4155: if (document.$formname.dcdomain.value != document.$formname.origdom.value) {
4156: alert("$alerts{'curd'}");
4157: return;
4158: }
4159: var userrole = document.$formname.$role.options[document.$formname.$role.selectedIndex].value
4160: var section="";
4161: var numsections = 0;
4162: var newsecs = new Array();
4163: for (var i=0; i<document.$formname.currsec.length; i++) {
4164: if (document.$formname.currsec.options[i].selected == true ) {
4165: if (document.$formname.currsec.options[i].value != "" && document.$formname.currsec.options[i].value != null) {
4166: if (numsections == 0) {
4167: section = document.$formname.currsec.options[i].value
4168: numsections = 1;
4169: }
4170: else {
4171: section = section + "," + document.$formname.currsec.options[i].value
4172: numsections ++;
4173: }
4174: }
4175: }
4176: }
4177: if (document.$formname.newsec.value != "" && document.$formname.newsec.value != null) {
4178: if (numsections == 0) {
4179: section = document.$formname.newsec.value
4180: }
4181: else {
4182: section = section + "," + document.$formname.newsec.value
4183: }
4184: newsecs = document.$formname.newsec.value.split(/,/g);
4185: numsections = numsections + newsecs.length;
4186: }
4187: if ((userrole == 'st') && (numsections > 1)) {
4188: alert("$alerts{'inea'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
4189: return;
4190: }
4191: for (var j=0; j<newsecs.length; j++) {
4192: if ((newsecs[j] == 'all') || (newsecs[j] == 'none')) {
4193: alert("'"+newsecs[j]+"' $alerts{'mayn'}.\\n$alerts{'plsc'}.");
4194: return;
4195: }
4196: if (document.$formname.groups.value != '') {
4197: var groups = document.$formname.groups.value.split(/,/g);
4198: for (var k=0; k<groups.length; k++) {
4199: if (newsecs[j] == groups[k]) {
4200: alert("'"+newsecs[j]+"' $alerts{'mayt'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
4201: return;
4202: }
4203: }
4204: }
4205: }
4206: if ((userrole == 'cc') && (numsections > 0)) {
4207: alert("$alerts{'secd'} $cctext $alerts{'role'}.\\n$alerts{'accr'}.");
4208: section = "";
4209: }
4210: SCRIPTTOP
4211: if ($mode ne 'upload') {
4212: $dc_setcourse_code .= (<<"ENDSCRIPT");
4213: var coursename = "_$env{'request.role.domain'}"+"_"+course+"_"+userrole
4214: var numcourse = getIndex(document.$formname.dccourse);
4215: if (numcourse == "-1") {
4216: alert("$alerts{'thwa'}");
4217: return;
4218: }
4219: else {
4220: document.$formname.elements[numcourse].name = "act"+coursename;
4221: var numnewsec = getIndex(document.$formname.newsec);
4222: if (numnewsec != "-1") {
4223: document.$formname.elements[numnewsec].name = "sec"+coursename;
4224: document.$formname.elements[numnewsec].value = section;
4225: }
4226: var numstart = getIndex(document.$formname.start);
4227: if (numstart != "-1") {
4228: document.$formname.elements[numstart].name = "start"+coursename;
4229: }
4230: var numend = getIndex(document.$formname.end);
4231: if (numend != "-1") {
4232: document.$formname.elements[numend].name = "end"+coursename
4233: }
4234: }
4235: }
4236: document.$formname.submit();
4237: }
4238:
4239: ENDSCRIPT
4240: } else {
4241: $dc_setcourse_code .= "
4242: document.$formname.sections.value = section;
4243: }
4244: return 'ok';
4245: }
4246: ";
4247: }
4248: $dc_setcourse_code .= (<<"ENDSCRIPT");
4249:
4250: function getIndex(caller) {
4251: for (var i=0;i<document.$formname.elements.length;i++) {
4252: if (document.$formname.elements[i] == caller) {
4253: return i;
4254: }
4255: }
4256: return -1;
4257: }
4258: ENDSCRIPT
4259: }
4260:
4261: sub sectioncheck_alerts {
4262: my %alerts = &Apache::lonlocal::texthash(
4263: curd => 'You must select a course in the current domain',
4264: inea => 'In each course, each user may only have one student role at a time',
4265: youh => 'You had selected',
4266: sect => 'sections',
4267: plsm => 'Please modify your selections so they include no more than one section',
4268: mayn => 'may not be used as the name for a section, as it is a reserved word',
4269: plsc => 'Please choose a different section name',
4270: mayt => 'may not be used as the name for a section, as it is the name of a course group',
4271: secn => 'Section names and group names must be distinct',
4272: secd => 'Section designations do not apply to ',
4273: role => 'roles',
4274: accr => 'role will be added with access to all sections',
4275: thwa => 'There was a problem with your course selection'
4276: );
4277: return %alerts;
4278: }
1.17 raeburn 4279:
4280:
1.1 raeburn 4281: 1;
4282:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>