1: # The LearningOnline Network
2: # Pick a student from the classlist
3: #
4: # $Id: lonpickstudent.pm,v 1.34 2023/09/28 15:56:48 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: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28:
29: package Apache::lonpickstudent;
30:
31: use strict;
32: use Apache::Constants qw(:common);
33: use Apache::loncommon;
34: use Apache::loncoursedata;
35: use Apache::lonnet;
36: use Apache::lonlocal;
37: use Apache::longroup;
38: use LONCAPA;
39:
40: sub handler {
41: my $r = shift;
42: &Apache::loncommon::content_type($r,'text/html');
43: $r->send_http_header;
44: return OK if $r->header_only;
45:
46: # ------------------------------------------------------------ Print the screen
47: $r->print(&Apache::loncommon::start_page("Selecting a User",undef,
48: {'no_nav_bar' => 1}));
49:
50:
51: &Apache::loncommon::get_unprocessed_cgi
52: ($ENV{'QUERY_STRING'},['filter','form','unameelement','udomelement',
53: 'roles','courseadv','clicker','identelement']);
54: # Allowed?
55: my $allowed;
56: if ($env{'request.course.id'}) {
57: if (!($allowed = &Apache::lonnet::allowed('srm',$env{'request.course.id'}))) {
58: if ($env{'request.course.sec'}) {
59: $allowed = &Apache::lonnet::allowed('srm',
60: "$env{'request.course.id'}/$env{'request.course.sec'}");
61: if ($allowed) { $allowed = 'section'; }
62: }
63: }
64: }
65:
66: unless (($env{'form.roles'}) || ($allowed)) {
67: $r->print(&mt('No context.').
68: &Apache::loncommon::end_page());
69: return OK;
70: }
71:
72: # See if filter present
73: my $filter=$env{'form.filter'};
74: my $filtermsg;
75: my $encoded_filter = &HTML::Entities::encode($filter,'<>&"');
76: $filter = quotemeta($filter);
77: my $change = &mt('Change');
78: my $filterbutton =<<FILTER;
79: <input type="text" name="filter" value="$encoded_filter" />
80: <input type="submit" name="Change" value="$change" />
81: FILTER
82: foreach my $name ('form','unameelement','udomelement','roles','courseadv','clicker','identelement') {
83: my $value = &HTML::Entities::encode($env{"form.$name"},'<>&"');
84: $filterbutton .= <<HIDDEN;
85: <input type="hidden" name="$name" value="$value" />
86: HIDDEN
87: }
88: unless ($env{'form.courseadv'} eq 'only') {
89: if ($env{'request.course.id'}) {
90: my $crstype = &Apache::loncommon::course_type();
91: my $showrole = &Apache::lonnet::plaintext('st',$crstype);
92: $filtermsg = &mt("Showing users with role of '[_1]' with name starting: [_2]",
93: $showrole,$filterbutton);
94: } else {
95: $filtermsg =
96: &mt('Showing users with a name starting with [_1]',
97: $filterbutton);
98: }
99: }
100: if ($filter eq '') {
101: $filter = '.'
102: }
103:
104: # --------------------------------------- There is such a user, get environment
105:
106: $r->print(<<ENDSCRIPT);
107: <script type="text/javascript">
108: function gochoose(uname,udom) {
109: var form = opener.document.$env{'form.form'};
110: form.elements["$env{'form.unameelement'}"].value=uname;
111: var slct = form.elements['$env{"form.udomelement"}'];
112: if ((slct.tagName === 'INPUT') && ((slct.type === 'text') || (slct.type === 'hidden'))) {
113: slct.value=udom;
114: } else if (slct.tagName === 'SELECT') {
115: var i;
116: for (i=0;i<slct.length;i++) {
117: if (slct.options[i].value==udom) {
118: slct.selectedIndex=i;
119: break;
120: }
121: }
122: }
123: var identname = "$env{'form.identelement'}";
124: if ((identname != undefined) && (identname != '') && (identname != 'null')) {
125: ident = opener.document.getElementsByName("$env{'form.identelement'}");
126: if (ident.length > 0) {
127: var j;
128: for (j=0; j<ident.length; j++) {
129: if (ident[j].value == 'uname') {
130: if (ident[j].checked) {
131: break;
132: } else {
133: ident[j].checked = true;
134: window.opener.toggleIdentifier(form);
135: }
136: } else {
137: ident[j].checked = false;
138: }
139: }
140: }
141: }
142: self.close();
143: }
144: </script>
145: ENDSCRIPT
146:
147: $r->print('<form action="">');
148: if ($allowed && (!$env{'form.roles'})) {
149: unless ($env{'form.courseadv'} eq 'none') {
150: # -------------------------------------------------------- Get course personnel
151: $r->print('<h3>'.$env{'course.'.$env{'request.course.id'}.'.description'}.
152: '</h3>');
153: my %coursepersonnel=
154: &Apache::lonnet::get_course_adv_roles();
155: $r->print(&Apache::loncommon::start_data_table());
156: foreach my $role (sort(keys(%coursepersonnel))) {
157: foreach my $user (split(/\,/,$coursepersonnel{$role})) {
158: my ($puname,$pudom)=split(/\:/,$user);
159: $r->print(&Apache::loncommon::start_data_table_row().
160: '<td>'.
161: '<input type="button" value="'.&mt('Select').'" onclick="gochoose('.
162: "'".$puname."','".$pudom."')".'" /></td><td>'.$role.'</td><td>'.
163: &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($puname,$pudom),$puname,$pudom).'</td>'.
164: &Apache::loncommon::end_data_table_row());
165: }
166: }
167: $r->print(&Apache::loncommon::end_data_table().'<br /><hr />');
168: }
169:
170: if ($filtermsg ne '') {
171: $r->print('<p>'.$filtermsg.'</p>');
172: }
173:
174: $r->rflush();
175:
176: unless ($env{'form.courseadv'} eq 'only') {
177:
178: # ------------------------------------------------------------------ Students
179:
180: # Do we have a clicker?
181:
182: my $clicker=$env{'form.clicker'};
183: my @fragments=();
184: if ($clicker) {
185: $r->print('<h1>'.&mt('Clicker: [_1]',$clicker).'</h1>');
186: my $clicklength=length($clicker);
187: my $maxlength=$clicklength-1;
188: if ($maxlength>2) {
189: my $minlength=$maxlength-2;
190: if ($minlength<2) { $minlength=2; }
191: for (my $length=$maxlength;$length>=$minlength;$length--) {
192: for (my $startidx=0; $startidx<=$clicklength-$length; $startidx++) {
193: push(@fragments,substr($clicker,$startidx,$length));
194: }
195: }
196: }
197: }
198:
199: my $result;
200: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
201: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
202: my $classlist=&Apache::loncoursedata::get_classlist();
203: my %grouplist=&Apache::lonnet::get_group_membership($cdom,$cnum);
204: my $now = time;
205: my %allgroups = &Apache::longroup::coursegroups($cdom,$cnum);
206:
207: foreach my $user (sort(keys(%$classlist))) {
208: # the following undefs are for 'domain', and 'username' respectively.
209: my (undef,undef,$end,$start,$id,$section,$fullname,$status)=
210: @{$classlist->{$user}};
211: if ($allowed eq 'section' && $section ne $env{'request.course.sec'}) {
212: next;
213: }
214: if ($user=~/^($LONCAPA::username_re)\:($LONCAPA::domain_re)$/) {
215: my ($uname,$udom)=($1,$2);
216: if (($uname=~/^$filter/) || ($fullname=~/^$filter/i)) {
217: my $grouplist = '';
218: foreach my $group (sort(keys(%allgroups))) {
219: if (exists($grouplist{$group.':'.$uname.':'.$udom})) {
220: my ($end,$start) = split(/:/,$grouplist{$group.':'.$uname.':'.$udom});
221: if (($end!=0) && ($end<$now)) { next; }
222: if (($start!=0) && ($start>$now)) {next; }
223: $grouplist .= " $group,";
224: }
225: }
226: $grouplist =~ s/,$//;
227: my $markedupclicker;
228: if ($clicker) {
229: $markedupclicker=(&Apache::lonnet::userenvironment($udom,$uname,'clickers'))[1];
230: if ($markedupclicker!~/\w/) {
231: $markedupclicker='-';
232: } else {
233: foreach my $frag (@fragments) {
234: if ($markedupclicker=~/\Q$frag\E/is) {
235: $markedupclicker=~s/(\Q$frag\E)/<font color=\"green\" size=\"\+1\"><b>$1<\/b><\/font>/gis;
236: last;
237: }
238: }
239: }
240: }
241: $result .=&Apache::loncommon::start_data_table_row().
242: '<td>'.
243: '<input type="button" value="'.&mt('Select').'" onclick="gochoose('.
244: "'".$uname."','".$udom."')".'" /></td>'.
245: '<td><tt>'.$uname.'</tt></td><td> <tt>'.$udom.
246: '</tt></td><td>'.
247: &Apache::loncommon::aboutmewrapper($fullname,
248: $uname,$udom).
249: '</td><td>'.$id.'</td><td>'.$section.
250: '</td><td>'.$grouplist.'</td>'.
251: ($clicker?'<td>'.$markedupclicker.'</td>':'').
252: &Apache::loncommon::end_data_table_row();
253: }
254: }
255: }
256: if (!$result) {
257: $r->print('<p>'.&mt('No students found.').'</p>');
258: } else {
259: $r->print(&Apache::loncommon::start_data_table().
260: &Apache::loncommon::start_data_table_header_row().
261: &Apache::loncommon::end_data_table_header_row().
262: '<th> </th>'.
263: '<th>'.&mt('username').'</th>'.
264: '<th>'.&mt('domain').'</th>'.
265: '<th>'.&mt('Name').'</th>'.
266: '<th>'.&mt('ID').'</th>'.
267: '<th>'.&mt('section').'</th>'.
268: '<th>'.&mt('active group(s)').'</th>'.
269: ($clicker?'<th>'.&mt('clicker').'</th>':'').
270: &Apache::loncommon::end_data_table_header_row().
271: $result.
272: &Apache::loncommon::end_data_table());
273: }
274: }
275: } else {
276: $r->print('<h3>'.&mt('Users with Roles Assigned by').' '.
277: &Apache::loncommon::plainname($env{'user.name'},
278: $env{'user.domain'}).'</h3>');
279: $r->print($filtermsg);
280: $r->rflush();
281:
282: $r->print('<p>'.&Apache::loncommon::start_data_table());
283: my %user_role=&Apache::lonnet::get_my_roles();
284: my %users;
285: foreach my $user_role (keys(%user_role)) {
286: next if ($user_role !~
287: /^($LONCAPA::username_re):($LONCAPA::domain_re):(\w+)$/);
288: my ($uname,$udom,$urole)=($1,$2,$3);
289: my $fullname=&Apache::loncommon::plainname($uname,$udom);
290: next if (($uname!~/^$filter/i) && ($fullname !~/^$filter/i));
291:
292: if (!exists($users{"$uname:$udom"})) {
293: $users{"$uname:$udom"} = {'fullname' => $fullname };
294: }
295: push(@{$users{"$uname:$udom"}{'roles'}},$urole);
296: }
297: foreach my $user (sort {lc($a) cmp lc($b)} (keys(%users))) {
298: my ($uname,$udom) = split(':',$user);
299: $r->print(&Apache::loncommon::start_data_table_row().
300: '<td>'.
301: '<input type="button" value="'.&mt('Select').'" onclick="gochoose('.
302: "'".$uname."','".$udom."')".'" /></td>'.
303: '<td><tt>'.$uname.'</tt></td>'.
304: '<td><tt>'.$udom.'</tt></td><td>'.
305: &Apache::loncommon::aboutmewrapper($users{$user}{'fullname'},
306: $uname,$udom).'</td>'.
307: '<td>'.
308: join(', ',sort(map {&Apache::lonnet::plaintext($_)}
309: (@{$users{$user}{'roles'}}))).
310: '</td>'.
311: &Apache::loncommon::end_data_table_row());
312: }
313: $r->print(&Apache::loncommon::end_data_table().'</p>');
314: }
315: $r->print('</form>'.&Apache::loncommon::end_page());
316: return OK;
317: }
318:
319: 1;
320: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>