Annotation of loncom/interface/lonuserutils.pm, revision 1.24
1.1 raeburn 1: # The LearningOnline Network with CAPA
2: # Utility functions for managing LON-CAPA user accounts
3: #
1.24 ! raeburn 4: # $Id: lonuserutils.pm,v 1.23 2007/12/22 00:47:25 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".
741: &mt('Choose the role to assign to users without one specified in the uploaded file');
742: } elsif ($context eq 'course') {
743: $Str .= '<h3>'.&mt('Default role and section')."</h3>\n".
744: &mt('Choose the role and/or section to assign to users without one specified in the uploaded file');
745: } else {
746: $Str .= '<br /><br /><b>'.&mt('Default role and/or section')."</b><br />\n".
747: &mt('Role and/or section for users without one in the uploaded file.');
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".
789: &mt('(only do if you know what you are doing.)')."</br><br />\n";
790: if ($context eq 'domain') {
791: $output .= '<label><input type="checkbox" name="recurseid"'.
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.5 raeburn 794: '</label></p>'."\n";
795: }
796: return $output;
797: }
798:
1.1 raeburn 799: ###############################################################
800: ###############################################################
801: sub print_upload_manager_form {
1.21 raeburn 802: my ($r,$context,$permission) = @_;
1.1 raeburn 803: my $firstLine;
804: my $datatoken;
805: if (!$env{'form.datatoken'}) {
806: $datatoken=&Apache::loncommon::upfile_store($r);
807: } else {
808: $datatoken=$env{'form.datatoken'};
809: &Apache::loncommon::load_tmp_file($r);
810: }
811: my @records=&Apache::loncommon::upfile_record_sep();
812: if($env{'form.noFirstLine'}){
813: $firstLine=shift(@records);
814: }
815: my $total=$#records;
816: my $distotal=$total+1;
817: my $today=time;
818: my $halfyear=$today+15552000;
819: #
820: # Restore memorized settings
821: my $col_setting_names = { 'username_choice' => 'scalar', # column settings
822: 'names_choice' => 'scalar',
823: 'fname_choice' => 'scalar',
824: 'mname_choice' => 'scalar',
825: 'lname_choice' => 'scalar',
826: 'gen_choice' => 'scalar',
827: 'id_choice' => 'scalar',
828: 'sec_choice' => 'scalar',
829: 'ipwd_choice' => 'scalar',
830: 'email_choice' => 'scalar',
831: 'role_choice' => 'scalar',
832: };
833: my $defdom = $env{'request.role.domain'};
834: if ($context eq 'course') {
835: &Apache::loncommon::restore_course_settings('enrollment_upload',
836: $col_setting_names);
837: } else {
838: &Apache::loncommon::restore_settings($context,'user_upload',
839: $col_setting_names);
840: }
841: #
842: # Determine kerberos parameters as appropriate
843: my ($krbdef,$krbdefdom) =
844: &Apache::loncommon::get_kerberos_defaults($defdom);
845: #
1.22 raeburn 846: &print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom,$context,
847: $permission);
1.1 raeburn 848: my $i;
849: my $keyfields;
850: if ($total>=0) {
851: my @field=
852: (['username',&mt('Username'), $env{'form.username_choice'}],
853: ['names',&mt('Last Name, First Names'),$env{'form.names_choice'}],
854: ['fname',&mt('First Name'), $env{'form.fname_choice'}],
855: ['mname',&mt('Middle Names/Initials'),$env{'form.mname_choice'}],
856: ['lname',&mt('Last Name'), $env{'form.lname_choice'}],
857: ['gen', &mt('Generation'), $env{'form.gen_choice'}],
858: ['id', &mt('ID/Student Number'),$env{'form.id_choice'}],
859: ['sec', &mt('Section'), $env{'form.sec_choice'}],
860: ['ipwd', &mt('Initial Password'),$env{'form.ipwd_choice'}],
861: ['email',&mt('E-mail Address'), $env{'form.email_choice'}],
862: ['role',&mt('Role'), $env{'form.role_choice'}]);
863: if ($env{'form.upfile_associate'} eq 'reverse') {
864: &Apache::loncommon::csv_print_samples($r,\@records);
865: $i=&Apache::loncommon::csv_print_select_table($r,\@records,
866: \@field);
867: foreach (@field) {
868: $keyfields.=$_->[0].',';
869: }
870: chop($keyfields);
871: } else {
872: unshift(@field,['none','']);
873: $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
874: \@field);
875: my %sone=&Apache::loncommon::record_sep($records[0]);
876: $keyfields=join(',',sort(keys(%sone)));
877: }
878: }
879: $r->print('</div>');
880: &print_upload_manager_footer($r,$i,$keyfields,$defdom,$today,$halfyear,
1.22 raeburn 881: $context,$permission);
1.1 raeburn 882: }
883:
884: sub setup_date_selectors {
1.22 raeburn 885: my ($starttime,$endtime,$mode,$nolink,$formname) = @_;
886: if ($formname eq '') {
887: $formname = 'studentform';
888: }
1.1 raeburn 889: if (! defined($starttime)) {
890: $starttime = time;
891: unless ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
892: if (exists($env{'course.'.$env{'request.course.id'}.
893: '.default_enrollment_start_date'})) {
894: $starttime = $env{'course.'.$env{'request.course.id'}.
895: '.default_enrollment_start_date'};
896: }
897: }
898: }
899: if (! defined($endtime)) {
900: $endtime = time+(6*30*24*60*60); # 6 months from now, approx
901: unless ($mode eq 'createcourse') {
902: if (exists($env{'course.'.$env{'request.course.id'}.
903: '.default_enrollment_end_date'})) {
904: $endtime = $env{'course.'.$env{'request.course.id'}.
905: '.default_enrollment_end_date'};
906: }
907: }
908: }
1.11 raeburn 909:
910: my $startdateform =
1.22 raeburn 911: &Apache::lonhtmlcommon::date_setter($formname,'startdate',$starttime,
1.11 raeburn 912: undef,undef,undef,undef,undef,undef,undef,$nolink);
913:
914: my $enddateform =
1.22 raeburn 915: &Apache::lonhtmlcommon::date_setter($formname,'enddate',$endtime,
1.11 raeburn 916: undef,undef,undef,undef,undef,undef,undef,$nolink);
917:
1.1 raeburn 918: if ($mode eq 'create_enrolldates') {
919: $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
920: 'startenroll',
921: $starttime);
922: $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
923: 'endenroll',
924: $endtime);
925: }
926: if ($mode eq 'create_defaultdates') {
927: $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
928: 'startaccess',
929: $starttime);
930: $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
931: 'endaccess',
932: $endtime);
933: }
934: return ($startdateform,$enddateform);
935: }
936:
937:
938: sub get_dates_from_form {
939: my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate');
940: my $enddate = &Apache::lonhtmlcommon::get_date_from_form('enddate');
941: if ($env{'form.no_end_date'}) {
942: $enddate = 0;
943: }
944: return ($startdate,$enddate);
945: }
946:
947: sub date_setting_table {
1.22 raeburn 948: my ($starttime,$endtime,$mode,$bulkaction,$formname,$permission) = @_;
1.11 raeburn 949: my $nolink;
950: if ($bulkaction) {
951: $nolink = 1;
952: }
953: my ($startform,$endform) =
1.22 raeburn 954: &setup_date_selectors($starttime,$endtime,$mode,$nolink,$formname);
1.1 raeburn 955: my $dateDefault;
956: if ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
957: $dateDefault = ' ';
1.13 raeburn 958: } elsif ($mode ne 'author' && $mode ne 'domain') {
1.11 raeburn 959: if (($bulkaction eq 'reenable') ||
960: ($bulkaction eq 'activate') ||
1.22 raeburn 961: ($bulkaction eq 'chgdates') ||
962: ($env{'form.action'} eq 'upload')) {
963: if ($env{'request.course.sec'} eq '') {
964: $dateDefault = '<span class="LC_nobreak">'.
965: '<label><input type="checkbox" name="makedatesdefault" /> '.
966: &mt('make these dates the default access dates for future student enrollment').
967: '</label></span>';
968: }
1.11 raeburn 969: }
1.1 raeburn 970: }
1.11 raeburn 971: my $perpetual = '<span class="LC_nobreak"><label><input type="checkbox" name="no_end_date"';
1.1 raeburn 972: if (defined($endtime) && $endtime == 0) {
973: $perpetual .= ' checked';
974: }
1.11 raeburn 975: $perpetual.= ' /> '.&mt('no ending date').'</label></span>';
1.1 raeburn 976: if ($mode eq 'create_enrolldates') {
977: $perpetual = ' ';
978: }
1.11 raeburn 979: my $result = &Apache::lonhtmlcommon::start_pick_box()."\n";
980: $result .= &Apache::lonhtmlcommon::row_title(&mt('Starting Date'),
981: 'LC_oddrow_value')."\n".
982: $startform."\n".
983: &Apache::lonhtmlcommon::row_closure(1).
984: &Apache::lonhtmlcommon::row_title(&mt('Ending Date'),
985: 'LC_oddrow_value')."\n".
986: $endform.' '.$perpetual.
987: &Apache::lonhtmlcommon::row_closure(1).
1.22 raeburn 988: &Apache::lonhtmlcommon::end_pick_box();
1.1 raeburn 989: if ($dateDefault) {
990: $result .= $dateDefault.'<br />'."\n";
991: }
992: return $result;
993: }
994:
995: sub make_dates_default {
996: my ($startdate,$enddate,$context) = @_;
997: my $result = '';
998: if ($context eq 'course') {
1.17 raeburn 999: my ($cnum,$cdom) = &get_course_identity();
1.1 raeburn 1000: my $put_result = &Apache::lonnet::put('environment',
1001: {'default_enrollment_start_date'=>$startdate,
1.17 raeburn 1002: 'default_enrollment_end_date' =>$enddate},$cdom,$cnum);
1.1 raeburn 1003: if ($put_result eq 'ok') {
1.11 raeburn 1004: $result .= &mt('Set default start and end dates for course').
1005: '<br />'."\n";
1.1 raeburn 1006: #
1007: # Refresh the course environment
1008: &Apache::lonnet::coursedescription($env{'request.course.id'},
1009: {'freshen_cache' => 1});
1010: } else {
1011: $result .= &mt('Unable to set default dates for course').":".$put_result.
1012: '<br />';
1013: }
1014: }
1015: return $result;
1016: }
1017:
1018: sub default_role_selector {
1.2 raeburn 1019: my ($context,$checkpriv) = @_;
1.1 raeburn 1020: my %customroles;
1021: my ($options,$coursepick,$cb_jscript);
1.13 raeburn 1022: if ($context ne 'author') {
1.1 raeburn 1023: %customroles = &my_custom_roles();
1024: }
1025:
1026: my %lt=&Apache::lonlocal::texthash(
1027: 'rol' => "Role",
1028: 'grs' => "Section",
1029: 'exs' => "Existing sections",
1030: 'new' => "New section",
1031: );
1032: $options = '<select name="defaultrole">'."\n".
1033: ' <option value="">'.&mt('Please select').'</option>'."\n";
1034: if ($context eq 'course') {
1.2 raeburn 1035: $options .= &default_course_roles($context,$checkpriv,%customroles);
1.13 raeburn 1036: } elsif ($context eq 'author') {
1.2 raeburn 1037: my @roles = &construction_space_roles($checkpriv);
1.1 raeburn 1038: foreach my $role (@roles) {
1039: my $plrole=&Apache::lonnet::plaintext($role);
1040: $options .= ' <option value="'.$role.'">'.$plrole.'</option>'."\n";
1041: }
1042: } elsif ($context eq 'domain') {
1.2 raeburn 1043: my @roles = &domain_roles($checkpriv);
1.1 raeburn 1044: foreach my $role (@roles) {
1045: my $plrole=&Apache::lonnet::plaintext($role);
1046: $options .= ' <option value="'.$role.'">'.$plrole.'</option>';
1047: }
1048: my $courseform = &Apache::loncommon::selectcourse_link
1.22 raeburn 1049: ('studentform','dccourse','dcdomain','coursedesc',"$env{'request.role.domain'}",undef,'Course');
1.1 raeburn 1050: $cb_jscript =
1.22 raeburn 1051: &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'},'currsec','studentform');
1.1 raeburn 1052: $coursepick = &Apache::loncommon::start_data_table().
1053: &Apache::loncommon::start_data_table_header_row().
1054: '<th>'.$courseform.'</th><th>'.$lt{'rol'}.'</th>'.
1055: '<th>'.$lt{'grs'}.'</th>'.
1056: &Apache::loncommon::end_data_table_header_row().
1057: &Apache::loncommon::start_data_table_row()."\n".
1.22 raeburn 1058: '<td><input type="text" name="coursedesc" value="" onFocus="this.blur();opencrsbrowser('."'studentform','dccourse','dcdomain','coursedesc',''".')" /></td>'."\n".
1.1 raeburn 1059: '<td><select name="courserole">'."\n".
1.2 raeburn 1060: &default_course_roles($context,$checkpriv,%customroles)."\n".
1.1 raeburn 1061: '</select></td><td>'.
1062: '<table class="LC_createuser">'.
1063: '<tr class="LC_section_row"><td valign"top">'.
1.22 raeburn 1064: $lt{'exs'}.'<br /><select name="currsec">'.
1.1 raeburn 1065: ' <option value=""><--'.&mt('Pick course first').
1066: '</select></td>'.
1067: '<td> </td>'.
1068: '<td valign="top">'.$lt{'new'}.'<br />'.
1069: '<input type="text" name="newsec" value="" size="5" />'.
1.22 raeburn 1070: '<input type="hidden" name="groups" value="" />'.
1071: '<input type="hidden" name="sections" value="" />'.
1072: '<input type="hidden" name="origdom" value="'.
1073: $env{'request.role.domain'}.'" />'.
1074: '<input type="hidden" name="dccourse" value="" />'.
1075: '<input type="hidden" name="dcdomain" value="" />'.
1076: '</td></tr></table></td>'.
1.1 raeburn 1077: &Apache::loncommon::end_data_table_row().
1.22 raeburn 1078: &Apache::loncommon::end_data_table()."\n";
1.1 raeburn 1079: }
1080: $options .= '</select>';
1081: return ($options,$cb_jscript,$coursepick);
1082: }
1083:
1084: sub default_course_roles {
1.2 raeburn 1085: my ($context,$checkpriv,%customroles) = @_;
1.1 raeburn 1086: my $output;
1.17 raeburn 1087: my $custom = 1;
1088: my @roles = &course_roles($context,$checkpriv,$custom);
1.1 raeburn 1089: foreach my $role (@roles) {
1.22 raeburn 1090: if ($role ne 'cr') {
1091: my $plrole=&Apache::lonnet::plaintext($role);
1092: $output .= ' <option value="'.$role.'">'.$plrole.'</option>';
1093: }
1.1 raeburn 1094: }
1095: if (keys(%customroles) > 0) {
1.22 raeburn 1096: if (grep(/^cr$/,@roles)) {
1097: foreach my $cust (sort(keys(%customroles))) {
1098: my $custrole='cr_'.$env{'user.domain'}.
1099: '_'.$env{'user.name'}.'_'.$cust;
1100: $output .= ' <option value="'.$custrole.'">'.$cust.'</option>';
1101: }
1.1 raeburn 1102: }
1103: }
1104: return $output;
1105: }
1106:
1107: sub construction_space_roles {
1.2 raeburn 1108: my ($checkpriv) = @_;
1.17 raeburn 1109: my @allroles = &roles_by_context('author');
1.1 raeburn 1110: my @roles;
1.2 raeburn 1111: if ($checkpriv) {
1112: foreach my $role (@allroles) {
1113: if (&Apache::lonnet::allowed('c'.$role,$env{'user.domain'}.'/'.$env{'user.name'})) {
1114: push(@roles,$role);
1115: }
1.1 raeburn 1116: }
1.2 raeburn 1117: return @roles;
1118: } else {
1119: return @allroles;
1.1 raeburn 1120: }
1121: }
1122:
1123: sub domain_roles {
1.2 raeburn 1124: my ($checkpriv) = @_;
1.17 raeburn 1125: my @allroles = &roles_by_context('domain');
1.1 raeburn 1126: my @roles;
1.2 raeburn 1127: if ($checkpriv) {
1128: foreach my $role (@allroles) {
1129: if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) {
1130: push(@roles,$role);
1131: }
1.1 raeburn 1132: }
1.2 raeburn 1133: return @roles;
1134: } else {
1135: return @allroles;
1.1 raeburn 1136: }
1137: }
1138:
1139: sub course_roles {
1.17 raeburn 1140: my ($context,$checkpriv,$custom) = @_;
1141: my @allroles = &roles_by_context('course',$custom);
1.1 raeburn 1142: my @roles;
1143: if ($context eq 'domain') {
1144: @roles = @allroles;
1145: } elsif ($context eq 'course') {
1146: if ($env{'request.course.id'}) {
1.2 raeburn 1147: if ($checkpriv) {
1148: foreach my $role (@allroles) {
1149: if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
1150: push(@roles,$role);
1151: } else {
1.22 raeburn 1152: if ($role ne 'cc' && $env{'request.course.sec'} ne '') {
1153: if (&Apache::lonnet::allowed('c'.$role,
1.2 raeburn 1154: $env{'request.course.id'}.'/'.
1.22 raeburn 1155: $env{'request.course.sec'})) {
1.2 raeburn 1156: push(@roles,$role);
1157: }
1.1 raeburn 1158: }
1159: }
1160: }
1.2 raeburn 1161: } else {
1162: @roles = @allroles;
1.1 raeburn 1163: }
1164: }
1165: }
1166: return @roles;
1167: }
1168:
1169: sub curr_role_permissions {
1.2 raeburn 1170: my ($context,$setting,$checkpriv) = @_;
1.17 raeburn 1171: my $custom = 1;
1.1 raeburn 1172: my @roles;
1.13 raeburn 1173: if ($context eq 'author') {
1.2 raeburn 1174: @roles = &construction_space_roles($checkpriv);
1.1 raeburn 1175: } elsif ($context eq 'domain') {
1176: if ($setting eq 'course') {
1.17 raeburn 1177: @roles = &course_roles($context,$checkpriv,$custom);
1.1 raeburn 1178: } else {
1.2 raeburn 1179: @roles = &domain_roles($checkpriv);
1.1 raeburn 1180: }
1181: } elsif ($context eq 'course') {
1.17 raeburn 1182: @roles = &course_roles($context,$checkpriv,$custom);
1.1 raeburn 1183: }
1184: return @roles;
1185: }
1186:
1187: # ======================================================= Existing Custom Roles
1188:
1189: sub my_custom_roles {
1190: my %returnhash=();
1191: my %rolehash=&Apache::lonnet::dump('roles');
1192: foreach my $key (keys %rolehash) {
1193: if ($key=~/^rolesdef\_(\w+)$/) {
1194: $returnhash{$1}=$1;
1195: }
1196: }
1197: return %returnhash;
1198: }
1199:
1.2 raeburn 1200: sub print_userlist {
1201: my ($r,$mode,$permission,$context,$formname,$totcodes,$codetitles,
1202: $idlist,$idlist_titles) = @_;
1203: my $format = $env{'form.output'};
1.1 raeburn 1204: if (! exists($env{'form.sortby'})) {
1205: $env{'form.sortby'} = 'username';
1206: }
1.2 raeburn 1207: if ($env{'form.Status'} !~ /^(Any|Expired|Active|Future)$/) {
1208: $env{'form.Status'} = 'Active';
1.1 raeburn 1209: }
1210: my $status_select = &Apache::lonhtmlcommon::StatusOptions
1.2 raeburn 1211: ($env{'form.Status'});
1.1 raeburn 1212:
1.2 raeburn 1213: if ($env{'form.showrole'} eq '') {
1.13 raeburn 1214: if ($context eq 'course') {
1215: $env{'form.showrole'} = 'st';
1216: } else {
1217: $env{'form.showrole'} = 'Any';
1218: }
1.2 raeburn 1219: }
1.1 raeburn 1220: if (! defined($env{'form.output'}) ||
1221: $env{'form.output'} !~ /^(csv|excel|html)$/ ) {
1222: $env{'form.output'} = 'html';
1223: }
1224:
1.2 raeburn 1225: my @statuses;
1226: if ($env{'form.Status'} eq 'Any') {
1227: @statuses = ('previous','active','future');
1228: } elsif ($env{'form.Status'} eq 'Expired') {
1229: @statuses = ('previous');
1230: } elsif ($env{'form.Status'} eq 'Active') {
1231: @statuses = ('active');
1232: } elsif ($env{'form.Status'} eq 'Future') {
1233: @statuses = ('future');
1234: }
1.1 raeburn 1235:
1.2 raeburn 1236: # if ($context eq 'course') {
1237: # $r->print(&display_adv_courseroles());
1238: # }
1.1 raeburn 1239: #
1240: # Interface output
1.2 raeburn 1241: $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n".
1242: '<input type="hidden" name="action" value="'.
1.1 raeburn 1243: $env{'form.action'}.'" />');
1244: $r->print("<p>\n");
1245: if ($env{'form.action'} ne 'modifystudent') {
1246: my %lt=&Apache::lonlocal::texthash('csv' => "CSV",
1247: 'excel' => "Excel",
1248: 'html' => 'HTML');
1249: my $output_selector = '<select size="1" name="output" >';
1250: foreach my $outputformat ('html','csv','excel') {
1251: my $option = '<option value="'.$outputformat.'" ';
1252: if ($outputformat eq $env{'form.output'}) {
1253: $option .= 'selected ';
1254: }
1255: $option .='>'.$lt{$outputformat}.'</option>';
1256: $output_selector .= "\n".$option;
1257: }
1258: $output_selector .= '</select>';
1259: $r->print('<label>'.&mt('Output Format: [_1]',$output_selector).'</label>'.(' 'x3));
1260: }
1.2 raeburn 1261: $r->print('<label>'.&mt('User Status: [_1]',$status_select).'</label>'.(' 'x3)."\n");
1262: my $roleselected = '';
1263: if ($env{'form.showrole'} eq 'Any') {
1264: $roleselected = ' selected="selected" ';
1265: }
1266: my $role_select;
1267: if ($context eq 'domain') {
1268: $role_select = &domain_roles_select();
1269: $r->print('<label>'.&mt('Role Type: [_1]',$role_select).'</label>');
1270: } else {
1271: $role_select = '<select name="showrole">'."\n".
1272: '<option value="Any" '.$roleselected.'>'.
1273: &mt('Any role').'</option>';
1274: my @poss_roles = &curr_role_permissions($context);
1275: foreach my $role (@poss_roles) {
1276: $roleselected = '';
1277: if ($role eq $env{'form.showrole'}) {
1278: $roleselected = ' selected="selected" ';
1279: }
1.22 raeburn 1280: my $plrole;
1281: if ($role eq 'cr') {
1282: $plrole = &mt('Custom role');
1283: } else {
1284: $plrole=&Apache::lonnet::plaintext($role);
1285: }
1.2 raeburn 1286: $role_select .= '<option value="'.$role.'"'.$roleselected.'>'.$plrole.'</option>';
1287: }
1.22 raeburn 1288: $role_select .= '</select>';
1.2 raeburn 1289: $r->print('<label>'.&mt('Role: [_1]',$role_select).'</label>');
1290: }
1291: if (!(($context eq 'domain') && ($env{'form.roletype'} eq 'course'))) {
1292: $r->print(&list_submit_button(&mt('Update Display'))."\n</p>\n");
1293: }
1294: my ($indexhash,$keylist) = &make_keylist_array();
1295: my (%userlist,%userinfo);
1.3 raeburn 1296: if ($context eq 'domain' && $env{'form.roletype'} eq 'course') {
1297: my $courseform =
1298: &Apache::lonhtmlcommon::course_selection($formname,$totcodes,
1299: $codetitles,$idlist,$idlist_titles);
1300: $r->print('<p>'.&Apache::lonhtmlcommon::start_pick_box()."\n".
1301: &Apache::lonhtmlcommon::start_pick_box()."\n".
1302: &Apache::lonhtmlcommon::row_title(&mt('Select Course(s)'),
1303: 'LC_oddrow_value')."\n".
1304: $courseform."\n".
1305: &Apache::lonhtmlcommon::row_closure(1).
1306: &Apache::lonhtmlcommon::end_pick_box().'</p>'.
1307: '<p>'.&list_submit_button(&mt('Update Display')).
1308: "\n</p>\n");
1.11 raeburn 1309: if ($env{'form.coursepick'}) {
1310: $r->print('<hr />'.&mt('Searching').' ...<br /> <br />');
1311: }
1312: } else {
1313: $r->print('<hr />'.&mt('Searching').' ...<br /> <br />');
1.3 raeburn 1314: }
1315: $r->rflush();
1.1 raeburn 1316: if ($context eq 'course') {
1.3 raeburn 1317: my $classlist = &Apache::loncoursedata::get_classlist();
1318: my $secidx = &Apache::loncoursedata::CL_SECTION();
1.22 raeburn 1319: my $viewablesec = &viewable_section($permission);
1.3 raeburn 1320: foreach my $student (keys(%{$classlist})) {
1.22 raeburn 1321: my $section = $classlist->{$student}[$secidx];
1322: if ($viewablesec ne '') {
1323: if ($section ne $viewablesec) {
1.3 raeburn 1324: next;
1325: } else {
1326: $userlist{$student} = $classlist->{$student};
1.1 raeburn 1327: }
1.3 raeburn 1328: } else {
1329: $userlist{$student} = $classlist->{$student};
1.1 raeburn 1330: }
1331: }
1.22 raeburn 1332: my $cid = $env{'request.course.id'};
1.17 raeburn 1333: my ($cnum,$cdom) = &get_course_identity($cid);
1.2 raeburn 1334: my $showroles;
1335: if ($env{'form.showrole'} ne 'Any') {
1336: $showroles = [$env{'form.showrole'}];
1.1 raeburn 1337: } else {
1.2 raeburn 1338: $showroles = undef;
1339: }
1.22 raeburn 1340: my $withsec = 1;
1.2 raeburn 1341: my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
1.22 raeburn 1342: \@statuses,$showroles,undef,$withsec);
1.2 raeburn 1343: &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
1.11 raeburn 1344: \%advrolehash,$permission);
1.2 raeburn 1345: } else {
1346: my (%cstr_roles,%dom_roles);
1.13 raeburn 1347: if ($context eq 'author') {
1.2 raeburn 1348: # List co-authors and assistant co-authors
1.17 raeburn 1349: my @possroles = &roles_by_context($context);
1.2 raeburn 1350: %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
1351: \@statuses,\@possroles);
1352: &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
1.11 raeburn 1353: \%cstr_roles,$permission);
1.2 raeburn 1354: } elsif ($context eq 'domain') {
1355: if ($env{'form.roletype'} eq 'domain') {
1356: %dom_roles = &Apache::lonnet::get_domain_roles($env{'request.role.domain'});
1357: foreach my $key (keys(%dom_roles)) {
1358: if (ref($dom_roles{$key}) eq 'HASH') {
1359: &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11 raeburn 1360: \%userinfo,$dom_roles{$key},$permission);
1.2 raeburn 1361: }
1362: }
1.13 raeburn 1363: } elsif ($env{'form.roletype'} eq 'author') {
1.2 raeburn 1364: my %dom_roles = &Apache::lonnet::get_domain_roles($env{'request.role.domain'},['au']);
1365: my %coauthors;
1366: foreach my $key (keys(%dom_roles)) {
1367: if (ref($dom_roles{$key}) eq 'HASH') {
1368: if ($env{'form.showrole'} eq 'au') {
1369: &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11 raeburn 1370: \%userinfo,$dom_roles{$key},$permission);
1.2 raeburn 1371: } else {
1372: my @possroles;
1373: if ($env{'form.showrole'} eq 'Any') {
1.22 raeburn 1374: @possroles = &roles_by_context('author');
1.2 raeburn 1375: } else {
1376: @possroles = ($env{'form.showrole'});
1377: }
1378: foreach my $author (sort(keys(%{$dom_roles{$key}}))) {
1.22 raeburn 1379: my ($role,$authorname,$authordom) = split(/:/,$author,-1);
1.2 raeburn 1380: my $extent = '/'.$authordom.'/'.$authorname;
1381: %{$coauthors{$extent}} =
1382: &Apache::lonnet::get_my_roles($authorname,
1383: $authordom,undef,\@statuses,\@possroles);
1384: }
1385: &gather_userinfo($context,$format,\%userlist,
1.11 raeburn 1386: $indexhash,\%userinfo,\%coauthors,$permission);
1.2 raeburn 1387: }
1388: }
1389: }
1390: } elsif ($env{'form.roletype'} eq 'course') {
1391: if ($env{'form.coursepick'}) {
1392: my %courses = &process_coursepick();
1393: my %allusers;
1394: foreach my $cid (keys(%courses)) {
1.17 raeburn 1395: my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
1.11 raeburn 1396: next if ($cnum eq '' || $cdom eq '');
1.17 raeburn 1397: my $custom = 1;
1.2 raeburn 1398: my (@roles,@sections,%access,%users,%userdata,
1.6 albertel 1399: %statushash);
1.2 raeburn 1400: if ($env{'form.showrole'} eq 'Any') {
1.17 raeburn 1401: @roles = &course_roles($context,undef,$custom);
1.2 raeburn 1402: } else {
1403: @roles = ($env{'form.showrole'});
1404: }
1405: foreach my $role (@roles) {
1406: %{$users{$role}} = ();
1407: }
1408: foreach my $type (@statuses) {
1409: $access{$type} = $type;
1410: }
1411: &Apache::loncommon::get_course_users($cdom,$cnum,\%access,\@roles,\@sections,\%users,\%userdata,\%statushash);
1412: foreach my $user (keys(%userdata)) {
1413: next if (ref($userinfo{$user}) eq 'HASH');
1414: foreach my $item ('fullname','id') {
1415: $userinfo{$user}{$item} = $userdata{$user}[$indexhash->{$item}];
1416: }
1417: }
1418: foreach my $role (keys(%users)) {
1419: foreach my $user (keys(%{$users{$role}})) {
1420: my $uniqid = $user.':'.$role;
1421: $allusers{$uniqid}{$cid} = { desc => $cdesc,
1422: secs => $statushash{$user}{$role},
1423: };
1424: }
1425: }
1426: }
1427: &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11 raeburn 1428: \%userinfo,\%allusers,$permission);
1.2 raeburn 1429: } else {
1.10 raeburn 1430: $r->print('<input type="hidden" name="phase" value="'.
1431: $env{'form.phase'}.'" /></form>');
1.2 raeburn 1432: return;
1433: }
1.1 raeburn 1434: }
1435: }
1.3 raeburn 1436: }
1437: if (keys(%userlist) == 0) {
1.13 raeburn 1438: if ($context eq 'author') {
1.3 raeburn 1439: $r->print(&mt('There are no co-authors to display.')."\n");
1440: } elsif ($context eq 'domain') {
1441: if ($env{'form.roletype'} eq 'domain') {
1442: $r->print(&mt('There are no users with domain roles to display.')."\n");
1.13 raeburn 1443: } elsif ($env{'form.roletype'} eq 'author') {
1.3 raeburn 1444: $r->print(&mt('There are no authors or co-authors to display.')."\n");
1445: } elsif ($env{'form.roletype'} eq 'course') {
1446: $r->print(&mt('There are no course users to display')."\n");
1.2 raeburn 1447: }
1.3 raeburn 1448: } elsif ($context eq 'course') {
1449: $r->print(&mt('There are no course users to display.')."\n");
1450: }
1451: } else {
1452: # Print out the available choices
1.4 raeburn 1453: my $usercount;
1.3 raeburn 1454: if ($env{'form.action'} eq 'modifystudent') {
1.10 raeburn 1455: ($usercount) = &show_users_list($r,$context,'view',$permission,
1.4 raeburn 1456: $env{'form.Status'},\%userlist,$keylist);
1.1 raeburn 1457: } else {
1.4 raeburn 1458: ($usercount) = &show_users_list($r,$context,$env{'form.output'},
1.10 raeburn 1459: $permission,$env{'form.Status'},\%userlist,$keylist);
1.4 raeburn 1460: }
1461: if (!$usercount) {
1462: $r->print('<br />'.&mt('There are no users matching the search criteria.'));
1.2 raeburn 1463: }
1464: }
1.10 raeburn 1465: $r->print('<input type="hidden" name="phase" value="'.
1466: $env{'form.phase'}.'" /></form>');
1.2 raeburn 1467: }
1468:
1469: sub list_submit_button {
1470: my ($text) = @_;
1.11 raeburn 1471: return '<input type="button" name="updatedisplay" value="'.$text.'" onclick="javascript:display_update()" />';
1.2 raeburn 1472: }
1473:
1474: sub gather_userinfo {
1.11 raeburn 1475: my ($context,$format,$userlist,$indexhash,$userinfo,$rolehash,$permission) = @_;
1.2 raeburn 1476: foreach my $item (keys(%{$rolehash})) {
1477: my %userdata;
1.22 raeburn 1478: if ($context eq 'author') {
1.2 raeburn 1479: ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
1480: split(/:/,$item);
1481: ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
1.24 ! raeburn 1482: &build_user_record($context,\%userdata,$userinfo,$indexhash,
! 1483: $item,$userlist);
1.22 raeburn 1484: } elsif ($context eq 'course') {
1485: my $viewablesec = &viewable_section($permission);
1486: ($userdata{'username'},$userdata{'domain'},$userdata{'role'},
1487: $userdata{'section'}) = split(/:/,$item,-1);
1488: ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
1489: if (($viewablesec ne '') && ($userdata{'section'} ne '')) {
1490: next if ($viewablesec ne $userdata{'section'});
1491: }
1.24 ! raeburn 1492: &build_user_record($context,\%userdata,$userinfo,$indexhash,
! 1493: $item,$userlist);
1.2 raeburn 1494: } elsif ($context eq 'domain') {
1495: if ($env{'form.roletype'} eq 'domain') {
1496: ($userdata{'role'},$userdata{'username'},$userdata{'domain'}) =
1497: split(/:/,$item);
1498: ($userdata{'end'},$userdata{'start'})=split(/:/,$rolehash->{$item});
1.24 ! raeburn 1499: &build_user_record($context,\%userdata,$userinfo,$indexhash,
! 1500: $item,$userlist);
1.13 raeburn 1501: } elsif ($env{'form.roletype'} eq 'author') {
1.2 raeburn 1502: if (ref($rolehash->{$item}) eq 'HASH') {
1503: $userdata{'extent'} = $item;
1504: foreach my $key (keys(%{$rolehash->{$item}})) {
1505: ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) = split(/:/,$key);
1506: ($userdata{'start'},$userdata{'end'}) =
1507: split(/:/,$rolehash->{$item}{$key});
1508: my $uniqid = $key.':'.$item;
1.24 ! raeburn 1509: &build_user_record($context,\%userdata,$userinfo,$indexhash,
! 1510: $uniqid,$userlist);
1.2 raeburn 1511: }
1512: }
1513: } elsif ($env{'form.roletype'} eq 'course') {
1514: ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
1515: split(/:/,$item);
1516: if (ref($rolehash->{$item}) eq 'HASH') {
1.11 raeburn 1517: my $numcids = keys(%{$rolehash->{$item}});
1.2 raeburn 1518: foreach my $cid (sort(keys(%{$rolehash->{$item}}))) {
1519: if (ref($rolehash->{$item}{$cid}) eq 'HASH') {
1520: my $spanstart = '';
1521: my $spanend = '; ';
1522: my $space = ', ';
1523: if ($format eq 'html' || $format eq 'view') {
1524: $spanstart = '<span class="LC_nobreak">';
1.23 raeburn 1525: # FIXME: actions on courses disabled for now
1526: # if ($permission->{'cusr'}) {
1527: # if ($numcids > 1) {
1528: # $spanstart .= '<input type="radio" name="'.$item.'" value="'.$cid.'" />';
1529: # } else {
1530: # $spanstart .= '<input type="hidden" name="'.$item.'" value="'.$cid.'" />';
1531: # }
1532: # }
1.2 raeburn 1533: $spanend = '</span><br />';
1534: $space = ', ';
1535: }
1536: $userdata{'extent'} .= $spanstart.
1537: $rolehash->{$item}{$cid}{'desc'}.$space;
1538: if (ref($rolehash->{$item}{$cid}{'secs'}) eq 'HASH') {
1539: foreach my $sec (sort(keys(%{$rolehash->{$item}{$cid}{'secs'}}))) {
1540: $userdata{'extent'} .= $sec.$space.$rolehash->{$item}{$cid}{'secs'}{$sec}.$spanend;
1541: }
1542: }
1543: }
1544: }
1545: }
1.24 ! raeburn 1546: &build_user_record($context,\%userdata,$userinfo,$indexhash,
! 1547: $item,$userlist);
1.2 raeburn 1548: }
1549: }
1550: }
1551: return;
1552: }
1553:
1554: sub build_user_record {
1.24 ! raeburn 1555: my ($context,$userdata,$userinfo,$indexhash,$record_key,$userlist) = @_;
1.11 raeburn 1556: next if ($userdata->{'start'} eq '-1' && $userdata->{'end'} eq '-1');
1.24 ! raeburn 1557: if (!(($context eq 'domain') && ($env{'form.roletype'} eq 'course'))) {
! 1558: &process_date_info($userdata);
! 1559: }
1.2 raeburn 1560: my $username = $userdata->{'username'};
1561: my $domain = $userdata->{'domain'};
1562: if (ref($userinfo->{$username.':'.$domain}) eq 'HASH') {
1.24 ! raeburn 1563: $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
1.2 raeburn 1564: $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
1565: } else {
1566: &aggregate_user_info($domain,$username,$userinfo);
1567: $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
1568: $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
1569: }
1570: foreach my $key (keys(%{$indexhash})) {
1571: if (defined($userdata->{$key})) {
1572: $userlist->{$record_key}[$indexhash->{$key}] = $userdata->{$key};
1573: }
1574: }
1575: return;
1576: }
1577:
1578: sub courses_selector {
1579: my ($cdom,$formname) = @_;
1580: my %coursecodes = ();
1581: my %codes = ();
1582: my @codetitles = ();
1583: my %cat_titles = ();
1584: my %cat_order = ();
1585: my %idlist = ();
1586: my %idnums = ();
1587: my %idlist_titles = ();
1588: my $caller = 'global';
1589: my $format_reply;
1590: my $jscript = '';
1591:
1.7 albertel 1592: my $totcodes = 0;
1593: $totcodes =
1.2 raeburn 1594: &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,
1595: $cdom,$totcodes);
1596: if ($totcodes > 0) {
1597: $format_reply =
1598: &Apache::lonnet::auto_instcode_format($caller,$cdom,\%coursecodes,
1599: \%codes,\@codetitles,\%cat_titles,\%cat_order);
1600: if ($format_reply eq 'ok') {
1601: my $numtypes = @codetitles;
1602: &Apache::courseclassifier::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
1603: my ($scripttext,$longtitles) = &Apache::courseclassifier::javascript_definitions(\@codetitles,\%idlist,\%idlist_titles,\%idnums,\%cat_titles);
1604: my $longtitles_str = join('","',@{$longtitles});
1605: my $allidlist = $idlist{$codetitles[0]};
1606: $jscript .= &Apache::courseclassifier::courseset_js_start($formname,$longtitles_str,$allidlist);
1607: $jscript .= $scripttext;
1608: $jscript .= &Apache::courseclassifier::javascript_code_selections($formname,@codetitles);
1609: }
1610: }
1611: my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($cdom);
1612:
1613: my %elements = (
1614: Year => 'selectbox',
1615: coursepick => 'radio',
1616: coursetotal => 'text',
1617: courselist => 'text',
1618: );
1619: $jscript .= &Apache::lonhtmlcommon::set_form_elements(\%elements);
1620: if ($env{'form.coursepick'} eq 'category') {
1621: $jscript .= qq|
1622: function setCourseCat(formname) {
1623: if (formname.Year.options[formname.Year.selectedIndex].value == -1) {
1624: return;
1625: }
1626: courseSet('Year');
1627: for (var j=0; j<formname.Semester.length; j++) {
1628: if (formname.Semester.options[j].value == "$env{'form.Semester'}") {
1629: formname.Semester.options[j].selected = true;
1630: }
1631: }
1632: if (formname.Semester.options[formname.Semester.selectedIndex].value == -1) {
1633: return;
1634: }
1635: courseSet('Semester');
1636: for (var j=0; j<formname.Department.length; j++) {
1637: if (formname.Department.options[j].value == "$env{'form.Department'}") { formname.Department.options[j].selected = true;
1638: }
1639: }
1640: if (formname.Department.options[formname.Department.selectedIndex].value == -1) {
1641: return;
1642: }
1643: courseSet('Department');
1644: for (var j=0; j<formname.Number.length; j++) {
1645: if (formname.Number.options[j].value == "$env{'form.Number'}") {
1646: formname.Number.options[j].selected = true;
1647: }
1648: }
1649: }
1650: |;
1651: }
1652: return ($cb_jscript,$jscript,$totcodes,\@codetitles,\%idlist,
1653: \%idlist_titles);
1654: }
1655:
1656: sub course_selector_loadcode {
1657: my ($formname) = @_;
1658: my $loadcode;
1659: if ($env{'form.coursepick'} ne '') {
1660: $loadcode = 'javascript:setFormElements(document.'.$formname.')';
1661: if ($env{'form.coursepick'} eq 'category') {
1662: $loadcode .= ';javascript:setCourseCat(document.'.$formname.')';
1663: }
1664: }
1665: return $loadcode;
1666: }
1667:
1668: sub process_coursepick {
1669: my $coursefilter = $env{'form.coursepick'};
1670: my $cdom = $env{'request.role.domain'};
1671: my %courses;
1672: if ($coursefilter eq 'all') {
1673: %courses = &Apache::lonnet::courseiddump($cdom,'.','.','.','.','.',
1674: undef,undef,'Course');
1675: } elsif ($coursefilter eq 'category') {
1676: my $instcode = &instcode_from_coursefilter();
1677: %courses = &Apache::lonnet::courseiddump($cdom,'.','.',$instcode,'.','.',
1678: undef,undef,'Course');
1679: } elsif ($coursefilter eq 'specific') {
1680: if ($env{'form.coursetotal'} > 1) {
1681: my @course_ids = split(/&&/,$env{'form.courselist'});
1682: foreach my $cid (@course_ids) {
1683: $courses{$cid} = '';
1.1 raeburn 1684: }
1.2 raeburn 1685: } else {
1686: $courses{$env{'form.courselist'}} = '';
1.1 raeburn 1687: }
1.2 raeburn 1688: }
1689: return %courses;
1690: }
1691:
1692: sub instcode_from_coursefilter {
1693: my $instcode = '';
1694: my @cats = ('Semester','Year','Department','Number');
1695: foreach my $category (@cats) {
1696: if (defined($env{'form.'.$category})) {
1697: unless ($env{'form.'.$category} eq '-1') {
1698: $instcode .= $env{'form.'.$category};
1699: }
1700: }
1701: }
1702: if ($instcode eq '') {
1703: $instcode = '.';
1704: }
1705: return $instcode;
1706: }
1707:
1708: sub display_adv_courseroles {
1709: my $output;
1710: #
1711: # List course personnel
1712: my %coursepersonnel =
1713: &Apache::lonnet::get_course_adv_roles($env{'request.course.id'});
1714: #
1715: $output = '<br />'.&Apache::loncommon::start_data_table();
1716: foreach my $role (sort(keys(%coursepersonnel))) {
1717: next if ($role =~ /^\s*$/);
1718: $output .= &Apache::loncommon::start_data_table_row().
1719: '<td>'.$role.'</td><td>';
1720: foreach my $user (split(',',$coursepersonnel{$role})) {
1721: my ($puname,$pudom)=split(':',$user);
1722: $output .= ' '.&Apache::loncommon::aboutmewrapper(
1723: &Apache::loncommon::plainname($puname,$pudom),
1724: $puname,$pudom);
1725: }
1726: $output .= '</td>'.&Apache::loncommon::end_data_table_row();
1727: }
1728: $output .= &Apache::loncommon::end_data_table();
1729: }
1730:
1731: sub make_keylist_array {
1732: my ($index,$keylist);
1733: $index->{'domain'} = &Apache::loncoursedata::CL_SDOM();
1734: $index->{'username'} = &Apache::loncoursedata::CL_SNAME();
1735: $index->{'end'} = &Apache::loncoursedata::CL_END();
1736: $index->{'start'} = &Apache::loncoursedata::CL_START();
1737: $index->{'id'} = &Apache::loncoursedata::CL_ID();
1738: $index->{'section'} = &Apache::loncoursedata::CL_SECTION();
1739: $index->{'fullname'} = &Apache::loncoursedata::CL_FULLNAME();
1740: $index->{'status'} = &Apache::loncoursedata::CL_STATUS();
1741: $index->{'type'} = &Apache::loncoursedata::CL_TYPE();
1742: $index->{'lockedtype'} = &Apache::loncoursedata::CL_LOCKEDTYPE();
1743: $index->{'groups'} = &Apache::loncoursedata::CL_GROUP();
1744: $index->{'email'} = &Apache::loncoursedata::CL_PERMANENTEMAIL();
1745: $index->{'role'} = &Apache::loncoursedata::CL_ROLE();
1746: $index->{'extent'} = &Apache::loncoursedata::CL_EXTENT();
1747: foreach my $key (keys(%{$index})) {
1748: $keylist->[$index->{$key}] = $key;
1749: }
1750: return ($index,$keylist);
1751: }
1752:
1753: sub aggregate_user_info {
1754: my ($udom,$uname,$userinfo) = @_;
1755: my %info=&Apache::lonnet::get('environment',
1756: ['firstname','middlename',
1757: 'lastname','generation','id'],
1758: $udom,$uname);
1759: my ($tmp) = keys(%info);
1760: my ($fullname,$id);
1761: if ($tmp =~/^(con_lost|error|no_such_host)/i) {
1762: $fullname = 'not available';
1763: $id = 'not available';
1764: &Apache::lonnet::logthis('unable to retrieve environment '.
1765: 'for '.$uname.':'.$udom);
1.1 raeburn 1766: } else {
1.2 raeburn 1767: $fullname = &Apache::lonnet::format_name(@info{qw/firstname middlename lastname generation/},'lastname');
1768: $id = $info{'id'};
1769: }
1770: $userinfo->{$uname.':'.$udom} = {
1771: fullname => $fullname,
1772: id => $id,
1773: };
1774: return;
1775: }
1.1 raeburn 1776:
1.2 raeburn 1777: sub process_date_info {
1778: my ($userdata) = @_;
1779: my $now = time;
1780: $userdata->{'status'} = 'Active';
1781: if ($userdata->{'start'} > 0) {
1782: if ($now < $userdata->{'start'}) {
1783: $userdata->{'status'} = 'Future';
1784: }
1.1 raeburn 1785: }
1.2 raeburn 1786: if ($userdata->{'end'} > 0) {
1787: if ($now > $userdata->{'end'}) {
1788: $userdata->{'status'} = 'Expired';
1789: }
1790: }
1791: return;
1.1 raeburn 1792: }
1793:
1794: sub show_users_list {
1.10 raeburn 1795: my ($r,$context,$mode,$permission,$statusmode,$userlist,$keylist)=@_;
1.1 raeburn 1796: #
1797: # Variables for excel output
1798: my ($excel_workbook, $excel_sheet, $excel_filename,$row,$format);
1799: #
1800: # Variables for csv output
1801: my ($CSVfile,$CSVfilename);
1802: #
1803: my $sortby = $env{'form.sortby'};
1.3 raeburn 1804: my @sortable = ('username','domain','id','fullname','start','end','email','role');
1.2 raeburn 1805: if ($context eq 'course') {
1.3 raeburn 1806: push(@sortable,('section','groups','type'));
1.2 raeburn 1807: } else {
1.3 raeburn 1808: push(@sortable,'extent');
1809: }
1810: if (!grep(/^\Q$sortby\E$/,@sortable)) {
1811: $sortby = 'username';
1.1 raeburn 1812: }
1.22 raeburn 1813: my $setting = $env{'form.roletype'};
1.2 raeburn 1814: my ($cid,$cdom,$cnum,$classgroups,$displayphotos,$displayclickers);
1.1 raeburn 1815: if ($context eq 'course') {
1.22 raeburn 1816: $cid = $env{'request.course.id'};
1.17 raeburn 1817: ($cnum,$cdom) = &get_course_identity($cid);
1.2 raeburn 1818: ($classgroups) = &Apache::loncoursedata::get_group_memberships(
1819: $userlist,$keylist,$cdom,$cnum);
1.16 raeburn 1820: if ($mode eq 'autoenroll') {
1821: $env{'form.showrole'} = 'st';
1822: } else {
1823: if (! exists($env{'form.displayphotos'})) {
1824: $env{'form.displayphotos'} = 'off';
1825: }
1826: $displayphotos = $env{'form.displayphotos'};
1827: if (! exists($env{'form.displayclickers'})) {
1828: $env{'form.displayclickers'} = 'off';
1829: }
1830: $displayclickers = $env{'form.displayclickers'};
1831: if ($env{'course.'.$cid.'.internal.showphoto'}) {
1832: $r->print('
1.1 raeburn 1833: <script type="text/javascript">
1834: function photowindow(photolink) {
1835: var title = "Photo_Viewer";
1836: var options = "scrollbars=1,resizable=1,menubar=0";
1837: options += ",width=240,height=240";
1838: stdeditbrowser = open(photolink,title,options,"1");
1839: stdeditbrowser.focus();
1840: }
1841: </script>
1.16 raeburn 1842: ');
1843: }
1844: $r->print(<<END);
1.1 raeburn 1845: <input type="hidden" name="displayphotos" value="$displayphotos" />
1846: <input type="hidden" name="displayclickers" value="$displayclickers" />
1847: END
1.16 raeburn 1848: }
1.1 raeburn 1849: }
1.16 raeburn 1850: if ($mode ne 'autoenroll') {
1.11 raeburn 1851: my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
1852: my $alert = &mt("You must select at least one user by checking a user's 'Select' checkbox");
1853: my $singconfirm = &mt(' for a single user');
1854: my $multconfirm = &mt(' for multiple users');
1855: my $date_sec_selector = &date_section_javascript($context,$setting,$statusmode);
1.1 raeburn 1856: $r->print(<<END);
1.10 raeburn 1857:
1858: <script type="text/javascript" language="Javascript">
1.11 raeburn 1859: $check_uncheck_js
1860:
1861: function verify_action (field) {
1862: var numchecked = 0;
1863: var singconf = '$singconfirm';
1864: var multconf = '$multconfirm';
1865: if (field.length > 0) {
1866: for (i = 0; i < field.length; i++) {
1867: if (field[i].checked == true) {
1868: numchecked ++;
1869: }
1870: }
1871: } else {
1872: if (field.checked == true) {
1873: numchecked ++;
1874: }
1875: }
1876: if (numchecked == 0) {
1877: alert("$alert");
1878: }
1879: else {
1880: var message = document.studentform.bulkaction[document.studentform.bulkaction.selectedIndex].text;
1881: if (numchecked == 1) {
1882: message += singconf;
1883: }
1884: else {
1885: message += multconf;
1886: }
1887: if (confirm(message)) {
1888: document.studentform.phase.value = 'bulkchange';
1889: document.studentform.submit();
1890: }
1891: }
1892: }
1.10 raeburn 1893:
1894: function username_display_launch(username,domain) {
1895: var target;
1896: for (var i=0; i<document.studentform.usernamelink.length; i++) {
1897: if (document.studentform.usernamelink[i].checked) {
1898: target = document.studentform.usernamelink[i].value;
1899: }
1900: }
1901: if (target == 'modify') {
1902: document.studentform.srchterm.value=username;
1903: document.studentform.srchdomain.value=domain;
1904: document.studentform.phase.value='get_user_info';
1905: document.studentform.action.value = 'singleuser';
1906: document.studentform.submit();
1907: }
1908: else {
1909: document.location.href = '/adm/'+domain+'/'+username+'/aboutme';
1910: }
1911: }
1912: </script>
1.11 raeburn 1913: $date_sec_selector
1.1 raeburn 1914: <input type="hidden" name="state" value="$env{'form.state'}" />
1915: END
1916: }
1917: $r->print(<<END);
1918: <input type="hidden" name="sortby" value="$sortby" />
1919: END
1920:
1921: my %lt=&Apache::lonlocal::texthash(
1922: 'username' => "username",
1923: 'domain' => "domain",
1924: 'id' => 'ID',
1925: 'fullname' => "name",
1926: 'section' => "section",
1927: 'groups' => "active groups",
1928: 'start' => "start date",
1929: 'end' => "end date",
1930: 'status' => "status",
1.2 raeburn 1931: 'role' => "role",
1.1 raeburn 1932: 'type' => "enroll type/action",
1933: 'email' => "email address",
1934: 'clicker' => "clicker id",
1935: 'photo' => "photo",
1.2 raeburn 1936: 'extent' => "extent",
1.11 raeburn 1937: 'pr' => "Proceed",
1938: 'ca' => "check all",
1939: 'ua' => "uncheck all",
1940: 'ac' => "Action to take for selected users",
1.10 raeburn 1941: 'link' => "Behavior of username links",
1942: 'aboutme' => "Display a user's personal page",
1943: 'modify' => "Modify a user's information",
1.1 raeburn 1944: );
1.2 raeburn 1945: if ($context eq 'domain' && $env{'form.roletype'} eq 'course') {
1946: $lt{'extent'} = &mt('Course(s): description, section(s), status');
1.13 raeburn 1947: } elsif ($context eq 'author') {
1.2 raeburn 1948: $lt{'extent'} = &mt('Author');
1949: }
1.1 raeburn 1950: my @cols = ('username','domain','id','fullname');
1951: if ($context eq 'course') {
1952: push(@cols,'section');
1953: }
1.2 raeburn 1954: if (!($context eq 'domain' && $env{'form.roletype'} eq 'course')) {
1955: push(@cols,('start','end'));
1956: }
1.3 raeburn 1957: if ($env{'form.showrole'} eq 'Any' || $env{'form.showrole'} eq 'cr') {
1.2 raeburn 1958: push(@cols,'role');
1959: }
1.13 raeburn 1960: if ($context eq 'domain' && ($env{'form.roletype'} eq 'author' ||
1.2 raeburn 1961: $env{'form.roletype'} eq 'course')) {
1962: push (@cols,'extent');
1963: }
1964: if (($statusmode eq 'Any') &&
1965: (!($context eq 'domain' && $env{'form.roletype'} eq 'course'))) {
1.1 raeburn 1966: push(@cols,'status');
1967: }
1968: if ($context eq 'course') {
1969: push(@cols,'groups');
1970: }
1971: push(@cols,'email');
1972:
1.4 raeburn 1973: my $rolefilter = $env{'form.showrole'};
1.5 raeburn 1974: if ($env{'form.showrole'} eq 'cr') {
1975: $rolefilter = &mt('custom');
1976: } elsif ($env{'form.showrole'} ne 'Any') {
1.2 raeburn 1977: $rolefilter = &Apache::lonnet::plaintext($env{'form.showrole'});
1978: }
1.16 raeburn 1979: my $results_description;
1980: if ($mode ne 'autoenroll') {
1981: $results_description = &results_header_row($rolefilter,$statusmode,
1.24 ! raeburn 1982: $context,$permission,$mode);
1.16 raeburn 1983: $r->print('<b>'.$results_description.'</b><br />');
1984: }
1.11 raeburn 1985: my ($output,$actionselect);
1.16 raeburn 1986: if ($mode eq 'html' || $mode eq 'view' || $mode eq 'autoenroll') {
1987: if ($mode ne 'autoenroll') {
1988: if ($permission->{'cusr'}) {
1989: $actionselect = &select_actions($context,$setting,$statusmode);
1990: }
1991: $r->print(<<END);
1.10 raeburn 1992: <input type="hidden" name="srchby" value="uname" />
1993: <input type="hidden" name="srchin" value="dom" />
1994: <input type="hidden" name="srchtype" value="exact" />
1995: <input type="hidden" name="srchterm" value="" />
1.11 raeburn 1996: <input type="hidden" name="srchdomain" value="" />
1.1 raeburn 1997: END
1.16 raeburn 1998: $output = '<p>';
1999: my @linkdests = ('aboutme');
2000: if ($permission->{'cusr'}) {
2001: push (@linkdests,'modify');
2002: $output .= '<span class="LC_nobreak">'.$lt{'link'}.': ';
2003: my $usernamelink = $env{'form.usernamelink'};
2004: if ($usernamelink eq '') {
2005: $usernamelink = 'aboutme';
2006: }
2007: foreach my $item (@linkdests) {
2008: my $checkedstr = '';
2009: if ($item eq $usernamelink) {
2010: $checkedstr = ' checked="checked" ';
2011: }
2012: $output .= '<label><input type="radio" name="usernamelink" value="'.$item.'"'.$checkedstr.'> '.$lt{$item}.'</label> ';
2013: }
2014: $output .= '</span><br />';
2015: } else {
2016: $output .= &mt("Click on a username to view the user's personal page.").'<br />';
2017: }
2018: if ($actionselect) {
2019: $output .= <<"END";
1.11 raeburn 2020: $lt{'ac'}: $actionselect <input type="button" value="$lt{'pr'}" onclick="javascript:verify_action(document.studentform.actionlist)" /></p>
2021: <p><input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.studentform.actionlist)" />
2022: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.studentform.actionlist)" />
2023: END
1.16 raeburn 2024: }
1.4 raeburn 2025: }
2026: $output .= "\n<p>\n".
1.1 raeburn 2027: &Apache::loncommon::start_data_table().
1.4 raeburn 2028: &Apache::loncommon::start_data_table_header_row();
1.1 raeburn 2029: if ($mode eq 'autoenroll') {
1.4 raeburn 2030: $output .= "
1.1 raeburn 2031: <th><a href=\"javascript:document.studentform.sortby.value='type';document.studentform.submit();\">$lt{'type'}</a></th>
1.4 raeburn 2032: ";
1.1 raeburn 2033: } else {
1.11 raeburn 2034: $output .= "\n".'<th>'.&mt('Count').'</th>'."\n";
2035: if ($actionselect) {
2036: $output .= '<th>'.&mt('Select').'</th>'."\n";
2037: }
1.1 raeburn 2038: }
2039: foreach my $item (@cols) {
1.4 raeburn 2040: $output .= "<th><a href=\"javascript:document.studentform.sortby.value='$item';document.studentform.submit();\">$lt{$item}</a></th>\n";
1.1 raeburn 2041: }
1.2 raeburn 2042: my %role_types = &role_type_names();
1.16 raeburn 2043: if ($context eq 'course' && $mode ne 'autoenroll') {
1.4 raeburn 2044: if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
2045: # Clicker display on or off?
2046: my %clicker_options = &Apache::lonlocal::texthash(
2047: 'on' => 'Show',
2048: 'off' => 'Hide',
2049: );
2050: my $clickerchg = 'on';
2051: if ($displayclickers eq 'on') {
2052: $clickerchg = 'off';
2053: }
2054: $output .= ' <th>'."\n".' '.
2055: '<a href="javascript:document.studentform.displayclickers.value='.
1.1 raeburn 2056: "'".$clickerchg."'".';document.studentform.submit();">'.
2057: $clicker_options{$clickerchg}.'</a> '.$lt{'clicker'}."\n".
1.4 raeburn 2058: ' </th>'."\n";
1.1 raeburn 2059:
1.4 raeburn 2060: # Photo display on or off?
2061: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
2062: my %photo_options = &Apache::lonlocal::texthash(
2063: 'on' => 'Show',
2064: 'off' => 'Hide',
2065: );
2066: my $photochg = 'on';
2067: if ($displayphotos eq 'on') {
2068: $photochg = 'off';
2069: }
2070: $output .= ' <th>'."\n".' '.
2071: '<a href="javascript:document.studentform.displayphotos.value='.
1.1 raeburn 2072: "'".$photochg."'".';document.studentform.submit();">'.
2073: $photo_options{$photochg}.'</a> '.$lt{'photo'}."\n".
1.4 raeburn 2074: ' </th>'."\n";
2075: }
1.1 raeburn 2076: }
1.4 raeburn 2077: }
1.16 raeburn 2078: $output .= &Apache::loncommon::end_data_table_header_row();
1.1 raeburn 2079: # Done with the HTML header line
2080: } elsif ($mode eq 'csv') {
2081: #
2082: # Open a file
2083: $CSVfilename = '/prtspool/'.
1.2 raeburn 2084: $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
2085: time.'_'.rand(1000000000).'.csv';
1.1 raeburn 2086: unless ($CSVfile = Apache::File->new('>/home/httpd'.$CSVfilename)) {
2087: $r->log_error("Couldn't open $CSVfilename for output $!");
2088: $r->print("Problems occured in writing the csv file. ".
2089: "This error has been logged. ".
2090: "Please alert your LON-CAPA administrator.");
2091: $CSVfile = undef;
2092: }
2093: #
2094: # Write headers and data to file
1.2 raeburn 2095: print $CSVfile '"'.$results_description.'"'."\n";
1.1 raeburn 2096: print $CSVfile '"'.join('","',map {
2097: &Apache::loncommon::csv_translate($lt{$_})
2098: } (@cols)).'"'."\n";
2099: } elsif ($mode eq 'excel') {
2100: # Create the excel spreadsheet
2101: ($excel_workbook,$excel_filename,$format) =
2102: &Apache::loncommon::create_workbook($r);
2103: return if (! defined($excel_workbook));
2104: $excel_sheet = $excel_workbook->addworksheet('userlist');
1.2 raeburn 2105: $excel_sheet->write($row++,0,$results_description,$format->{'h2'});
1.1 raeburn 2106: #
2107: my @colnames = map {$lt{$_}} (@cols);
2108: $excel_sheet->write($row++,0,\@colnames,$format->{'bold'});
2109: }
2110:
2111: # Done with header lines in all formats
2112:
2113: my %index;
2114: my $i;
1.2 raeburn 2115: foreach my $idx (@$keylist) {
2116: $index{$idx} = $i++;
2117: }
1.4 raeburn 2118: my $usercount = 0;
1.2 raeburn 2119: # Get groups, role, permanent e-mail so we can sort on them if
2120: # necessary.
2121: foreach my $user (keys(%{$userlist})) {
1.11 raeburn 2122: if ($context eq 'domain' && $user eq $env{'request.role.domain'}.'-domainconfig:'.$env{'request.role.domain'}) {
2123: delete($userlist->{$user});
2124: next;
2125: }
1.2 raeburn 2126: my ($uname,$udom,$role,$groups,$email);
1.5 raeburn 2127: if (($statusmode ne 'Any') &&
2128: ($userlist->{$user}->[$index{'status'}] ne $statusmode)) {
2129: delete($userlist->{$user});
2130: next;
2131: }
1.2 raeburn 2132: if ($context eq 'domain') {
2133: if ($env{'form.roletype'} eq 'domain') {
2134: ($role,$uname,$udom) = split(/:/,$user);
1.11 raeburn 2135: if (($uname eq $env{'request.role.domain'}.'-domainconfig') &&
2136: ($udom eq $env{'request.role.domain'})) {
2137: delete($userlist->{$user});
2138: next;
2139: }
1.13 raeburn 2140: } elsif ($env{'form.roletype'} eq 'author') {
1.2 raeburn 2141: ($uname,$udom,$role) = split(/:/,$user,-1);
2142: } elsif ($env{'form.roletype'} eq 'course') {
2143: ($uname,$udom,$role) = split(/:/,$user);
2144: }
2145: } else {
2146: ($uname,$udom,$role) = split(/:/,$user,-1);
2147: if (($context eq 'course') && $role eq '') {
2148: $role = 'st';
2149: }
2150: }
2151: $userlist->{$user}->[$index{'role'}] = $role;
2152: if (($env{'form.showrole'} ne 'Any') && (!($env{'form.showrole'} eq 'cr' && $role =~ /^cr\//)) && ($role ne $env{'form.showrole'})) {
2153: delete($userlist->{$user});
2154: next;
2155: }
2156: if (ref($classgroups) eq 'HASH') {
2157: $groups = $classgroups->{$user};
2158: }
2159: if (ref($groups->{active}) eq 'HASH') {
2160: $userlist->{$user}->[$index{'groups'}] = join(', ',keys(%{$groups->{'active'}}));
2161: }
2162: my %emails = &Apache::loncommon::getemails($uname,$udom);
2163: if ($emails{'permanentemail'} =~ /\S/) {
2164: $userlist->{$user}->[$index{'email'}] = $emails{'permanentemail'};
2165: }
1.4 raeburn 2166: $usercount ++;
2167: }
2168: my $autocount = 0;
2169: my $manualcount = 0;
2170: my $lockcount = 0;
2171: my $unlockcount = 0;
2172: if ($usercount) {
2173: $r->print($output);
2174: } else {
2175: if ($mode eq 'autoenroll') {
2176: return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
2177: } else {
2178: return;
2179: }
1.1 raeburn 2180: }
1.2 raeburn 2181: #
2182: # Sort the users
1.1 raeburn 2183: my $index = $index{$sortby};
2184: my $second = $index{'username'};
2185: my $third = $index{'domain'};
1.2 raeburn 2186: my @sorted_users = sort {
2187: lc($userlist->{$a}->[$index]) cmp lc($userlist->{$b}->[$index])
1.1 raeburn 2188: ||
1.2 raeburn 2189: lc($userlist->{$a}->[$second]) cmp lc($userlist->{$b}->[$second]) ||
2190: lc($userlist->{$a}->[$third]) cmp lc($userlist->{$b}->[$third])
2191: } (keys(%$userlist));
1.4 raeburn 2192: my $rowcount = 0;
1.2 raeburn 2193: foreach my $user (@sorted_users) {
1.4 raeburn 2194: my %in;
1.2 raeburn 2195: my $sdata = $userlist->{$user};
1.4 raeburn 2196: $rowcount ++;
1.2 raeburn 2197: foreach my $item (@{$keylist}) {
2198: $in{$item} = $sdata->[$index{$item}];
2199: }
1.11 raeburn 2200: my $role = $in{'role'};
1.2 raeburn 2201: $in{'role'}=&Apache::lonnet::plaintext($sdata->[$index{'role'}]);
2202: if (! defined($in{'start'}) || $in{'start'} == 0) {
2203: $in{'start'} = &mt('none');
2204: } else {
2205: $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
1.1 raeburn 2206: }
1.2 raeburn 2207: if (! defined($in{'end'}) || $in{'end'} == 0) {
2208: $in{'end'} = &mt('none');
1.1 raeburn 2209: } else {
1.2 raeburn 2210: $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
1.1 raeburn 2211: }
1.2 raeburn 2212: if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll') {
2213: $r->print(&Apache::loncommon::start_data_table_row());
1.11 raeburn 2214: my $checkval;
1.16 raeburn 2215: if ($mode eq 'autoenroll') {
2216: my $cellentry;
2217: if ($in{'type'} eq 'auto') {
2218: $cellentry = '<b>'.&mt('auto').'</b> <label><input type="checkbox" name="chgauto" value="'.$in{'username'}.':'.$in{'domain'}.'" /> Change</label>';
2219: $autocount ++;
2220: } else {
2221: $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>';
2222: $manualcount ++;
2223: if ($in{'lockedtype'}) {
2224: $cellentry .= '<label><input type="checkbox" name="unlockchg" value="'.$in{'username'}.':'.$in{'domain'}.'" /> '.&mt('Unlock').'</label>';
2225: $unlockcount ++;
2226: } else {
2227: $cellentry .= '<label><input type="checkbox" name="lockchg" value="'.$in{'username'}.':'.$in{'domain'}.'" /> '.&mt('Lock').'</label>';
2228: $lockcount ++;
1.11 raeburn 2229: }
1.16 raeburn 2230: $cellentry .= '</nobr></td></tr></table>';
2231: }
2232: $r->print("<td>$cellentry</td>\n");
2233: } else {
2234: $r->print("<td>$rowcount</td>\n");
2235: $checkval;
2236: if ($actionselect) {
2237: $checkval = $user;
2238: if ($context eq 'course') {
2239: if ($role eq 'st') {
2240: $checkval .= ':st';
2241: }
2242: $checkval .= ':'.$in{'section'};
2243: if ($role eq 'st') {
2244: $checkval .= ':'.$in{'type'}.':'.$in{'lockedtype'};
2245: }
2246: }
2247: $r->print('<td><input type="checkbox" name="actionlist" value="'.
2248: $checkval.'"></td>');
1.11 raeburn 2249: }
2250: }
1.2 raeburn 2251: foreach my $item (@cols) {
1.10 raeburn 2252: if ($item eq 'username') {
1.16 raeburn 2253: $r->print('<td>'.&print_username_link($mode,$permission,
2254: \%in).'</td>');
2255: } elsif (($item eq 'start' || $item eq 'end') && ($actionselect)) {
1.11 raeburn 2256: $r->print('<td>'.$in{$item}.'<input type="hidden" name="'.$checkval.'_'.$item.'" value="'.$sdata->[$index{$item}].'" /></td>'."\n");
1.10 raeburn 2257: } else {
2258: $r->print('<td>'.$in{$item}.'</td>'."\n");
2259: }
1.2 raeburn 2260: }
1.16 raeburn 2261: if (($context eq 'course') && ($mode ne 'autoenroll')) {
1.4 raeburn 2262: if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
2263: if ($displayclickers eq 'on') {
2264: my $clickers =
1.2 raeburn 2265: (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
1.4 raeburn 2266: if ($clickers!~/\w/) { $clickers='-'; }
2267: $r->print('<td>'.$clickers.'</td>');
1.2 raeburn 2268: } else {
2269: $r->print(' <td> </td> ');
2270: }
1.4 raeburn 2271: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
2272: if ($displayphotos eq 'on' && $sdata->[$index{'role'}] eq 'st') {
2273: my $imgurl =
2274: &Apache::lonnet::retrievestudentphoto($in{'domain'},$in{'username'},
2275: 'gif','thumbnail');
2276: $r->print(' <td align="right"><a href="javascript:photowindow('."'".&Apache::lonnet::studentphoto($in{'domain'},$in{'username'},'jpg')."'".')"><img src="'.$imgurl.'" border="1"></a></td>');
2277: } else {
2278: $r->print(' <td> </td> ');
2279: }
2280: }
1.2 raeburn 2281: }
2282: }
2283: $r->print(&Apache::loncommon::end_data_table_row());
2284: } elsif ($mode eq 'csv') {
2285: next if (! defined($CSVfile));
2286: # no need to bother with $linkto
2287: if (! defined($in{'start'}) || $in{'start'} == 0) {
2288: $in{'start'} = &mt('none');
2289: } else {
2290: $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
2291: }
2292: if (! defined($in{'end'}) || $in{'end'} == 0) {
2293: $in{'end'} = &mt('none');
2294: } else {
2295: $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
2296: }
2297: my @line = ();
2298: foreach my $item (@cols) {
2299: push @line,&Apache::loncommon::csv_translate($in{$item});
2300: }
2301: print $CSVfile '"'.join('","',@line).'"'."\n";
2302: } elsif ($mode eq 'excel') {
2303: my $col = 0;
2304: foreach my $item (@cols) {
2305: if ($item eq 'start' || $item eq 'end') {
2306: if (defined($item) && $item != 0) {
2307: $excel_sheet->write($row,$col++,
2308: &Apache::lonstathelpers::calc_serial($in{item}),
2309: $format->{'date'});
2310: } else {
2311: $excel_sheet->write($row,$col++,'none');
2312: }
2313: } else {
2314: $excel_sheet->write($row,$col++,$in{$item});
2315: }
2316: }
2317: $row++;
1.1 raeburn 2318: }
2319: }
1.2 raeburn 2320: if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll') {
2321: $r->print(&Apache::loncommon::end_data_table().'<br />');
2322: } elsif ($mode eq 'excel') {
2323: $excel_workbook->close();
2324: $r->print('<p><a href="'.$excel_filename.'">'.
2325: &mt('Your Excel spreadsheet').'</a> '.&mt('is ready for download').'.</p>'."\n");
2326: } elsif ($mode eq 'csv') {
2327: close($CSVfile);
2328: $r->print('<a href="'.$CSVfilename.'">'.
2329: &mt('Your CSV file').'</a> is ready for download.'.
2330: "\n");
2331: $r->rflush();
2332: }
2333: if ($mode eq 'autoenroll') {
2334: return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
1.4 raeburn 2335: } else {
2336: return ($usercount);
1.2 raeburn 2337: }
1.1 raeburn 2338: }
2339:
1.10 raeburn 2340: sub print_username_link {
1.16 raeburn 2341: my ($mode,$permission,$in) = @_;
1.10 raeburn 2342: my $output;
1.16 raeburn 2343: if ($mode eq 'autoenroll') {
2344: $output = $in->{'username'};
2345: } elsif (!$permission->{'cusr'}) {
1.10 raeburn 2346: $output = &Apache::loncommon::aboutmewrapper($in->{'username'},
2347: $in->{'username'},
2348: $in->{'domain'});
2349: } else {
2350: $output = '<a href="javascript:username_display_launch('.
2351: "'$in->{'username'}','$in->{'domain'}'".')" />'.
2352: $in->{'username'}.'</a>';
2353: }
2354: return $output;
2355: }
2356:
1.2 raeburn 2357: sub role_type_names {
2358: my %lt = &Apache::lonlocal::texthash (
1.13 raeburn 2359: 'domain' => 'Domain Roles',
2360: 'author' => 'Co-Author Roles',
2361: 'course' => 'Course Roles',
1.2 raeburn 2362: );
2363: return %lt;
2364: }
2365:
1.11 raeburn 2366: sub select_actions {
2367: my ($context,$setting,$statusmode) = @_;
2368: my %lt = &Apache::lonlocal::texthash(
2369: revoke => "Revoke user roles",
2370: delete => "Delete user roles",
2371: reenable => "Re-enable expired user roles",
2372: activate => "Make future user roles active now",
2373: chgdates => "Change starting/ending dates",
2374: chgsec => "Change section associated with user roles",
2375: );
2376: my ($output,$options,%choices);
1.23 raeburn 2377: # FIXME Disable actions for now for roletype=course in domain context
2378: if ($context eq 'domain' && $setting eq 'course') {
2379: return;
2380: }
1.11 raeburn 2381: if ($statusmode eq 'Any') {
2382: $options .= '
2383: <option value="chgdates">'.$lt{'chgdates'}.'</option>';
2384: $choices{'dates'} = 1;
2385: } else {
2386: if ($statusmode eq 'Future') {
2387: $options .= '
2388: <option value="activate">'.$lt{'activate'}.'</option>';
2389: $choices{'dates'} = 1;
2390: } elsif ($statusmode eq 'Expired') {
2391: $options .= '
2392: <option value="reenable">'.$lt{'reenable'}.'</option>';
2393: $choices{'dates'} = 1;
2394: }
1.13 raeburn 2395: if ($statusmode eq 'Active' || $statusmode eq 'Future') {
2396: $options .= '
2397: <option value="chgdates">'.$lt{'chgdates'}.'</option>
2398: <option value="revoke">'.$lt{'revoke'}.'</option>';
2399: $choices{'dates'} = 1;
2400: }
1.11 raeburn 2401: }
2402: if ($context eq 'domain') {
2403: $options .= '
2404: <option value="delete">'.$lt{'delete'}.'</option>';
2405: }
2406: if (($context eq 'course') || ($context eq 'domain' && $setting eq 'course')) {
2407: if ($statusmode ne 'Expired') {
2408: $options .= '
2409: <option value="chgsec">'.$lt{'chgsec'}.'</option>';
2410: $choices{'sections'} = 1;
2411: }
2412: }
2413: if ($options) {
2414: $output = '<select name="bulkaction" onchange="javascript:opendatebrowser(this.form,'."'studentform'".')" />'."\n".
2415: '<option value="" selected="selected">'.
2416: &mt('Please select').'</option>'."\n".$options."\n".'</select>';
2417: if ($choices{'dates'}) {
2418: $output .=
2419: '<input type="hidden" name="startdate_month" value="" />'."\n".
2420: '<input type="hidden" name="startdate_day" value="" />'."\n".
2421: '<input type="hidden" name="startdate_year" value="" />'."\n".
2422: '<input type="hidden" name="startdate_hour" value="" />'."\n".
2423: '<input type="hidden" name="startdate_minute" value="" />'."\n".
2424: '<input type="hidden" name="startdate_second" value="" />'."\n".
2425: '<input type="hidden" name="enddate_month" value="" />'."\n".
2426: '<input type="hidden" name="enddate_day" value="" />'."\n".
2427: '<input type="hidden" name="enddate_year" value="" />'."\n".
2428: '<input type="hidden" name="enddate_hour" value="" />'."\n".
2429: '<input type="hidden" name="enddate_minute" value="" />'."\n".
2430: '<input type="hidden" name="enddate_second" value="" />'."\n";
2431: if ($context eq 'course') {
2432: $output .= '<input type="hidden" name="makedatesdefault" value="" />'."\n";
2433: }
2434: }
2435: if ($choices{'sections'}) {
2436: $output .= '<input type="hidden" name="retainsec" value= "" />'."\n".
2437: '<input type="hidden" name="newsecs" value= "" />'."\n";
2438: }
2439: }
2440: return $output;
2441: }
2442:
2443: sub date_section_javascript {
2444: my ($context,$setting) = @_;
2445: my $title;
2446: if (($context eq 'course') || ($context eq 'domain' && $setting eq 'course')) {
2447: $title = &mt('Date and Section selector');
2448: } else {
2449: $title = &mt('Date selector');
2450: }
2451: my $output = '
2452: <script type="text/javascript">
2453: var stdeditbrowser;'."\n";
2454: $output .= <<"ENDONE";
2455: function opendatebrowser(callingform,formname) {
2456: var bulkaction = callingform.bulkaction.options[callingform.bulkaction.selectedIndex].value;
2457: if (bulkaction == 'revoke' || bulkaction == 'delete' || bulkaction == '') {
2458: return;
2459: }
2460: var url = '/adm/createuser?';
2461: var type = '';
2462: var showrole = callingform.showrole.options[callingform.showrole.selectedIndex].value;
2463: ENDONE
2464: if ($context eq 'domain') {
2465: $output .= '
2466: type = callingform.roletype.options[callingform.roletype.selectedIndex].value;
2467: ';
2468: }
2469: my $width= '700';
2470: my $height = '400';
2471: $output .= <<"ENDTWO";
2472: url += 'action=dateselect&callingform=' + formname +
2473: '&roletype='+type+'&showrole='+showrole +'&bulkaction='+bulkaction;
2474: var title = '$title';
2475: var options = 'scrollbars=1,resizable=1,menubar=0';
2476: options += ',width=$width,height=$height';
2477: stdeditbrowser = open(url,title,options,'1');
2478: stdeditbrowser.focus();
2479: }
2480: </script>
2481: ENDTWO
2482: return $output;
2483: }
2484:
2485: sub date_section_selector {
1.21 raeburn 2486: my ($context,$permission) = @_;
1.11 raeburn 2487: my $callingform = $env{'form.callingform'};
2488: my $formname = 'dateselect';
2489: my $groupslist = &get_groupslist();
2490: my $sec_js = &setsections_javascript($formname,$groupslist);
2491: my $output = <<"END";
2492: <script type="text/javascript">
2493:
2494: $sec_js
2495:
2496: function saveselections(formname) {
2497:
2498: END
2499: if ($env{'form.bulkaction'} eq 'chgsec') {
2500: $output .= <<"END";
2501: opener.document.$callingform.retainsec.value = formname.retainsec.value;
2502: setSections(formname);
2503: if (seccheck == 'ok') {
2504: opener.document.$callingform.newsecs.value = formname.sections.value;
2505: window.close();
2506: }
2507: return;
2508: END
2509: } else {
2510: if ($context eq 'course') {
2511: if (($env{'form.bulkaction'} eq 'reenable') ||
2512: ($env{'form.bulkaction'} eq 'activate') ||
2513: ($env{'form.bulkaction'} eq 'chgdates')) {
2514: $output .= <<"END";
2515:
2516: if (formname.makedatesdefault.checked == true) {
2517: opener.document.$callingform.makedatesdefault.value = 1;
2518: }
2519: else {
2520: opener.document.$callingform.makedatesdefault.value = 0;
2521: }
2522:
2523: END
2524: }
2525: }
2526: $output .= <<"END";
2527: opener.document.$callingform.startdate_month.value = formname.startdate_month.options[formname.startdate_month.selectedIndex].value;
2528: opener.document.$callingform.startdate_day.value = formname.startdate_day.value;
2529: opener.document.$callingform.startdate_year.value = formname.startdate_year.value;
2530: opener.document.$callingform.startdate_hour.value = formname.startdate_hour.options[formname.startdate_hour.selectedIndex].value;
2531: opener.document.$callingform.startdate_minute.value = formname.startdate_minute.value;
2532: opener.document.$callingform.startdate_second.value = formname.startdate_second.value;
2533: opener.document.$callingform.enddate_month.value = formname.enddate_month.options[formname.enddate_month.selectedIndex].value;
2534: opener.document.$callingform.enddate_day.value = formname.enddate_day.value;
2535: opener.document.$callingform.enddate_year.value = formname.enddate_year.value;
2536: opener.document.$callingform.enddate_hour.value = formname.enddate_hour.options[formname.enddate_hour.selectedIndex].value;
2537: opener.document.$callingform.enddate_minute.value = formname.enddate_minute.value;
2538: opener.document.$callingform.enddate_second.value = formname.enddate_second.value;
2539: window.close();
2540: END
2541: }
2542: $output .= '
2543: }
2544: </script>
2545: ';
2546: my %lt = &Apache::lonlocal::texthash (
2547: chac => 'Access dates to apply for selected users',
2548: chse => 'Changes in section affiliation to apply to selected users',
2549: 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.',
2550: 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.',
2551: reta => "Retain each user's current section affiliations?",
2552: dnap => '(Does not apply to student roles).',
2553: );
2554: my ($date_items,$headertext);
2555: if ($env{'form.bulkaction'} eq 'chgsec') {
2556: $headertext = $lt{'chse'};
2557: } else {
2558: $headertext = $lt{'chac'};
2559: my $starttime;
2560: if (($env{'form.bulkaction'} eq 'activate') ||
2561: ($env{'form.bulkaction'} eq 'reenable')) {
2562: $starttime = time;
2563: }
2564: $date_items = &date_setting_table($starttime,undef,$context,
1.21 raeburn 2565: $env{'form.bulkaction'},$formname,
2566: $permission);
1.11 raeburn 2567: }
2568: $output .= '<h3>'.$headertext.'</h3>'.
2569: '<form name="'.$formname.'" method="post">'."\n".
2570: $date_items;
2571: if ($context eq 'course' && $env{'form.bulkaction'} eq 'chgsec') {
1.17 raeburn 2572: my ($cnum,$cdom) = &get_course_identity();
1.11 raeburn 2573: my %sections_count =
2574: &Apache::loncommon::get_sections($cdom,$cnum);
2575: my $info;
2576: if ($env{'form.showrole'} eq 'st') {
2577: $output .= '<p>'.$lt{'fors'}.'</p>';
2578: } elsif ($env{'form.shorole'} eq 'Any') {
2579: $output .= '<p>'.$lt{'fors'}.'</p>'.
2580: '<p>'.$lt{'forn'}.' ';
2581: $info = $lt{'reta'};
2582: } else {
2583: $output .= '<p>'.$lt{'forn'}.' ';
2584: $info = $lt{'reta'};
2585: }
2586: if ($info) {
2587: $info .= '<span class="LC_nobreak">'.
2588: '<label><input type="radio" name="retainsec" value="1" '.
2589: 'checked="checked" />'.&mt('Yes').'</label> '.
2590: '<label><input type="radio" name="retainsec" value="0" />'.
2591: &mt('No').'</label></span>';
2592: if ($env{'form.showrole'} eq 'Any') {
2593: $info .= '<br />'.$lt{'dnap'};
2594: }
2595: $info .= '</p>';
2596: } else {
2597: $info = '<input type="hidden" name="retainsec" value="0" />';
2598: }
2599: my $sections_select .= &course_sections(\%sections_count,$env{'form.showrole'});
1.21 raeburn 2600: my $rowtitle = &mt('New section to assign');
2601: my $secbox = §ion_picker($cdom,$cnum,$env{'form.showrole'},$rowtitle,$permission,$context);
1.11 raeburn 2602: $output .= $info.$secbox;
2603: }
2604: $output .= '<p>'.
2605: &mt('Use "Save" to update the main window with your selections.').'<br /><br />'.
2606: '<input type="button" name="dateselection" value="'.&mt('Save').'" onclick="javascript:saveselections(this.form)" /></p>'."\n".
2607: '</form>';
2608: return $output;
2609: }
2610:
1.17 raeburn 2611: sub section_picker {
2612: my ($cdom,$cnum,$role,$rowtitle,$permission,$context,$mode) = @_;
2613: my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
2614: my $sections_select .= &course_sections(\%sections_count,$role);
2615: my $secbox = '<p>'.&Apache::lonhtmlcommon::start_pick_box()."\n";
2616: if ($mode eq 'upload') {
2617: my ($options,$cb_script,$coursepick) =
2618: &default_role_selector($context,1);
2619: $secbox .= &Apache::lonhtmlcommon::row_title('role','LC_oddrow_value').
2620: $options. &Apache::lonhtmlcommon::row_closure(1)."\n";
2621: }
2622: $secbox .= &Apache::lonhtmlcommon::row_title($rowtitle,'LC_oddrow_value')."\n";
2623: if ($env{'request.course.sec'} eq '') {
2624: $secbox .= '<table class="LC_createuser"><tr class="LC_section_row">'."\n".
2625: '<td align="center">'.&mt('Existing sections')."\n".
2626: '<br />'.$sections_select.'</td><td align="center">'.
2627: &mt('New section').'<br />'."\n".
2628: '<input type="text" name="newsec" size="15" />'."\n".
2629: '<input type="hidden" name="sections" value="" />'."\n".
2630: '</td></tr></table>'."\n";
2631: } else {
2632: $secbox .= '<input type="hidden" name="sections" value="'.
2633: $env{'request.course.sec'}.'" />'.
2634: $env{'request.course.sec'};
2635: }
2636: $secbox .= &Apache::lonhtmlcommon::row_closure(1)."\n".
2637: &Apache::lonhtmlcommon::end_pick_box().'</p>';
2638: return $secbox;
2639: }
2640:
1.2 raeburn 2641: sub results_header_row {
1.24 ! raeburn 2642: my ($rolefilter,$statusmode,$context,$permission,$mode) = @_;
1.5 raeburn 2643: my ($description,$showfilter);
2644: if ($rolefilter ne 'Any') {
2645: $showfilter = $rolefilter;
2646: }
1.2 raeburn 2647: if ($context eq 'course') {
1.24 ! raeburn 2648: if ($mode eq 'csv' || $mode eq 'excel') {
! 2649: $description = &mt('Course - ').$env{'course.'.$env{'request.course.id'}.'.description'}.': ';
! 2650: }
1.2 raeburn 2651: if ($statusmode eq 'Expired') {
1.5 raeburn 2652: $description .= &mt('Users in course with expired [_1] roles',$showfilter);
1.11 raeburn 2653: } elsif ($statusmode eq 'Future') {
1.5 raeburn 2654: $description .= &mt('Users in course with future [_1] roles',$showfilter);
1.2 raeburn 2655: } elsif ($statusmode eq 'Active') {
1.5 raeburn 2656: $description .= &mt('Users in course with active [_1] roles',$showfilter);
1.2 raeburn 2657: } else {
2658: if ($rolefilter eq 'Any') {
2659: $description .= &mt('All users in course');
2660: } else {
2661: $description .= &mt('All users in course with [_1] roles',$rolefilter);
2662: }
2663: }
1.15 raeburn 2664: if (exists($permission->{'view_section'})) {
2665: if ($env{'form.showrole'} eq 'st') {
2666: $description .= ' '.&mt('(section [_1] only)',$permission->{'view_section'});
2667: } elsif ($env{'form.showrole'} eq 'any') {
2668: $description .= ' '.&mt('(section [_1] only)',$permission->{'view_section'});
2669: }
2670: }
1.13 raeburn 2671: } elsif ($context eq 'author') {
1.14 raeburn 2672: $description =
2673: &mt('Author space for <span class="LC_cusr_emph">[_1]</span>',
2674: &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'})).': ';
1.2 raeburn 2675: if ($statusmode eq 'Expired') {
1.5 raeburn 2676: $description .= &mt('Co-authors with expired [_1] roles',$showfilter);
1.2 raeburn 2677: } elsif ($statusmode eq 'Future') {
1.5 raeburn 2678: $description .= &mt('Co-authors with future [_1] roles',$showfilter);
1.2 raeburn 2679: } elsif ($statusmode eq 'Active') {
1.5 raeburn 2680: $description .= &mt('Co-authors with active [_1] roles',$showfilter);
1.2 raeburn 2681: } else {
2682: if ($rolefilter eq 'Any') {
1.5 raeburn 2683: $description .= &mt('All co-authors');
1.2 raeburn 2684: } else {
2685: $description .= &mt('All co-authors with [_1] roles',$rolefilter);
2686: }
2687: }
2688: } elsif ($context eq 'domain') {
2689: my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
2690: $description = &mt('Domain - ').$domdesc.': ';
2691: if ($env{'form.roletype'} eq 'domain') {
2692: if ($statusmode eq 'Expired') {
1.5 raeburn 2693: $description .= &mt('Users in domain with expired [_1] roles',$showfilter);
1.2 raeburn 2694: } elsif ($statusmode eq 'Future') {
1.5 raeburn 2695: $description .= &mt('Users in domain with future [_1] roles',$showfilter);
1.2 raeburn 2696: } elsif ($statusmode eq 'Active') {
1.5 raeburn 2697: $description .= &mt('Users in domain with active [_1] roles',$showfilter);
1.2 raeburn 2698: } else {
2699: if ($rolefilter eq 'Any') {
1.5 raeburn 2700: $description .= &mt('All users in domain');
1.2 raeburn 2701: } else {
2702: $description .= &mt('All users in domain with [_1] roles',$rolefilter);
2703: }
2704: }
1.13 raeburn 2705: } elsif ($env{'form.roletype'} eq 'author') {
1.2 raeburn 2706: if ($statusmode eq 'Expired') {
1.5 raeburn 2707: $description .= &mt('Co-authors in domain with expired [_1] roles',$showfilter);
1.2 raeburn 2708: } elsif ($statusmode eq 'Future') {
1.5 raeburn 2709: $description .= &mt('Co-authors in domain with future [_1] roles',$showfilter);
1.2 raeburn 2710: } elsif ($statusmode eq 'Active') {
1.5 raeburn 2711: $description .= &mt('Co-authors in domain with active [_1] roles',$showfilter);
1.2 raeburn 2712: } else {
2713: if ($rolefilter eq 'Any') {
1.5 raeburn 2714: $description .= &mt('All users with co-author roles in domain',$showfilter);
1.2 raeburn 2715: } else {
2716: $description .= &mt('All co-authors in domain with [_1] roles',$rolefilter);
2717: }
2718: }
2719: } elsif ($env{'form.roletype'} eq 'course') {
2720: my $coursefilter = $env{'form.coursepick'};
2721: if ($coursefilter eq 'category') {
2722: my $instcode = &instcode_from_coursefilter();
2723: if ($instcode eq '.') {
2724: $description .= &mt('All courses in domain').' - ';
2725: } else {
2726: $description .= &mt('Courses in domain with institutional code: [_1]',$instcode).' - ';
2727: }
2728: } elsif ($coursefilter eq 'selected') {
2729: $description .= &mt('Selected courses in domain').' - ';
2730: } elsif ($coursefilter eq 'all') {
2731: $description .= &mt('All courses in domain').' - ';
2732: }
2733: if ($statusmode eq 'Expired') {
1.5 raeburn 2734: $description .= &mt('users with expired [_1] roles',$showfilter);
1.2 raeburn 2735: } elsif ($statusmode eq 'Future') {
1.5 raeburn 2736: $description .= &mt('users with future [_1] roles',$showfilter);
1.2 raeburn 2737: } elsif ($statusmode eq 'Active') {
1.5 raeburn 2738: $description .= &mt('users with active [_1] roles',$showfilter);
1.2 raeburn 2739: } else {
2740: if ($rolefilter eq 'Any') {
2741: $description .= &mt('all users');
2742: } else {
2743: $description .= &mt('users with [_1] roles',$rolefilter);
2744: }
2745: }
2746: }
2747: }
2748: return $description;
2749: }
1.22 raeburn 2750:
2751: sub viewable_section {
2752: my ($permission) = @_;
2753: my $viewablesec;
2754: if (ref($permission) eq 'HASH') {
2755: if (exists($permission->{'view_section'})) {
2756: $viewablesec = $permission->{'view_section'};
2757: } elsif (exists($permission->{'cusr_section'})) {
2758: $viewablesec = $permission->{'cusr_section'};
2759: }
2760: }
2761: return $viewablesec;
2762: }
2763:
1.2 raeburn 2764:
1.1 raeburn 2765: #################################################
2766: #################################################
2767: sub show_drop_list {
1.17 raeburn 2768: my ($r,$classlist,$keylist,$nosort,$permission)=@_;
1.1 raeburn 2769: my $cid=$env{'request.course.id'};
1.17 raeburn 2770: my ($cnum,$cdom) = &get_course_identity($cid);
1.1 raeburn 2771: if (! exists($env{'form.sortby'})) {
2772: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
2773: ['sortby']);
2774: }
2775: my $sortby = $env{'form.sortby'};
2776: if ($sortby !~ /^(username|domain|section|groups|fullname|id|start|end)$/) {
2777: $sortby = 'username';
2778: }
2779: my ($classgroups) = &Apache::loncoursedata::get_group_memberships(
2780: $classlist,$keylist,$cdom,$cnum);
2781: #
2782: my $action = "drop";
1.17 raeburn 2783: my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
1.1 raeburn 2784: $r->print(<<END);
2785: <input type="hidden" name="sortby" value="$sortby" />
2786: <input type="hidden" name="action" value="$action" />
2787: <input type="hidden" name="state" value="done" />
1.17 raeburn 2788: <script type="text/javascript" language="Javascript">
2789: $check_uncheck_js
1.1 raeburn 2790: </script>
2791: <p>
2792: <input type="hidden" name="phase" value="four">
2793: END
2794:
2795: my %lt=&Apache::lonlocal::texthash('usrn' => "username",
2796: 'dom' => "domain",
2797: 'sn' => "student name",
2798: 'sec' => "section",
2799: 'start' => "start date",
2800: 'end' => "end date",
2801: 'groups' => "active groups",
2802: );
2803: if ($nosort) {
1.17 raeburn 2804: $r->print(&Apache::loncommon::start_data_table().
2805: &Apache::loncommon::start_data_table_header_row());
1.1 raeburn 2806: $r->print(<<END);
2807: <th> </th>
2808: <th>$lt{'usrn'}</th>
2809: <th>$lt{'dom'}</th>
2810: <th>ID</th>
2811: <th>$lt{'sn'}</th>
2812: <th>$lt{'sec'}</th>
2813: <th>$lt{'start'}</th>
2814: <th>$lt{'end'}</th>
2815: <th>$lt{'groups'}</th>
2816: END
1.17 raeburn 2817: $r->print(&Apache::loncommon::end_data_table_header_row());
1.1 raeburn 2818: } else {
1.17 raeburn 2819: $r->print(&Apache::loncommon::start_data_table().
2820: &Apache::loncommon::start_data_table_header_row());
1.1 raeburn 2821: $r->print(<<END);
1.17 raeburn 2822: <th> </th>
1.1 raeburn 2823: <th>
1.17 raeburn 2824: <a href="/adm/createuser?action=$action&sortby=username">$lt{'usrn'}</a>
1.1 raeburn 2825: </th><th>
1.17 raeburn 2826: <a href="/adm/createuser?action=$action&sortby=domain">$lt{'dom'}</a>
1.1 raeburn 2827: </th><th>
1.17 raeburn 2828: <a href="/adm/createuser?action=$action&sortby=id">ID</a>
1.1 raeburn 2829: </th><th>
1.17 raeburn 2830: <a href="/adm/createuser?action=$action&sortby=fullname">$lt{'sn'}</a>
1.1 raeburn 2831: </th><th>
1.17 raeburn 2832: <a href="/adm/createuser?action=$action&sortby=section">$lt{'sec'}</a>
1.1 raeburn 2833: </th><th>
1.17 raeburn 2834: <a href="/adm/createuser?action=$action&sortby=start">$lt{'start'}</a>
1.1 raeburn 2835: </th><th>
1.17 raeburn 2836: <a href="/adm/createuser?action=$action&sortby=end">$lt{'end'}</a>
1.1 raeburn 2837: </th><th>
1.17 raeburn 2838: <a href="/adm/createuser?action=$action&sortby=groups">$lt{'groups'}</a>
1.1 raeburn 2839: </th>
2840: END
1.17 raeburn 2841: $r->print(&Apache::loncommon::end_data_table_header_row());
1.1 raeburn 2842: }
2843: #
2844: # Sort the students
2845: my %index;
2846: my $i;
2847: foreach (@$keylist) {
2848: $index{$_} = $i++;
2849: }
2850: $index{'groups'} = scalar(@$keylist);
2851: my $index = $index{$sortby};
2852: my $second = $index{'username'};
2853: my $third = $index{'domain'};
2854: my @Sorted_Students = sort {
2855: lc($classlist->{$a}->[$index]) cmp lc($classlist->{$b}->[$index])
2856: ||
2857: lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
2858: ||
2859: lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
2860: } (keys(%$classlist));
2861: foreach my $student (@Sorted_Students) {
2862: my $error;
2863: my $sdata = $classlist->{$student};
2864: my $username = $sdata->[$index{'username'}];
2865: my $domain = $sdata->[$index{'domain'}];
2866: my $section = $sdata->[$index{'section'}];
2867: my $name = $sdata->[$index{'fullname'}];
2868: my $id = $sdata->[$index{'id'}];
2869: my $start = $sdata->[$index{'start'}];
2870: my $end = $sdata->[$index{'end'}];
2871: my $groups = $classgroups->{$student};
2872: my $active_groups;
2873: if (ref($groups->{active}) eq 'HASH') {
2874: $active_groups = join(', ',keys(%{$groups->{'active'}}));
2875: }
2876: if (! defined($start) || $start == 0) {
2877: $start = &mt('none');
2878: } else {
2879: $start = &Apache::lonlocal::locallocaltime($start);
2880: }
2881: if (! defined($end) || $end == 0) {
2882: $end = &mt('none');
2883: } else {
2884: $end = &Apache::lonlocal::locallocaltime($end);
2885: }
2886: my $status = $sdata->[$index{'status'}];
2887: next if ($status ne 'Active');
1.17 raeburn 2888: if ($env{'request.course.sec'} ne '') {
2889: if ($section ne $env{'request.course.sec'}) {
2890: next;
2891: }
2892: }
2893: my $studentkey = $student.':'.$section;
2894: my $startitem = '<input type="hidden" name="'.$studentkey.'_start" value="'.$sdata->[$index{'start'}].'" />';
1.1 raeburn 2895: #
2896: $r->print(&Apache::loncommon::start_data_table_row());
2897: $r->print(<<"END");
2898: <td><input type="checkbox" name="droplist" value="$student"></td>
2899: <td>$username</td>
2900: <td>$domain</td>
2901: <td>$id</td>
2902: <td>$name</td>
2903: <td>$section</td>
2904: <td>$start</td>
2905: <td>$end</td>
2906: <td>$active_groups</td>
2907: END
2908: $r->print(&Apache::loncommon::end_data_table_row());
2909: }
2910: $r->print(&Apache::loncommon::end_data_table().'<br />');
2911: %lt=&Apache::lonlocal::texthash(
2912: 'dp' => "Expire Users' Roles",
2913: 'ca' => "check all",
2914: 'ua' => "uncheck all",
2915: );
2916: $r->print(<<"END");
2917: </p><p>
2918: <input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.studentform.droplist)">
2919: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.studentform.droplist)">
2920: <p><input type=submit value="$lt{'dp'}"></p>
2921: END
2922: return;
2923: }
2924:
2925: #
2926: # Print out the initial form to get the file containing a list of users
2927: #
2928: sub print_first_users_upload_form {
2929: my ($r,$context) = @_;
2930: my $str;
2931: $str = '<input type="hidden" name="phase" value="two">';
2932: $str .= '<input type="hidden" name="action" value="upload" />';
2933: $str .= '<input type="hidden" name="state" value="got_file" />';
1.2 raeburn 2934: $str .= "<h3>".&mt('Upload a file containing information about users')."</h3>\n";
1.1 raeburn 2935: $str .= &Apache::loncommon::upfile_select_html();
2936: $str .= "<p>\n";
2937: $str .= '<input type="submit" name="fileupload" value="'.
1.2 raeburn 2938: &mt('Upload file of users').'">'."\n";
1.1 raeburn 2939: $str .= '<label><input type="checkbox" name="noFirstLine" /> '.
2940: &mt('Ignore First Line')."</label></p>\n";
2941: $str .= &Apache::loncommon::help_open_topic("Course_Create_Class_List",
2942: &mt("How do I create a users list from a spreadsheet")).
2943: "<br />\n";
2944: $str .= &Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
2945: &mt("How do I create a CSV file from a spreadsheet")).
2946: "<br />\n";
2947: $str .= &Apache::loncommon::end_page();
2948: $r->print($str);
2949: return;
2950: }
2951:
2952: # ================================================= Drop/Add from uploaded file
2953: sub upfile_drop_add {
1.21 raeburn 2954: my ($r,$context,$permission) = @_;
1.1 raeburn 2955: &Apache::loncommon::load_tmp_file($r);
2956: my @userdata=&Apache::loncommon::upfile_record_sep();
2957: if($env{'form.noFirstLine'}){shift(@userdata);}
2958: my @keyfields = split(/\,/,$env{'form.keyfields'});
2959: my %fields=();
2960: for (my $i=0; $i<=$env{'form.nfields'}; $i++) {
2961: if ($env{'form.upfile_associate'} eq 'reverse') {
2962: if ($env{'form.f'.$i} ne 'none') {
2963: $fields{$keyfields[$i]}=$env{'form.f'.$i};
2964: }
2965: } else {
2966: $fields{$env{'form.f'.$i}}=$keyfields[$i];
2967: }
2968: }
2969: #
2970: # Store the field choices away
2971: foreach my $field (qw/username names
2972: fname mname lname gen id sec ipwd email role/) {
2973: $env{'form.'.$field.'_choice'}=$fields{$field};
2974: }
2975: &Apache::loncommon::store_course_settings('enrollment_upload',
2976: { 'username_choice' => 'scalar',
2977: 'names_choice' => 'scalar',
2978: 'fname_choice' => 'scalar',
2979: 'mname_choice' => 'scalar',
2980: 'lname_choice' => 'scalar',
2981: 'gen_choice' => 'scalar',
2982: 'id_choice' => 'scalar',
2983: 'sec_choice' => 'scalar',
2984: 'ipwd_choice' => 'scalar',
2985: 'email_choice' => 'scalar',
2986: 'role_choice' => 'scalar' });
2987: #
2988: my ($startdate,$enddate) = &get_dates_from_form();
2989: if ($env{'form.makedatesdefault'}) {
2990: $r->print(&make_dates_default($startdate,$enddate));
2991: }
2992: # Determine domain and desired host (home server)
2993: my $domain=$env{'request.role.domain'};
2994: my $desiredhost = $env{'form.lcserver'};
2995: if (lc($desiredhost) eq 'default') {
2996: $desiredhost = undef;
2997: } else {
2998: my %home_servers = &Apache::lonnet::get_servers($domain,'library');
2999: if (! exists($home_servers{$desiredhost})) {
3000: $r->print('<span class="LC_error">'.&mt('Error').
3001: &mt('Invalid home server specified').'</span>');
3002: $r->print(&Apache::loncommon::end_page());
3003: return;
3004: }
3005: }
3006: # Determine authentication mechanism
3007: my $changeauth;
3008: if ($context eq 'domain') {
3009: $changeauth = $env{'form.changeauth'};
3010: }
3011: my $amode = '';
3012: my $genpwd = '';
3013: if ($env{'form.login'} eq 'krb') {
3014: $amode='krb';
3015: $amode.=$env{'form.krbver'};
3016: $genpwd=$env{'form.krbarg'};
3017: } elsif ($env{'form.login'} eq 'int') {
3018: $amode='internal';
3019: if ((defined($env{'form.intarg'})) && ($env{'form.intarg'})) {
3020: $genpwd=$env{'form.intarg'};
3021: }
3022: } elsif ($env{'form.login'} eq 'loc') {
3023: $amode='localauth';
3024: if ((defined($env{'form.locarg'})) && ($env{'form.locarg'})) {
3025: $genpwd=$env{'form.locarg'};
3026: }
3027: }
3028: if ($amode =~ /^krb/) {
3029: if (! defined($genpwd) || $genpwd eq '') {
3030: $r->print('<span class="Error">'.
3031: &mt('Unable to enroll users').' '.
3032: &mt('No Kerberos domain was specified.').'</span></p>');
3033: $amode = ''; # This causes the loop below to be skipped
3034: }
3035: }
3036: my ($cid,$defaultsec,$defaultrole,$setting);
3037: if ($context eq 'domain') {
3038: $setting = $env{'form.roleaction'};
3039: if ($setting eq 'domain') {
3040: $defaultrole = $env{'form.defaultrole'};
3041: } elsif ($setting eq 'course') {
3042: $defaultrole = $env{'form.courserole'};
3043: }
1.13 raeburn 3044: } elsif ($context eq 'author') {
1.1 raeburn 3045: $defaultrole = $env{'form.defaultrole'};
3046: }
3047: if ($context eq 'domain' && $setting eq 'course') {
3048: if ($env{'form.newsec'} ne '') {
3049: $defaultsec = $env{'form.newsec'};
3050: } elsif ($env{'form.defaultsec'} ne '') {
3051: $defaultsec = $env{'form.defaultsec'}
3052: }
3053: }
3054: if ($env{'request.course.id'} ne '') {
3055: $cid = $env{'request.course.id'};
3056: } elsif ($env{'form.defaultdomain'} ne '' && $env{'form.defaultcourse'} ne '') {
3057: $cid = $env{'form.defaultdomain'}.'_'.
3058: $env{'form.defaultcourse'};
3059: }
3060: if ( $domain eq &LONCAPA::clean_domain($domain)
3061: && ($amode ne '')) {
3062: #######################################
3063: ## Add/Modify Users ##
3064: #######################################
3065: if ($context eq 'course') {
3066: $r->print('<h3>'.&mt('Enrolling Users')."</h3>\n<p>\n");
1.13 raeburn 3067: } elsif ($context eq 'author') {
1.1 raeburn 3068: $r->print('<h3>'.&mt('Updating Co-authors')."</h3>\n<p>\n");
3069: } else {
3070: $r->print('<h3>'.&mt('Adding/Modifying Users')."</h3>\n<p>\n");
3071: }
3072: my %counts = (
3073: user => 0,
3074: auth => 0,
3075: role => 0,
3076: );
3077: my $flushc=0;
3078: my %student=();
3079: my %curr_groups;
3080: my %userchg;
3081: if ($context eq 'course') {
3082: # Get information about course groups
3083: %curr_groups = &Apache::longroup::coursegroups();
3084: }
1.5 raeburn 3085: my (%curr_rules,%got_rules,%alerts);
1.1 raeburn 3086: # Get new users list
3087: foreach (@userdata) {
3088: my %entries=&Apache::loncommon::record_sep($_);
3089: # Determine user name
3090: unless (($entries{$fields{'username'}} eq '') ||
3091: (!defined($entries{$fields{'username'}}))) {
3092: my ($fname, $mname, $lname,$gen) = ('','','','');
3093: if (defined($fields{'names'})) {
3094: ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
3095: /([^\,]+)\,\s*(\w+)\s*(.*)$/);
3096: } else {
3097: if (defined($fields{'fname'})) {
3098: $fname=$entries{$fields{'fname'}};
3099: }
3100: if (defined($fields{'mname'})) {
3101: $mname=$entries{$fields{'mname'}};
3102: }
3103: if (defined($fields{'lname'})) {
3104: $lname=$entries{$fields{'lname'}};
3105: }
3106: if (defined($fields{'gen'})) {
3107: $gen=$entries{$fields{'gen'}};
3108: }
3109: }
3110: if ($entries{$fields{'username'}}
3111: ne &LONCAPA::clean_username($entries{$fields{'username'}})) {
3112: $r->print('<br />'.
3113: &mt('<b>[_1]</b>: Unacceptable username for user [_2] [_3] [_4] [_5]',
3114: $entries{$fields{'username'}},$fname,$mname,$lname,$gen).
3115: '</b>');
3116: } else {
1.5 raeburn 3117: my $username = $entries{$fields{'username'}};
1.1 raeburn 3118: my $sec;
3119: if ($context eq 'course' || $setting eq 'course') {
3120: # determine section number
3121: if (defined($fields{'sec'})) {
3122: if (defined($entries{$fields{'sec'}})) {
3123: $sec=$entries{$fields{'sec'}};
3124: }
3125: } else {
3126: $sec = $defaultsec;
3127: }
3128: # remove non alphanumeric values from section
3129: $sec =~ s/\W//g;
3130: if ($sec eq "none" || $sec eq 'all') {
3131: $r->print('<br />'.
3132: &mt('<b>[_1]</b>: Unable to enroll: section name "[_2]" for user [_3] [_4] [_5] [_6] is a reserved word.',
3133: $username,$sec,$fname,$mname,$lname,$gen));
3134: next;
3135: } elsif (($sec ne '') && (exists($curr_groups{$sec}))) {
3136: $r->print('<br />'.
3137: &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.',
3138: $username,$sec,$fname,$mname,$lname,$gen));
3139: next;
3140: }
3141: }
3142: # determine id number
3143: my $id='';
3144: if (defined($fields{'id'})) {
3145: if (defined($entries{$fields{'id'}})) {
3146: $id=$entries{$fields{'id'}};
3147: }
3148: $id=~tr/A-Z/a-z/;
3149: }
3150: # determine email address
3151: my $email='';
3152: if (defined($fields{'email'})) {
3153: if (defined($entries{$fields{'email'}})) {
3154: $email=$entries{$fields{'email'}};
3155: unless ($email=~/^[^\@]+\@[^\@]+$/) { $email=''; } }
3156: }
3157: # determine user password
3158: my $password = $genpwd;
3159: if (defined($fields{'ipwd'})) {
3160: if ($entries{$fields{'ipwd'}}) {
3161: $password=$entries{$fields{'ipwd'}};
3162: }
3163: }
3164: # determine user role
3165: my $role = '';
3166: if (defined($fields{'role'})) {
3167: if ($entries{$fields{'role'}}) {
1.2 raeburn 3168: my @poss_roles =
1.1 raeburn 3169: &curr_role_permissions($context,$setting);
1.2 raeburn 3170: if (grep(/^\Q$entries{$fields{'role'}}\E/,@poss_roles)) {
1.1 raeburn 3171: $role=$entries{$fields{'role'}};
3172: } else {
3173: my $rolestr = join(', ',@poss_roles);
3174: $r->print('<br />'.
3175: &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");
3176: next;
3177: }
3178: }
3179: }
3180: if ($role eq '') {
3181: $role = $defaultrole;
3182: }
3183: # Clean up whitespace
3184: foreach (\$domain,\$username,\$id,\$fname,\$mname,
3185: \$lname,\$gen,\$sec,\$role) {
3186: $$_ =~ s/(\s+$|^\s+)//g;
3187: }
1.5 raeburn 3188: # check against rules
3189: my $checkid = 0;
3190: my $newuser = 0;
3191: my (%rulematch,%inst_results,%idinst_results);
3192: my $uhome=&Apache::lonnet::homeserver($username,$domain);
3193: if ($uhome eq 'no_host') {
3194: $checkid = 1;
3195: $newuser = 1;
3196: my $checkhash;
3197: my $checks = { 'username' => 1 };
3198: $checkhash->{$username.':'.$domain} = { 'newuser' => 1, };
3199: &Apache::loncommon::user_rule_check($checkhash,$checks,
3200: \%alerts,\%rulematch,\%inst_results,\%curr_rules,
3201: \%got_rules);
3202: if (ref($alerts{'username'}) eq 'HASH') {
3203: if (ref($alerts{'username'}{$domain}) eq 'HASH') {
3204: next if ($alerts{'username'}{$domain}{$username});
3205: }
3206: }
1.13 raeburn 3207: } else {
3208: # FIXME check if user info can be updated.
1.5 raeburn 3209: }
3210: if ($id ne '') {
3211: if (!$newuser) {
3212: my %idhash = &Apache::lonnet::idrget($domain,($username));
3213: if ($idhash{$username} ne $id) {
3214: $checkid = 1;
3215: }
3216: }
3217: if ($checkid) {
3218: my $checkhash;
3219: my $checks = { 'id' => 1 };
3220: $checkhash->{$username.':'.$domain} = { 'newuser' => $newuser,
3221: 'id' => $id };
3222: &Apache::loncommon::user_rule_check($checkhash,$checks,
3223: \%alerts,\%rulematch,\%idinst_results,\%curr_rules,
3224: \%got_rules);
3225: if (ref($alerts{'id'}) eq 'HASH') {
3226: if (ref($alerts{'id'}{$domain}) eq 'HASH') {
3227: next if ($alerts{'id'}{$domain}{$id});
3228: }
3229: }
3230: }
3231: }
1.1 raeburn 3232: if ($password || $env{'form.login'} eq 'loc') {
3233: my ($userresult,$authresult,$roleresult);
3234: if ($role eq 'st') {
3235: &modifystudent($domain,$username,$cid,$sec,
3236: $desiredhost);
3237: $roleresult =
3238: &Apache::lonnet::modifystudent
3239: ($domain,$username,$id,$amode,$password,
3240: $fname,$mname,$lname,$gen,$sec,$enddate,
3241: $startdate,$env{'form.forceid'},
3242: $desiredhost,$email);
3243: } else {
3244: ($userresult,$authresult,$roleresult) =
3245: &modifyuserrole($context,$setting,
3246: $changeauth,$cid,$domain,$username,
3247: $id,$amode,$password,$fname,
3248: $mname,$lname,$gen,$sec,
3249: $env{'form.forceid'},$desiredhost,
1.5 raeburn 3250: $email,$role,$enddate,$startdate,$checkid);
1.1 raeburn 3251: }
3252: $flushc =
3253: &user_change_result($r,$userresult,$authresult,
3254: $roleresult,\%counts,$flushc,
3255: $username,%userchg);
3256: } else {
3257: if ($context eq 'course') {
3258: $r->print('<br />'.
3259: &mt('<b>[_1]</b>: Unable to enroll. No password specified.',$username)
3260: );
1.13 raeburn 3261: } elsif ($context eq 'author') {
1.1 raeburn 3262: $r->print('<br />'.
3263: &mt('<b>[_1]</b>: Unable to add co-author. No password specified.',$username)
3264: );
3265: } else {
3266: $r->print('<br />'.
3267: &mt('<b>[_1]</b>: Unable to add user. No password specified.',$username)
3268: );
3269: }
3270: }
3271: }
3272: }
3273: } # end of foreach (@userdata)
3274: # Flush the course logs so reverse user roles immediately updated
1.5 raeburn 3275: &Apache::lonnet::flushcourselogs();
1.1 raeburn 3276: $r->print("</p>\n<p>\n".&mt('Processed [_1] user(s).',$counts{'user'}).
3277: "</p>\n");
3278: if ($counts{'role'} > 0) {
3279: $r->print("<p>\n".
3280: &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");
3281: }
3282: if ($counts{'auth'} > 0) {
3283: $r->print("<p>\n".
3284: &mt('Authentication changed for [_1] existing users.',
3285: $counts{'auth'})."</p>\n");
3286: }
1.13 raeburn 3287: $r->print(&print_namespacing_alerts($domain,\%alerts,\%curr_rules));
1.1 raeburn 3288: $r->print('<form name="uploadresult" action="/adm/createuser">');
3289: $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','prevphase','currstate']));
3290: $r->print('</form>');
3291: #####################################
3292: # Drop students #
3293: #####################################
3294: if ($env{'form.fullup'} eq 'yes') {
3295: $r->print('<h3>'.&mt('Dropping Students')."</h3>\n");
3296: # Get current classlist
3297: my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
3298: if (! defined($classlist)) {
3299: $r->print(&mt('There are no students currently enrolled.').
3300: "\n");
3301: } else {
3302: # Remove the students we just added from the list of students.
3303: foreach (@userdata) {
3304: my %entries=&Apache::loncommon::record_sep($_);
3305: unless (($entries{$fields{'username'}} eq '') ||
3306: (!defined($entries{$fields{'username'}}))) {
3307: delete($classlist->{$entries{$fields{'username'}}.
3308: ':'.$domain});
3309: }
3310: }
3311: # Print out list of dropped students.
3312: &show_drop_list($r,$classlist,$keylist,'nosort');
3313: }
3314: }
3315: } # end of unless
3316: }
3317:
1.13 raeburn 3318: sub print_namespacing_alerts {
3319: my ($domain,$alerts,$curr_rules) = @_;
3320: my $output;
3321: if (ref($alerts) eq 'HASH') {
3322: if (keys(%{$alerts}) > 0) {
3323: if (ref($alerts->{'username'}) eq 'HASH') {
3324: foreach my $dom (sort(keys(%{$alerts->{'username'}}))) {
3325: my $count;
3326: if (ref($alerts->{'username'}{$dom}) eq 'HASH') {
3327: $count = keys(%{$alerts->{'username'}{$dom}});
3328: }
3329: my $domdesc = &Apache::lonnet::domain($domain,'description');
3330: if (ref($curr_rules->{$dom}) eq 'HASH') {
3331: $output .= &Apache::loncommon::instrule_disallow_msg(
3332: 'username',$domdesc,$count,'upload');
3333: }
3334: $output .= &Apache::loncommon::user_rule_formats($dom,
3335: $domdesc,$curr_rules->{$dom}{'username'},
3336: 'username');
3337: }
3338: }
3339: if (ref($alerts->{'id'}) eq 'HASH') {
3340: foreach my $dom (sort(keys(%{$alerts->{'id'}}))) {
3341: my $count;
3342: if (ref($alerts->{'id'}{$dom}) eq 'HASH') {
3343: $count = keys(%{$alerts->{'id'}{$dom}});
3344: }
3345: my $domdesc = &Apache::lonnet::domain($domain,'description');
3346: if (ref($curr_rules->{$dom}) eq 'HASH') {
3347: $output .= &Apache::loncommon::instrule_disallow_msg(
3348: 'id',$domdesc,$count,'upload');
3349: }
3350: $output .= &Apache::loncommon::user_rule_formats($dom,
3351: $domdesc,$curr_rules->{$dom}{'id'},'id');
3352: }
3353: }
3354: }
3355: }
3356: }
3357:
1.1 raeburn 3358: sub user_change_result {
3359: my ($r,$userresult,$authresult,$roleresult,$counts,$flushc,$username,
3360: $userchg) = @_;
3361: my $okresult = 0;
3362: if ($userresult ne 'ok') {
3363: if ($userresult =~ /^error:(.+)$/) {
3364: my $error = $1;
3365: $r->print('<br />'.
3366: &mt('<b>[_1]</b>: Unable to add/modify: [_2]',$username,$error));
3367: }
3368: } else {
3369: $counts->{'user'} ++;
3370: $okresult = 1;
3371: }
3372: if ($authresult ne 'ok') {
3373: if ($authresult =~ /^error:(.+)$/) {
3374: my $error = $1;
3375: $r->print('<br />'.
3376: &mt('<b>[_1]</b>: Unable to modify authentication: [_2]',$username,$error));
3377: }
3378: } else {
3379: $counts->{'auth'} ++;
3380: $okresult = 1;
3381: }
3382: if ($roleresult ne 'ok') {
3383: if ($roleresult =~ /^error:(.+)$/) {
3384: my $error = $1;
3385: $r->print('<br />'.
3386: &mt('<b>[_1]</b>: Unable to add role: [_2]',$username,$error));
3387: }
3388: } else {
3389: $counts->{'role'} ++;
3390: $okresult = 1;
3391: }
3392: if ($okresult) {
3393: $flushc++;
3394: $userchg->{$username}=1;
3395: $r->print('. ');
3396: if ($flushc>15) {
3397: $r->rflush;
3398: $flushc=0;
3399: }
3400: }
3401: return $flushc;
3402: }
3403:
3404: # ========================================================= Menu Phase Two Drop
1.17 raeburn 3405: sub print_drop_menu {
3406: my ($r,$context,$permission) = @_;
3407: $r->print('<h3>'.&mt("Drop Students").'</h3>'."\n".
3408: '<form name="studentform" method="post">'."\n");
1.1 raeburn 3409: my $cid=$env{'request.course.id'};
3410: my ($classlist,$keylist) = &Apache::loncoursedata::get_classlist();
3411: if (! defined($classlist)) {
3412: $r->print(&mt('There are no students currently enrolled.')."\n");
3413: return;
3414: }
3415: # Print out the available choices
1.17 raeburn 3416: &show_drop_list($r,$classlist,$keylist,$permission);
3417: $r->print('</form>'. &Apache::loncommon::end_page());
1.1 raeburn 3418: return;
3419: }
3420:
3421: # ================================================================== Phase four
3422:
1.11 raeburn 3423: sub update_user_list {
3424: my ($r,$context,$setting,$choice) = @_;
3425: my $now = time;
1.1 raeburn 3426: my $count=0;
1.11 raeburn 3427: my @changelist;
3428: if ($choice ne '') {
3429: @changelist = &Apache::loncommon::get_env_multiple('form.actionlist');
3430: } else {
3431: @changelist = &Apache::loncommon::get_env_multiple('form.droplist');
3432: }
3433: my %result_text = ( ok => { 'revoke' => 'Revoked',
3434: 'delete' => 'Deleted',
3435: 'reenable' => 'Re-enabled',
1.17 raeburn 3436: 'activate' => 'Activated',
3437: 'chgdates' => 'Changed Access Dates for',
3438: 'chgsec' => 'Changed section for',
3439: 'drop' => 'Dropped',
1.11 raeburn 3440: },
3441: error => {'revoke' => 'revoking',
3442: 'delete' => 'deleting',
3443: 'reenable' => 're-enabling',
3444: 'activate' => 'activating',
1.17 raeburn 3445: 'chgdates' => 'changing access dates for',
3446: 'chgsec' => 'changing section for',
3447: 'drop' => 'dropping',
1.11 raeburn 3448: },
3449: );
3450: my ($startdate,$enddate);
3451: if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
3452: ($startdate,$enddate) = &get_dates_from_form();
3453: }
3454: foreach my $item (@changelist) {
3455: my ($role,$uname,$udom,$cid,$sec,$scope,$result,$type,$locktype,@sections,
3456: $scopestem);
1.17 raeburn 3457: if ($choice eq 'drop') {
3458: ($uname,$udom,$sec) = split(/:/,$item,-1);
3459: $role = 'st';
3460: $cid = $env{'request.course.id'};
3461: $scopestem = '/'.$cid;
3462: $scopestem =~s/\_/\//g;
3463: if ($sec eq '') {
3464: $scope = $scopestem;
3465: } else {
3466: $scope = $scopestem.'/'.$sec;
3467: }
3468: } elsif ($context eq 'course') {
1.11 raeburn 3469: ($uname,$udom,$role,$sec,$type,$locktype) = split(/\:/,$item,-1);
3470: $cid = $env{'request.course.id'};
3471: $scopestem = '/'.$cid;
3472: $scopestem =~s/\_/\//g;
3473: if ($sec eq '') {
3474: $scope = $scopestem;
3475: } else {
3476: $scope = $scopestem.'/'.$sec;
3477: }
1.13 raeburn 3478: } elsif ($context eq 'author') {
1.11 raeburn 3479: ($uname,$udom,$role) = split(/\:/,$item,-1);
3480: $scope = '/'.$env{'user.domain'}.'/'.$env{'user.name'};
3481: } elsif ($context eq 'domain') {
3482: if ($setting eq 'domain') {
3483: ($role,$uname,$udom) = split(/\:/,$item,-1);
3484: $scope = '/'.$env{'request.role.domain'}.'/';
1.13 raeburn 3485: } elsif ($setting eq 'author') {
1.11 raeburn 3486: ($uname,$udom,$role,$scope) = split(/\:/,$item);
3487: } elsif ($setting eq 'course') {
3488: ($uname,$udom,$role,$cid,$sec,$type,$locktype) =
3489: split(/\:/,$item);
3490: $scope = '/'.$cid;
3491: $scope =~s/\_/\//g;
3492: if ($sec ne '') {
3493: $scope .= '/'.$sec;
3494: }
3495: }
3496: }
3497: my $plrole = &Apache::lonnet::plaintext($role);
3498: my ($uid,$first,$middle,$last,$gene,$sec);
3499: my $start = $env{'form.'.$item.'_start'};
3500: my $end = $env{'form.'.$item.'_end'};
1.17 raeburn 3501: if ($choice eq 'drop') {
3502: # drop students
3503: $end = $now;
3504: $type = 'manual';
3505: $result =
3506: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
3507: } elsif ($choice eq 'revoke') {
3508: # revoke or delete user role
1.11 raeburn 3509: $end = $now;
3510: if ($role eq 'st') {
3511: $result =
3512: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
3513: } else {
3514: $result =
3515: &Apache::lonnet::revokerole($udom,$uname,$scope,$role);
3516: }
3517: } elsif ($choice eq 'delete') {
3518: $start = -1;
3519: $end = -1;
3520: if ($role eq 'st') {
3521: # FIXME - how does role deletion affect classlist?
3522: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
3523: } else {
3524: $result =
3525: &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$now,
3526: 0,1);
3527: }
3528: } else {
3529: #reenable, activate, change access dates or change section
3530: if ($choice ne 'chgsec') {
3531: $start = $startdate;
3532: $end = $enddate;
3533: }
3534: if ($choice eq 'reenable') {
3535: if ($role eq 'st') {
3536: $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
3537: } else {
3538: $result =
3539: &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
3540: $now);
3541: }
3542: } elsif ($choice eq 'activate') {
3543: if ($role eq 'st') {
3544: $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
3545: } else {
3546: $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
3547: $now);
3548: }
3549: } elsif ($choice eq 'chgdates') {
3550: if ($role eq 'st') {
3551: $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
3552: } else {
3553: $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
3554: $start);
3555: }
3556: } elsif ($choice eq 'chgsec') {
3557: my (@newsecs,$revresult,$nochg,@retained);
3558: if ($role ne 'cc') {
3559: @newsecs = split(/,/,$env{'form.newsecs'});
3560: }
3561: # remove existing section if not to be retained.
3562: if (!$env{'form.retainsec'}) {
3563: if ($sec eq '') {
3564: if (@newsecs == 0) {
3565: $result = &mt('No change in section assignment (none)');
3566: $nochg = 1;
3567: }
3568: } else {
3569: if (!grep(/^\Q$sec\E$/,@newsecs)) {
3570: $revresult =
3571: &Apache::lonnet::revokerole($udom,$uname,$scope,$role);
3572: } else {
3573: push(@retained,$sec);
3574: }
3575: }
3576: } else {
3577: push(@retained,$sec);
3578: }
3579: # add new sections
3580: if (@newsecs == 0) {
3581: if (!$nochg) {
3582: if ($sec ne '') {
3583: if ($role eq 'st') {
3584: $result =
3585: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,undef,$end,$start,$type,$locktype,$cid);
3586: } else {
3587: my $newscope = $scopestem;
3588: $result = &Apache::lonnet::assignrole($udom,$uname,$newscope,$role,$end,$start);
3589: }
3590: }
3591: }
3592: } else {
3593: foreach my $newsec (@newsecs) {
3594: if (!grep(/^\Q$newsec\E$/,@retained)) {
3595: if ($role eq 'st') {
3596: $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$newsec,$end,$start,$type,$locktype,$cid);
3597: } else {
3598: my $newscope = $scopestem;
3599: if ($newsec ne '') {
3600: $newscope .= '/'.$newsec;
3601: }
3602: $result = &Apache::lonnet::assignrole($udom,$uname,
3603: $newscope,$role,$end,$start);
3604: }
3605: }
3606: }
3607: }
3608: }
3609: }
1.17 raeburn 3610: my $extent = $scope;
3611: if ($choice eq 'drop' || $context eq 'course') {
3612: my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
3613: if ($cdesc) {
3614: $extent = $cdesc;
3615: }
3616: }
1.1 raeburn 3617: if ($result eq 'ok' || $result eq 'ok:') {
1.11 raeburn 3618: $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for [_3]",
1.17 raeburn 3619: $plrole,$extent,$uname.':'.$udom).'<br />');
1.1 raeburn 3620: $count++;
3621: } else {
3622: $r->print(
1.11 raeburn 3623: &mt("Error $result_text{'error'}{$choice} [_1] in [_2] for [_3]:[_4]",
1.17 raeburn 3624: $plrole,$extent,$uname.':'.$udom,$result).'<br />');
1.11 raeburn 3625: }
3626: }
3627: $r->print('<p><b>'.&mt("$result_text{'ok'}{$choice} role(s) for [quant,_1,user,users,users].",$count).'</b></p>');
3628: if ($count > 0) {
1.17 raeburn 3629: if ($choice eq 'revoke' || $choice eq 'drop') {
1.11 raeburn 3630: $r->print('<p>'.&mt('Re-enabling will re-activate data for the role.</p>'));
3631: }
3632: # Flush the course logs so reverse user roles immediately updated
3633: &Apache::lonnet::flushcourselogs();
3634: }
3635: if ($env{'form.makedatesdefault'}) {
3636: if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
3637: $r->print(&make_dates_default($startdate,$enddate));
1.1 raeburn 3638: }
3639: }
3640: }
3641:
1.8 raeburn 3642: sub classlist_drop {
3643: my ($scope,$uname,$udom,$now,$action) = @_;
3644: my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)});
3645: my $cid=$cdom.'_'.$cnum;
3646: my $user = $uname.':'.$udom;
3647: if ($action eq 'drop') {
3648: if (!&active_student_roles($cnum,$cdom,$uname,$udom)) {
3649: my $result =
3650: &Apache::lonnet::cput('classlist',
3651: { $user => $now },
3652: $env{'course.'.$cid.'.domain'},
3653: $env{'course.'.$cid.'.num'});
3654: return &mt('Drop from classlist: [_1]',
3655: '<b>'.$result.'</b>').'<br />';
3656: }
3657: }
3658: }
3659:
3660: sub active_student_roles {
3661: my ($cnum,$cdom,$uname,$udom) = @_;
3662: my %roles =
3663: &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
3664: ['future','active'],['st']);
3665: return exists($roles{"$cnum:$cdom:st"});
3666: }
3667:
1.1 raeburn 3668: sub section_check_js {
1.8 raeburn 3669: my $groupslist= &get_groupslist();
1.1 raeburn 3670: return <<"END";
3671: function validate(caller) {
1.9 raeburn 3672: var groups = new Array($groupslist);
1.1 raeburn 3673: var secname = caller.value;
3674: if ((secname == 'all') || (secname == 'none')) {
3675: alert("'"+secname+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name.");
3676: return 'error';
3677: }
3678: if (secname != '') {
3679: for (var k=0; k<groups.length; k++) {
3680: if (secname == groups[k]) {
3681: 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.");
3682: return 'error';
3683: }
3684: }
3685: }
3686: return 'ok';
3687: }
3688: END
3689: }
3690:
3691: sub set_login {
3692: my ($dom,$authformkrb,$authformint,$authformloc) = @_;
3693: my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
3694: my $response;
3695: my ($authnum,%can_assign) =
3696: &Apache::loncommon::get_assignable_auth($dom);
3697: if ($authnum) {
3698: $response = &Apache::loncommon::start_data_table();
3699: if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
3700: $response .= &Apache::loncommon::start_data_table_row().
3701: '<td>'.$authformkrb.'</td>'.
3702: &Apache::loncommon::end_data_table_row()."\n";
3703: }
3704: if ($can_assign{'int'}) {
3705: $response .= &Apache::loncommon::start_data_table_row().
3706: '<td>'.$authformint.'</td>'.
3707: &Apache::loncommon::end_data_table_row()."\n"
3708: }
3709: if ($can_assign{'loc'}) {
3710: $response .= &Apache::loncommon::start_data_table_row().
3711: '<td>'.$authformloc.'</td>'.
3712: &Apache::loncommon::end_data_table_row()."\n";
3713: }
3714: $response .= &Apache::loncommon::end_data_table();
3715: }
3716: return $response;
3717: }
3718:
1.8 raeburn 3719: sub course_sections {
3720: my ($sections_count,$role) = @_;
3721: my $output = '';
3722: my @sections = (sort {$a <=> $b} keys %{$sections_count});
3723: if (scalar(@sections) == 1) {
3724: $output = '<select name="currsec_'.$role.'" >'."\n".
3725: ' <option value="">Select</option>'."\n".
3726: ' <option value="">No section</option>'."\n".
3727: ' <option value="'.$sections[0].'" >'.$sections[0].'</option>'."\n";
3728: } else {
3729: $output = '<select name="currsec_'.$role.'" ';
3730: my $multiple = 4;
3731: if (scalar(@sections) < 4) { $multiple = scalar(@sections); }
3732: $output .= 'multiple="multiple" size="'.$multiple.'">'."\n";
3733: foreach my $sec (@sections) {
3734: $output .= '<option value="'.$sec.'">'.$sec."</option>\n";
3735: }
3736: }
3737: $output .= '</select>';
3738: return $output;
3739: }
3740:
3741: sub get_groupslist {
3742: my $groupslist;
3743: my %curr_groups = &Apache::longroup::coursegroups();
3744: if (%curr_groups) {
3745: $groupslist = join('","',sort(keys(%curr_groups)));
3746: $groupslist = '"'.$groupslist.'"';
3747: }
1.11 raeburn 3748: return $groupslist;
1.8 raeburn 3749: }
3750:
3751: sub setsections_javascript {
3752: my ($form,$groupslist) = @_;
3753: my ($checkincluded,$finish,$roleplace,$setsection_js);
3754: if ($form eq 'cu') {
3755: $checkincluded = 'formname.elements[i-1].checked == true';
3756: $finish = 'formname.submit()';
3757: $roleplace = 3;
3758: } else {
1.11 raeburn 3759: $checkincluded = 'formname.name == "'.$form.'"';
1.8 raeburn 3760: $finish = "seccheck = 'ok';";
3761: $roleplace = 1;
1.11 raeburn 3762: $setsection_js = "var seccheck = 'alert';";
1.8 raeburn 3763: }
3764: my %alerts = &Apache::lonlocal::texthash(
3765: secd => 'Section designations do not apply to Course Coordinator roles.',
3766: accr => 'A course coordinator role will be added with access to all sections.',
3767: inea => 'In each course, each user may only have one student role at a time.',
3768: youh => 'You had selected ',
3769: secs => 'sections.',
3770: plmo => 'Please modify your selections so they include no more than one section.',
3771: mayn => 'may not be used as the name for a section, as it is a reserved word.',
3772: plch => 'Please choose a different section name.',
3773: mnot => 'may not be used as a section name, as it is the name of a course group.',
3774: secn => 'Section names and group names must be distinct. Please choose a different section name.',
1.11 raeburn 3775: );
1.8 raeburn 3776: $setsection_js .= <<"ENDSECCODE";
3777:
3778: function setSections(formname) {
3779: var re1 = /^currsec_/;
3780: var groups = new Array($groupslist);
3781: for (var i=0;i<formname.elements.length;i++) {
3782: var str = formname.elements[i].name;
3783: var checkcurr = str.match(re1);
3784: if (checkcurr != null) {
3785: if ($checkincluded) {
3786: var match = str.split('_');
3787: var role = match[$roleplace];
3788: if (role == 'cc') {
3789: alert("$alerts{'secd'}\\n$alerts{'accr'}");
3790: }
3791: else {
3792: var sections = '';
3793: var numsec = 0;
3794: var sections;
3795: for (var j=0; j<formname.elements[i].length; j++) {
3796: if (formname.elements[i].options[j].selected == true ) {
3797: if (formname.elements[i].options[j].value != "") {
3798: if (numsec == 0) {
3799: if (formname.elements[i].options[j].value != "") {
3800: sections = formname.elements[i].options[j].value;
3801: numsec ++;
3802: }
3803: }
3804: else {
3805: sections = sections + "," + formname.elements[i].options[j].value
3806: numsec ++;
3807: }
3808: }
3809: }
3810: }
3811: if (numsec > 0) {
3812: if (formname.elements[i+1].value != "" && formname.elements[i+1].value != null) {
3813: sections = sections + "," + formname.elements[i+1].value;
3814: }
3815: }
3816: else {
3817: sections = formname.elements[i+1].value;
3818: }
3819: var newsecs = formname.elements[i+1].value;
3820: var numsplit;
3821: if (newsecs != null && newsecs != "") {
3822: numsplit = newsecs.split(/,/g);
3823: numsec = numsec + numsplit.length;
3824: }
3825:
3826: if ((role == 'st') && (numsec > 1)) {
3827: alert("$alerts{'inea'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}")
3828: return;
3829: }
3830: else {
3831: if (numsplit != null) {
3832: for (var j=0; j<numsplit.length; j++) {
3833: if ((numsplit[j] == 'all') ||
3834: (numsplit[j] == 'none')) {
3835: alert("'"+numsplit[j]+"' $alerts{'mayn'}\\n$alerts{'plch'}");
3836: return;
3837: }
3838: for (var k=0; k<groups.length; k++) {
3839: if (numsplit[j] == groups[k]) {
3840: alert("'"+numsplit[j]+"' $alerts{'mnot'}\\n$alerts{'secn'}");
3841: return;
3842: }
3843: }
3844: }
3845: }
3846: formname.elements[i+2].value = sections;
3847: }
3848: }
3849: }
3850: }
3851: }
3852: $finish
3853: }
3854: ENDSECCODE
1.11 raeburn 3855: return $setsection_js;
1.8 raeburn 3856: }
3857:
1.15 raeburn 3858: sub can_create_user {
3859: my ($dom,$context,$usertype) = @_;
3860: my %domconf = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
3861: my $cancreate = 1;
3862: if (ref($domconf{'usercreation'}) eq 'HASH') {
3863: if (ref($domconf{'usercreation'}{'cancreate'}) eq 'HASH') {
3864: if ($context eq 'course' || $context eq 'author') {
3865: my $creation = $domconf{'usercreation'}{'cancreate'}{$context};
3866: if ($creation eq 'none') {
3867: $cancreate = 0;
3868: } elsif ($creation ne 'any') {
3869: if (defined($usertype)) {
3870: if ($creation ne $usertype) {
3871: $cancreate = 0;
3872: }
3873: }
3874: }
3875: }
3876: }
3877: }
3878: return $cancreate;
3879: }
3880:
1.20 raeburn 3881: sub can_modify_userinfo {
3882: my ($context,$dom,$fields,$userroles) = @_;
3883: my %domconfig =
3884: &Apache::lonnet::get_dom('configuration',['usermodification'],
3885: $dom);
3886: my %canmodify;
3887: if (ref($fields) eq 'ARRAY') {
3888: foreach my $field (@{$fields}) {
3889: $canmodify{$field} = 0;
3890: if (&Apache::lonnet::allowed('mau',$dom)) {
3891: $canmodify{$field} = 1;
3892: } else {
3893: if (ref($domconfig{'usermodification'}) eq 'HASH') {
3894: if (ref($domconfig{'usermodification'}{$context}) eq 'HASH') {
3895: if (ref($userroles) eq 'ARRAY') {
3896: foreach my $role (@{$userroles}) {
3897: my $testrole;
3898: if ($role =~ /^cr\//) {
3899: $testrole = 'cr';
3900: } else {
3901: $testrole = $role;
3902: }
3903: if (ref($domconfig{'usermodification'}{$context}{$testrole}) eq 'HASH') {
3904: if ($domconfig{'usermodification'}{$context}{$testrole}{$field}) {
3905: $canmodify{$field} = 1;
3906: last;
3907: }
3908: }
3909: }
3910: } else {
3911: foreach my $key (keys(%{$domconfig{'usermodification'}{$context}})) {
3912: if (ref($domconfig{'usermodification'}{$context}{$key}) eq 'HASH') {
3913: if ($domconfig{'usermodification'}{$context}{$key}{$field}) {
3914: $canmodify{$field} = 1;
3915: last;
3916: }
3917: }
3918: }
3919: }
3920: }
3921: } elsif ($context eq 'course') {
3922: if (ref($userroles) eq 'ARRAY') {
3923: if (grep(/^st$/,@{$userroles})) {
3924: $canmodify{$field} = 1;
3925: }
3926: } else {
3927: $canmodify{$field} = 1;
3928: }
3929: }
3930: }
3931: }
3932: }
3933: return %canmodify;
3934: }
3935:
1.18 raeburn 3936: sub check_usertype {
3937: my ($dom,$uname,$rules) = @_;
3938: my $usertype;
3939: if (ref($rules) eq 'HASH') {
3940: my @user_rules = keys(%{$rules});
3941: if (@user_rules > 0) {
3942: my %rule_check = &Apache::lonnet::inst_rulecheck($dom,$uname,undef,'username',\@user_rules);
3943: if (keys(%rule_check) > 0) {
3944: $usertype = 'unofficial';
3945: foreach my $item (keys(%rule_check)) {
3946: if ($rule_check{$item}) {
3947: $usertype = 'official';
3948: last;
3949: }
3950: }
3951: }
3952: }
3953: }
3954: return $usertype;
3955: }
3956:
1.17 raeburn 3957: sub roles_by_context {
3958: my ($context,$custom) = @_;
3959: my @allroles;
3960: if ($context eq 'course') {
3961: @allroles = ('st','ad','ta','ep','in','cc');
3962: if ($custom) {
3963: push(@allroles,'cr');
3964: }
3965: } elsif ($context eq 'author') {
3966: @allroles = ('ca','aa');
3967: } elsif ($context eq 'domain') {
3968: @allroles = ('li','dg','sc','au','dc');
3969: }
3970: return @allroles;
3971: }
3972:
1.16 raeburn 3973: sub get_permission {
1.17 raeburn 3974: my ($context,$roles) = @_;
1.16 raeburn 3975: my %permission;
3976: if ($context eq 'course') {
1.17 raeburn 3977: my $custom = 1;
3978: my @allroles = &roles_by_context($context,$custom);
3979: foreach my $role (@allroles) {
3980: if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
3981: $permission{'cusr'} = 1;
3982: last;
3983: }
1.16 raeburn 3984: }
3985: if (&Apache::lonnet::allowed('ccr',$env{'request.course.id'})) {
3986: $permission{'custom'} = 1;
3987: }
3988: if (&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) {
3989: $permission{'view'} = 1;
3990: }
3991: if (!$permission{'view'}) {
3992: my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
3993: $permission{'view'} = &Apache::lonnet::allowed('vcl',$scope);
3994: if ($permission{'view'}) {
3995: $permission{'view_section'} = $env{'request.course.sec'};
3996: }
3997: }
1.17 raeburn 3998: if (!$permission{'cusr'}) {
3999: if ($env{'request.course.sec'} ne '') {
4000: my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
4001: $permission{'cusr'} = (&Apache::lonnet::allowed('cst',$scope));
4002: if ($permission{'cusr'}) {
4003: $permission{'cusr_section'} = $env{'request.course.sec'};
4004: }
4005: }
4006: }
1.16 raeburn 4007: if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
4008: $permission{'grp_manage'} = 1;
4009: }
4010: } elsif ($context eq 'author') {
4011: $permission{'cusr'} = &authorpriv($env{'user.name'},$env{'request.role.domain'});
4012: $permission{'view'} = $permission{'cusr'};
4013: } else {
1.17 raeburn 4014: my @allroles = &roles_by_context($context);
4015: foreach my $role (@allroles) {
4016: if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) { $permission{'cusr'} = 1;
4017: last;
4018: }
4019: }
4020: if (!$permission{'cusr'}) {
4021: if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
4022: $permission{'cusr'} = 1;
4023: }
1.16 raeburn 4024: }
4025: if (&Apache::lonnet::allowed('ccr',$env{'request.role.domain'})) {
4026: $permission{'custom'} = 1;
4027: }
4028: $permission{'view'} = $permission{'cusr'};
4029: }
4030: my $allowed = 0;
4031: foreach my $perm (values(%permission)) {
4032: if ($perm) { $allowed=1; last; }
4033: }
4034: return (\%permission,$allowed);
4035: }
4036:
4037: # ==================================================== Figure out author access
4038:
4039: sub authorpriv {
4040: my ($auname,$audom)=@_;
4041: unless ((&Apache::lonnet::allowed('cca',$audom.'/'.$auname))
4042: || (&Apache::lonnet::allowed('caa',$audom.'/'.$auname))) { return ''; } return 1;
4043: }
4044:
1.17 raeburn 4045: sub get_course_identity {
4046: my ($cid) = @_;
4047: my ($cnum,$cdom,$cdesc);
4048: if ($cid eq '') {
4049: $cid = $env{'request.course.id'}
4050: }
4051: if ($cid ne '') {
4052: $cnum = $env{'course.'.$cid.'.num'};
4053: $cdom = $env{'course.'.$cid.'.domain'};
4054: $cdesc = $env{'course.'.$cid.'.description'};
4055: if ($cnum eq '' || $cdom eq '') {
4056: my %coursehash =
4057: &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
4058: $cdom = $coursehash{'domain'};
4059: $cnum = $coursehash{'num'};
4060: $cdesc = $coursehash{'description'};
4061: }
4062: }
4063: return ($cnum,$cdom,$cdesc);
4064: }
4065:
1.19 raeburn 4066: sub dc_setcourse_js {
4067: my ($formname,$mode) = @_;
4068: my $dc_setcourse_code;
4069: my $cctext = &Apache::lonnet::plaintext('cc');
4070: my %alerts = §ioncheck_alerts();
4071: my $role = 'role';
4072: if ($mode eq 'upload') {
4073: $role = 'courserole';
4074: }
4075: $dc_setcourse_code = (<<"SCRIPTTOP");
4076: function setCourse() {
4077: var course = document.$formname.dccourse.value;
4078: if (course != "") {
4079: if (document.$formname.dcdomain.value != document.$formname.origdom.value) {
4080: alert("$alerts{'curd'}");
4081: return;
4082: }
4083: var userrole = document.$formname.$role.options[document.$formname.$role.selectedIndex].value
4084: var section="";
4085: var numsections = 0;
4086: var newsecs = new Array();
4087: for (var i=0; i<document.$formname.currsec.length; i++) {
4088: if (document.$formname.currsec.options[i].selected == true ) {
4089: if (document.$formname.currsec.options[i].value != "" && document.$formname.currsec.options[i].value != null) {
4090: if (numsections == 0) {
4091: section = document.$formname.currsec.options[i].value
4092: numsections = 1;
4093: }
4094: else {
4095: section = section + "," + document.$formname.currsec.options[i].value
4096: numsections ++;
4097: }
4098: }
4099: }
4100: }
4101: if (document.$formname.newsec.value != "" && document.$formname.newsec.value != null) {
4102: if (numsections == 0) {
4103: section = document.$formname.newsec.value
4104: }
4105: else {
4106: section = section + "," + document.$formname.newsec.value
4107: }
4108: newsecs = document.$formname.newsec.value.split(/,/g);
4109: numsections = numsections + newsecs.length;
4110: }
4111: if ((userrole == 'st') && (numsections > 1)) {
4112: alert("$alerts{'inea'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
4113: return;
4114: }
4115: for (var j=0; j<newsecs.length; j++) {
4116: if ((newsecs[j] == 'all') || (newsecs[j] == 'none')) {
4117: alert("'"+newsecs[j]+"' $alerts{'mayn'}.\\n$alerts{'plsc'}.");
4118: return;
4119: }
4120: if (document.$formname.groups.value != '') {
4121: var groups = document.$formname.groups.value.split(/,/g);
4122: for (var k=0; k<groups.length; k++) {
4123: if (newsecs[j] == groups[k]) {
4124: alert("'"+newsecs[j]+"' $alerts{'mayt'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
4125: return;
4126: }
4127: }
4128: }
4129: }
4130: if ((userrole == 'cc') && (numsections > 0)) {
4131: alert("$alerts{'secd'} $cctext $alerts{'role'}.\\n$alerts{'accr'}.");
4132: section = "";
4133: }
4134: SCRIPTTOP
4135: if ($mode ne 'upload') {
4136: $dc_setcourse_code .= (<<"ENDSCRIPT");
4137: var coursename = "_$env{'request.role.domain'}"+"_"+course+"_"+userrole
4138: var numcourse = getIndex(document.$formname.dccourse);
4139: if (numcourse == "-1") {
4140: alert("$alerts{'thwa'}");
4141: return;
4142: }
4143: else {
4144: document.$formname.elements[numcourse].name = "act"+coursename;
4145: var numnewsec = getIndex(document.$formname.newsec);
4146: if (numnewsec != "-1") {
4147: document.$formname.elements[numnewsec].name = "sec"+coursename;
4148: document.$formname.elements[numnewsec].value = section;
4149: }
4150: var numstart = getIndex(document.$formname.start);
4151: if (numstart != "-1") {
4152: document.$formname.elements[numstart].name = "start"+coursename;
4153: }
4154: var numend = getIndex(document.$formname.end);
4155: if (numend != "-1") {
4156: document.$formname.elements[numend].name = "end"+coursename
4157: }
4158: }
4159: }
4160: document.$formname.submit();
4161: }
4162:
4163: ENDSCRIPT
4164: } else {
4165: $dc_setcourse_code .= "
4166: document.$formname.sections.value = section;
4167: }
4168: return 'ok';
4169: }
4170: ";
4171: }
4172: $dc_setcourse_code .= (<<"ENDSCRIPT");
4173:
4174: function getIndex(caller) {
4175: for (var i=0;i<document.$formname.elements.length;i++) {
4176: if (document.$formname.elements[i] == caller) {
4177: return i;
4178: }
4179: }
4180: return -1;
4181: }
4182: ENDSCRIPT
4183: }
4184:
4185: sub sectioncheck_alerts {
4186: my %alerts = &Apache::lonlocal::texthash(
4187: curd => 'You must select a course in the current domain',
4188: inea => 'In each course, each user may only have one student role at a time',
4189: youh => 'You had selected',
4190: sect => 'sections',
4191: plsm => 'Please modify your selections so they include no more than one section',
4192: mayn => 'may not be used as the name for a section, as it is a reserved word',
4193: plsc => 'Please choose a different section name',
4194: mayt => 'may not be used as the name for a section, as it is the name of a course group',
4195: secn => 'Section names and group names must be distinct',
4196: secd => 'Section designations do not apply to ',
4197: role => 'roles',
4198: accr => 'role will be added with access to all sections',
4199: thwa => 'There was a problem with your course selection'
4200: );
4201: return %alerts;
4202: }
1.17 raeburn 4203:
4204:
1.1 raeburn 4205: 1;
4206:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>