--- loncom/interface/lonuserutils.pm 2007/11/06 04:39:19 1.2 +++ loncom/interface/lonuserutils.pm 2007/11/10 22:18:09 1.4 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Utility functions for managing LON-CAPA user accounts # -# $Id: lonuserutils.pm,v 1.2 2007/11/06 04:39:19 raeburn Exp $ +# $Id: lonuserutils.pm,v 1.4 2007/11/10 22:18:09 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -1130,23 +1130,36 @@ sub print_userlist { } my ($indexhash,$keylist) = &make_keylist_array(); my (%userlist,%userinfo); + if ($context eq 'domain' && $env{'form.roletype'} eq 'course') { + my $courseform = + &Apache::lonhtmlcommon::course_selection($formname,$totcodes, + $codetitles,$idlist,$idlist_titles); + $r->print('<p>'.&Apache::lonhtmlcommon::start_pick_box()."\n". + &Apache::lonhtmlcommon::start_pick_box()."\n". + &Apache::lonhtmlcommon::row_title(&mt('Select Course(s)'), + 'LC_oddrow_value')."\n". + $courseform."\n". + &Apache::lonhtmlcommon::row_closure(1). + &Apache::lonhtmlcommon::end_pick_box().'</p>'. + '<p>'.&list_submit_button(&mt('Update Display')). + "\n</p>\n"); + } + $r->print('<hr />'.&mt('Searching').' ...<br /> <br />'); + $r->rflush(); if ($context eq 'course') { - # - # Print the userlist - $r->print('<h2>'.&mt('Current User List').'</h2>'); - (my $classlist,$keylist)=&Apache::loncoursedata::get_classlist(); - - if (exists($permission->{'view_section'})) { - my $sec = &Apache::loncoursedata::CL_SECTION(); - foreach my $student (keys(%{$classlist})) { - if ($userlist{$student}[$sec] ne $permission->{'view_section'}) { - delete($userlist{$student}); + my $classlist = &Apache::loncoursedata::get_classlist(); + my $secidx = &Apache::loncoursedata::CL_SECTION(); + foreach my $student (keys(%{$classlist})) { + if (exists($permission->{'view_section'})) { + if ($classlist->{$student}[$secidx] ne $permission->{'view_section'}) { + next; + } else { + $userlist{$student} = $classlist->{$student}; } + } else { + $userlist{$student} = $classlist->{$student}; } } - foreach my $item (keys(%{$classlist})) { - $userlist{$item} = $classlist->{$item}; - } my $cid =$env{'request.course.id'}; my $cdom=$env{'course.'.$cid.'.domain'}; my $cnum=$env{'course.'.$cid.'.num'}; @@ -1206,19 +1219,6 @@ sub print_userlist { } } } elsif ($env{'form.roletype'} eq 'course') { - my $courseform = - &Apache::lonhtmlcommon::course_selection($formname,$totcodes, - $codetitles,$idlist,$idlist_titles); - my $output='<p>'.&Apache::lonhtmlcommon::start_pick_box()."\n". - &Apache::lonhtmlcommon::start_pick_box()."\n". - &Apache::lonhtmlcommon::row_title(&mt('Select Course(s)'), - 'LC_oddrow_value')."\n". - $courseform."\n". - &Apache::lonhtmlcommon::row_closure(1). - &Apache::lonhtmlcommon::end_pick_box().'</p>'; - $r->print($output); - $r->print('<p>'.&list_submit_button(&mt('Update Display')). - "\n</p>\n"); if ($env{'form.coursepick'}) { my %courses = &process_coursepick(); my %allusers; @@ -1264,29 +1264,33 @@ sub print_userlist { } } } - if (keys(%userlist) == 0) { - if ($context eq 'construction_space') { - $r->print(&mt('There are no co-authors to display.')."\n"); - } elsif ($context eq 'domain') { - if ($env{'form.roletype'} eq 'domain') { - $r->print(&mt('There are no users with domain roles to display.')."\n"); - } elsif ($env{'form.roletype'} eq 'construction_space') { - $r->print(&mt('There are no authors or co-authors to display.')."\n"); - } elsif ($env{'form.roletype'} eq 'course') { - $r->print(&mt('There are no course users to display')."\n"); - } - } elsif ($context eq 'course') { - $r->print(&mt('There are no course users to display.')."\n"); + } + if (keys(%userlist) == 0) { + if ($context eq 'construction_space') { + $r->print(&mt('There are no co-authors to display.')."\n"); + } elsif ($context eq 'domain') { + if ($env{'form.roletype'} eq 'domain') { + $r->print(&mt('There are no users with domain roles to display.')."\n"); + } elsif ($env{'form.roletype'} eq 'construction_space') { + $r->print(&mt('There are no authors or co-authors to display.')."\n"); + } elsif ($env{'form.roletype'} eq 'course') { + $r->print(&mt('There are no course users to display')."\n"); } - } else { - # Print out the available choices - if ($env{'form.action'} eq 'modifystudent') { - &show_users_list($r,$context,'view','modify', - $env{'form.Status'},\%userlist,$keylist); - } else { - &show_users_list($r,$context,$env{'form.output'},'aboutme', + } elsif ($context eq 'course') { + $r->print(&mt('There are no course users to display.')."\n"); + } + } else { + # Print out the available choices + my $usercount; + if ($env{'form.action'} eq 'modifystudent') { + ($usercount) = &show_users_list($r,$context,'view','modify', $env{'form.Status'},\%userlist,$keylist); - } + } else { + ($usercount) = &show_users_list($r,$context,$env{'form.output'}, + 'aboutme',$env{'form.Status'},\%userlist,$keylist); + } + if (!$usercount) { + $r->print('<br />'.&mt('There are no users matching the search criteria.')); } } $r->print('</form>'); @@ -1603,14 +1607,14 @@ sub show_users_list { my ($CSVfile,$CSVfilename); # my $sortby = $env{'form.sortby'}; + my @sortable = ('username','domain','id','fullname','start','end','email','role'); if ($context eq 'course') { - if ($sortby !~ /^(username|domain|section|groups|fullname|id|start|end|type)$/) { - $sortby = 'username'; - } + push(@sortable,('section','groups','type')); } else { - if ($sortby !~ /^(username|domain|id|fullname|start|end|role|email|extent)$/) { - $sortby = 'username'; - } + push(@sortable,'extent'); + } + if (!grep(/^\Q$sortby\E$/,@sortable)) { + $sortby = 'username'; } my ($cid,$cdom,$cnum,$classgroups,$displayphotos,$displayclickers); if ($context eq 'course') { @@ -1683,7 +1687,7 @@ END if (!($context eq 'domain' && $env{'form.roletype'} eq 'course')) { push(@cols,('start','end')); } - if ($env{'form.showrole'} eq 'Any') { + if ($env{'form.showrole'} eq 'Any' || $env{'form.showrole'} eq 'cr') { push(@cols,'role'); } if ($context eq 'domain' && ($env{'form.roletype'} eq 'construction_space' || @@ -1699,76 +1703,76 @@ END } push(@cols,'email'); - my $rolefilter; + my $rolefilter = $env{'form.showrole'}; if ($env{'form.showrole'} ne 'Any') { $rolefilter = &Apache::lonnet::plaintext($env{'form.showrole'}); } my $results_description = &results_header_row($rolefilter,$statusmode, $context); - + $r->print('<b>'.$results_description.'</b><br />'); + my $output; if ($mode eq 'html' || $mode eq 'view') { - $r->print('<hr />'.&mt('Searching').' ...<br /> <br />'); - $r->rflush(); - $r->print('<b>'.$results_description.'</b><br />'); - if ($linkto eq 'aboutme') { - $r->print(&mt("Select a user name to view the user's personal page.")); - } elsif ($linkto eq 'modify') { - $r->print(&mt("Select a user name to modify the user's information")); - } $r->print(<<END); <input type="hidden" name="sname" value="" /> <input type="hidden" name="sdom" value="" /> END - $r->print("\n<p>\n". + if ($linkto eq 'aboutme') { + $output = &mt("Select a user name to view the user's personal page."); + } elsif ($linkto eq 'modify') { + $output = &mt("Select a user name to modify the user's information"); + } + $output .= "\n<p>\n". &Apache::loncommon::start_data_table(). - &Apache::loncommon::start_data_table_header_row()); + &Apache::loncommon::start_data_table_header_row(); if ($mode eq 'autoenroll') { - $r->print(" + $output .= " <th><a href=\"javascript:document.studentform.sortby.value='type';document.studentform.submit();\">$lt{'type'}</a></th> - "); + "; } else { - $r->print(" + $output .= " <th>Count</th> - "); + "; } foreach my $item (@cols) { - $r->print("<th><a href=\"javascript:document.studentform.sortby.value='$item';document.studentform.submit();\">$lt{$item}</a></th>\n"); + $output .= "<th><a href=\"javascript:document.studentform.sortby.value='$item';document.studentform.submit();\">$lt{$item}</a></th>\n"; } my %role_types = &role_type_names(); if ($context eq 'course') { - # Clicker display on or off? - my %clicker_options = &Apache::lonlocal::texthash( - 'on' => 'Show', - 'off' => 'Hide', - ); - my $clickerchg = 'on'; - if ($displayclickers eq 'on') { - $clickerchg = 'off'; - } - $r->print(' <th>'."\n".' '. - '<a href="javascript:document.studentform.displayclickers.value='. + if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') { + # Clicker display on or off? + my %clicker_options = &Apache::lonlocal::texthash( + 'on' => 'Show', + 'off' => 'Hide', + ); + my $clickerchg = 'on'; + if ($displayclickers eq 'on') { + $clickerchg = 'off'; + } + $output .= ' <th>'."\n".' '. + '<a href="javascript:document.studentform.displayclickers.value='. "'".$clickerchg."'".';document.studentform.submit();">'. $clicker_options{$clickerchg}.'</a> '.$lt{'clicker'}."\n". - ' </th>'."\n"); + ' </th>'."\n"; - # Photo display on or off? - if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) { - my %photo_options = &Apache::lonlocal::texthash( - 'on' => 'Show', - 'off' => 'Hide', - ); - my $photochg = 'on'; - if ($displayphotos eq 'on') { - $photochg = 'off'; - } - $r->print(' <th>'."\n".' '. - '<a href="javascript:document.studentform.displayphotos.value='. + # Photo display on or off? + if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) { + my %photo_options = &Apache::lonlocal::texthash( + 'on' => 'Show', + 'off' => 'Hide', + ); + my $photochg = 'on'; + if ($displayphotos eq 'on') { + $photochg = 'off'; + } + $output .= ' <th>'."\n".' '. + '<a href="javascript:document.studentform.displayphotos.value='. "'".$photochg."'".';document.studentform.submit();">'. $photo_options{$photochg}.'</a> '.$lt{'photo'}."\n". - ' </th>'."\n"); + ' </th>'."\n"; + } } - $r->print(&Apache::loncommon::end_data_table_header_row()); - } + $output .= &Apache::loncommon::end_data_table_header_row(); + } # Done with the HTML header line } elsif ($mode eq 'csv') { # @@ -1808,10 +1812,13 @@ END foreach my $idx (@$keylist) { $index{$idx} = $i++; } + my $usercount = 0; # Get groups, role, permanent e-mail so we can sort on them if # necessary. foreach my $user (keys(%{$userlist})) { my ($uname,$udom,$role,$groups,$email); + next if (($statusmode ne 'Any') && + ($userlist->{$user}->[$index{'status'}] ne $statusmode)); if ($context eq 'domain') { if ($env{'form.roletype'} eq 'domain') { ($role,$uname,$udom) = split(/:/,$user); @@ -1842,8 +1849,21 @@ END if ($emails{'permanentemail'} =~ /\S/) { $userlist->{$user}->[$index{'email'}] = $emails{'permanentemail'}; } + $usercount ++; + } + my $autocount = 0; + my $manualcount = 0; + my $lockcount = 0; + my $unlockcount = 0; + if ($usercount) { + $r->print($output); + } else { + if ($mode eq 'autoenroll') { + return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount); + } else { + return; + } } - # # Sort the users my $index = $index{$sortby}; @@ -1855,18 +1875,14 @@ END lc($userlist->{$a}->[$second]) cmp lc($userlist->{$b}->[$second]) || lc($userlist->{$a}->[$third]) cmp lc($userlist->{$b}->[$third]) } (keys(%$userlist)); - my $usercount = 0; - my $autocount = 0; - my $manualcount = 0; - my $lockcount = 0; - my $unlockcount = 0; + my $rowcount = 0; foreach my $user (@sorted_users) { - my $sdata = $userlist->{$user}; my %in; + my $sdata = $userlist->{$user}; + $rowcount ++; foreach my $item (@{$keylist}) { $in{$item} = $sdata->[$index{$item}]; } - next if (($statusmode ne 'Any') && ($in{'status'} ne $statusmode)); $in{'role'}=&Apache::lonnet::plaintext($sdata->[$index{'role'}]); if (! defined($in{'start'}) || $in{'start'} == 0) { $in{'start'} = &mt('none'); @@ -1878,10 +1894,9 @@ END } else { $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'}); } - $usercount ++; if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll') { $r->print(&Apache::loncommon::start_data_table_row()); - $r->print("<td>$usercount</td>\n"); + $r->print("<td>$rowcount</td>\n"); if ($linkto eq 'aboutme') { $in{'username'} = &Apache::loncommon::aboutmewrapper($in{'username'}, @@ -1900,23 +1915,25 @@ END $r->print('<td>'.$in{$item}.'</td>'."\n"); } if ($context eq 'course') { - if ($displayclickers eq 'on') { - my $clickers = + if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') { + if ($displayclickers eq 'on') { + my $clickers = (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1]; - if ($clickers!~/\w/) { $clickers='-'; } - $r->print('<td>'.$clickers.'</td>'); - } else { - $r->print(' <td> </td> '); - } - if ($displayphotos eq 'on') { - if ($env{'course.'.$env{'request.course.id'}. - '.internal.showphoto'}) { - my $imgurl = - &Apache::lonnet::retrievestudentphoto($in{'domain'},$in{'username'},'gif','thumbnail'); - $r->print(' <td align="right"><a href="javascript:photowindow('."'".&Apache::lonnet::studentphoto($in{'domain'},$in{'username'},'jpg')."'".')"><img src="'.$imgurl.'" border="1"></a></td>'); + if ($clickers!~/\w/) { $clickers='-'; } + $r->print('<td>'.$clickers.'</td>'); } else { $r->print(' <td> </td> '); } + if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) { + if ($displayphotos eq 'on' && $sdata->[$index{'role'}] eq 'st') { + my $imgurl = + &Apache::lonnet::retrievestudentphoto($in{'domain'},$in{'username'}, + 'gif','thumbnail'); + $r->print(' <td align="right"><a href="javascript:photowindow('."'".&Apache::lonnet::studentphoto($in{'domain'},$in{'username'},'jpg')."'".')"><img src="'.$imgurl.'" border="1"></a></td>'); + } else { + $r->print(' <td> </td> '); + } + } } } $r->print(&Apache::loncommon::end_data_table_row()); @@ -1971,8 +1988,9 @@ END } if ($mode eq 'autoenroll') { return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount); + } else { + return ($usercount); } - return; } sub role_type_names { @@ -1988,7 +2006,7 @@ sub results_header_row { my ($rolefilter,$statusmode,$context) = @_; my $description; if ($context eq 'course') { - $description = &mt('Course - ').$env{'course.'.env{'request.course.id'}.'.description'}.': '; + $description = &mt('Course - ').$env{'course.'.$env{'request.course.id'}.'.description'}.': '; if ($statusmode eq 'Expired') { $description .= &mt('Users in course with expired [_1] roles',$rolefilter); }