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