File:  [LON-CAPA] / loncom / interface / lonpickstudent.pm
Revision 1.33: download - view: text, annotated - select for diffs
Wed Feb 12 16:25:47 2020 UTC (4 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_X, version_2_11_4_uiuc, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, HEAD
- Parameter setting via table mode for users with both student and non-student
  roles.
- Pop-up launched via "Select User" link shows appropriate table(s) of users.

    1: # The LearningOnline Network
    2: # Pick a student from the classlist
    3: #
    4: # $Id: lonpickstudent.pm,v 1.33 2020/02/12 16:25:47 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']);
   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') {
   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:     opener.document.$env{'form.form'}.elements['$env{'form.unameelement'}'].value=uname;
  110:     var slct=opener.document.$env{'form.form'}.elements['$env{'form.udomelement'}'];
  111:     var i;
  112:     for (i=0;i<slct.length;i++) {
  113:         if (slct.options[i].value==udom) { slct.selectedIndex=i; }
  114:     }
  115:     self.close();
  116: }
  117: </script>
  118: ENDSCRIPT
  119: 
  120:     $r->print('<form action="">');
  121:     if ($allowed && (!$env{'form.roles'})) {
  122:         unless ($env{'form.courseadv'} eq 'none') {
  123: # -------------------------------------------------------- Get course personnel
  124: 	    $r->print('<h3>'.$env{'course.'.$env{'request.course.id'}.'.description'}.
  125: 		      '</h3>');
  126: 	    my %coursepersonnel=
  127: 	        &Apache::lonnet::get_course_adv_roles();
  128: 	    $r->print(&Apache::loncommon::start_data_table());
  129: 	    foreach my $role (sort(keys(%coursepersonnel))) {
  130: 	        foreach my $user (split(/\,/,$coursepersonnel{$role})) {
  131: 		    my ($puname,$pudom)=split(/\:/,$user);
  132: 		    $r->print(&Apache::loncommon::start_data_table_row().
  133: 			      '<td>'.
  134: 			      '<input type="button" value="'.&mt('Select').'" onclick="gochoose('.
  135: 			      "'".$puname."','".$pudom."')".'" /></td><td>'.$role.'</td><td>'.
  136: 			      &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($puname,$pudom),$puname,$pudom).'</td>'.
  137: 			      &Apache::loncommon::end_data_table_row());
  138:                 }
  139:             }
  140:             $r->print(&Apache::loncommon::end_data_table().'<br /><hr />');
  141:         }
  142: 
  143:         if ($filtermsg ne '') {
  144:             $r->print('<p>'.$filtermsg.'</p>');
  145:         }
  146: 
  147:         $r->rflush();
  148: 
  149:         unless ($env{'form.courseadv'} eq 'only') {
  150: 
  151: # ------------------------------------------------------------------ Students
  152: 
  153: # Do we have a clicker?
  154: 
  155:             my $clicker=$env{'form.clicker'};
  156:             my @fragments=();
  157:             if ($clicker) {
  158:                $r->print('<h1>'.&mt('Clicker: [_1]',$clicker).'</h1>');
  159:                my $clicklength=length($clicker);
  160:                my $maxlength=$clicklength-1;
  161:                if ($maxlength>2) {
  162:                   my $minlength=$maxlength-2;
  163:                   if ($minlength<2) { $minlength=2; }
  164:                   for (my $length=$maxlength;$length>=$minlength;$length--) {
  165:                       for (my $startidx=0; $startidx<=$clicklength-$length; $startidx++) {
  166:                           push(@fragments,substr($clicker,$startidx,$length));
  167:                       }
  168:                   }
  169:                }
  170:             }
  171: 
  172: 	    my $result;
  173:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  174:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  175:             my $classlist=&Apache::loncoursedata::get_classlist();
  176:             my %grouplist=&Apache::lonnet::get_group_membership($cdom,$cnum);
  177:             my $now = time;
  178:             my %allgroups = &Apache::longroup::coursegroups($cdom,$cnum);
  179: 
  180: 	    foreach my $user (sort(keys(%$classlist))) {
  181: 	        # the following undefs are for 'domain', and 'username' respectively.
  182: 	        my (undef,undef,$end,$start,$id,$section,$fullname,$status)=
  183: 		    @{$classlist->{$user}};
  184: 	        if ($allowed eq 'section' && $section ne $env{'request.course.sec'}) {
  185: 		    next;
  186: 	        }
  187: 	        if ($user=~/^($LONCAPA::username_re)\:($LONCAPA::domain_re)$/) {
  188: 		    my ($uname,$udom)=($1,$2);
  189: 		    if (($uname=~/^$filter/) || ($fullname=~/^$filter/i)) {
  190: 		        my $grouplist = '';
  191: 		        foreach my $group (sort(keys(%allgroups))) {
  192: 			    if (exists($grouplist{$group.':'.$uname.':'.$udom})) {
  193: 			        my ($end,$start) = split(/:/,$grouplist{$group.':'.$uname.':'.$udom});
  194: 			        if (($end!=0) && ($end<$now)) { next; }
  195: 			        if (($start!=0) && ($start>$now)) {next; }
  196: 			        $grouplist .= " $group,"; 
  197: 			    }
  198: 		        }
  199: 		        $grouplist =~ s/,$//;
  200:                         my $markedupclicker;
  201:                         if ($clicker) {
  202:                            $markedupclicker=(&Apache::lonnet::userenvironment($udom,$uname,'clickers'))[1];
  203:                            if ($markedupclicker!~/\w/) { 
  204:                               $markedupclicker='-'; 
  205:                            } else {
  206:                                foreach my $frag (@fragments) {
  207:                                   if ($markedupclicker=~/\Q$frag\E/is) {
  208:                                      $markedupclicker=~s/(\Q$frag\E)/<font color=\"green\" size=\"\+1\"><b>$1<\/b><\/font>/gis;
  209:                                      last;
  210:                                   }
  211:                                }
  212:                            }
  213:                         }
  214: 		        $result .=&Apache::loncommon::start_data_table_row().
  215: 		    	          '<td>'.
  216: 			          '<input type="button" value="'.&mt('Select').'" onclick="gochoose('.
  217: 			          "'".$uname."','".$udom."')".'" /></td>'.
  218: 			          '<td><tt>'.$uname.'</tt></td><td> <tt>'.$udom.
  219: 			          '</tt></td><td>'.
  220: 			          &Apache::loncommon::aboutmewrapper($fullname,
  221: 				    				     $uname,$udom).
  222: 			          '</td><td>'.$id.'</td><td>'.$section.
  223: 			          '</td><td>'.$grouplist.'</td>'.
  224:                                   ($clicker?'<td>'.$markedupclicker.'</td>':'').
  225: 			          &Apache::loncommon::end_data_table_row();
  226: 		    }
  227: 	        }
  228: 	    }
  229: 	    if (!$result) {
  230: 	        $r->print('<p>'.&mt('No students found.').'</p>');
  231: 	    } else {
  232: 	        $r->print(&Apache::loncommon::start_data_table().
  233: 	    	          &Apache::loncommon::start_data_table_header_row().
  234: 		          &Apache::loncommon::end_data_table_header_row().
  235: 		          '<th>&nbsp;</th>'.
  236: 		          '<th>'.&mt('username').'</th>'.
  237: 		          '<th>'.&mt('domain').'</th>'.
  238: 		          '<th>'.&mt('Name').'</th>'.
  239: 		          '<th>'.&mt('ID').'</th>'.
  240: 		          '<th>'.&mt('section').'</th>'.
  241: 		          '<th>'.&mt('active group(s)').'</th>'.
  242:                           ($clicker?'<th>'.&mt('clicker').'</th>':'').
  243: 		          &Apache::loncommon::end_data_table_header_row().
  244: 		          $result.
  245: 		          &Apache::loncommon::end_data_table());
  246: 	    }
  247:         }
  248:     } else {
  249: 	$r->print('<h3>'.&mt('Users with Roles Assigned by').' '.
  250: 		  &Apache::loncommon::plainname($env{'user.name'},
  251: 						$env{'user.domain'}).'</h3>');
  252: 	$r->print($filtermsg);
  253:         $r->rflush();
  254: 
  255: 	$r->print('<p>'.&Apache::loncommon::start_data_table());
  256: 	my %user_role=&Apache::lonnet::get_my_roles();
  257: 	my %users;
  258: 	foreach my $user_role (keys(%user_role)) {
  259: 	    next if ($user_role !~
  260: 		     /^($LONCAPA::username_re):($LONCAPA::domain_re):(\w+)$/);
  261: 	    my ($uname,$udom,$urole)=($1,$2,$3);
  262: 	    my $fullname=&Apache::loncommon::plainname($uname,$udom);
  263: 	    next if (($uname!~/^$filter/i) && ($fullname !~/^$filter/i));
  264: 	    
  265: 	    if (!exists($users{"$uname:$udom"})) {
  266: 		$users{"$uname:$udom"} = {'fullname' => $fullname };
  267: 	    }
  268: 	    push(@{$users{"$uname:$udom"}{'roles'}},$urole);
  269: 	}
  270: 	foreach my $user (sort {lc($a) cmp lc($b)} (keys(%users))) {
  271: 	    my ($uname,$udom) = split(':',$user);
  272: 	    $r->print(&Apache::loncommon::start_data_table_row().
  273: 		      '<td>'.
  274: 		      '<input type="button" value="'.&mt('Select').'" onclick="gochoose('.
  275: 		      "'".$uname."','".$udom."')".'" /></td>'.
  276: 		      '<td><tt>'.$uname.'</tt></td>'.
  277: 		      '<td><tt>'.$udom.'</tt></td><td>'.
  278: 		      &Apache::loncommon::aboutmewrapper($users{$user}{'fullname'},
  279: 							 $uname,$udom).'</td>'.
  280: 		      '<td>'.
  281: 		      join(', ',sort(map {&Apache::lonnet::plaintext($_)}
  282: 				         (@{$users{$user}{'roles'}}))).
  283: 		      '</td>'.
  284: 		      &Apache::loncommon::end_data_table_row());
  285: 	}
  286: 	$r->print(&Apache::loncommon::end_data_table().'</p>');
  287:     }
  288:     $r->print('</form>'.&Apache::loncommon::end_page());
  289:     return OK;
  290: }
  291: 
  292: 1;
  293: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>