1: # The LearningOnline Network with CAPA
2: # (Publication Handler
3: #
4: # $Id: lonchart.pm,v 1.37 2002/03/02 02:03:47 minaeibi 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: # Homework Performance Chart
29: #
30: # (Navigate Maps Handler
31: #
32: # (Page Handler
33: #
34: # (TeX Content Handler
35: # YEAR=2000
36: # 05/29/00,05/30 Gerd Kortemeyer)
37: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
38: # 10/02,10/10,10/14,10/16,10/18,10/19,10/31,11/6,11/14,11/16 Gerd Kortemeyer)
39: # YEAR=2001
40: # 3/1/1,6/1,17/1,29/1,30/1,31/1 Gerd Kortemeyer)
41: # 7/10/01 Behrouz Minaei
42: # 9/8 Gerd Kortemeyer
43: # 10/1, 10/19, 11/17, 11/22, 11/24, 11/28 12/18 Behrouz Minaei
44: # YEAR=2002
45: # 2/1, 2/6, 2/19, 2/28 Behrouz Minaei
46: #
47: ###
48:
49: package Apache::lonchart;
50:
51: use strict;
52: use Apache::Constants qw(:common :http);
53: use Apache::lonnet();
54: use Apache::loncommon();
55: use HTML::TokeParser;
56: use GDBM_File;
57:
58: # -------------------------------------------------------------- Module Globals
59: my %hash;
60: my %CachData;
61: my @cols;
62: my @rowlabels;
63: my @students;
64: my @PreCol;
65: my $r;
66:
67: # ------------------------------------------------------------- Find out status
68:
69: sub ExtractStudentData {
70: my ($index,$coid)=@_;
71: my ($sname,$sdom) = split( /\:/, $students[$index] );
72: my $shome=&Apache::lonnet::homeserver( $sname,$sdom );
73: my $reply=&Apache::lonnet::reply('dump:'.$sdom.':'.$sname.':'.$coid,$shome );
74: my %result=();
75: my $ResId;
76: my $Code;
77: my $Tries;
78: my $Wrongs;
79: my %TempHash;
80: my $Version;
81: my $ProbNo;
82: my $ProbSolved;
83: my $ProbTotal;
84: my $LatestVersion;
85: my $Str=substr($students[$index].
86: ' ',0,14).' ! '.
87: substr($rowlabels[$index].
88: ' ',0,45).' ! ';
89: unless ($reply=~/^error\:/) {
90: foreach (split(/\&/,$reply)) {
91: my ($name,$value)=split(/\=/,&Apache::lonnet::unescape($_));
92: $result{$name}=$value;
93: }
94: $ProbNo = 0;
95: $ProbTotal = 0;
96: $ProbSolved = 0;
97: my $IterationNo = 0;
98: foreach $ResId (@cols) {
99: if ($IterationNo == 0) {$IterationNo++; next;}
100: if (!$ResId) {
101: my $PrNo = sprintf( "%3d", $ProbNo );
102: $Str .= ' '.'<font color="#007700">'.$PrNo.'</font> ';
103: $ProbSolved += $ProbNo;
104: $ProbNo=0;
105: next;
106: }
107: $ResId=~/(\d+)\.(\d+)/;
108: my $meta=$hash{'src_'.$ResId};
109: my $PartNo = 0;
110: undef %TempHash;
111: foreach (split(/\,/,&Apache::lonnet::metadata($meta,'keys'))) {
112: if ($_=~/^stores\_(\d+)\_tries$/) {
113: my $Part=&Apache::lonnet::metadata($meta,$_.'.part');
114: if ( $TempHash{"$Part"} eq '' ) {
115: $TempHash{"$Part"} = $Part;
116: $TempHash{$PartNo}=$Part;
117: $TempHash{"$Part.Code"} = ' ';
118: $PartNo++;
119: }
120: }
121: }
122:
123: my $Prob = &Apache::lonnet::declutter( $hash{'map_id_'.$1} ).
124: '___'.$2.'___'.
125: &Apache::lonnet::declutter( $hash{'src_'.$ResId} );
126: $Code=' ';
127: $Tries = 0;
128: $LatestVersion = $result{"version:$Prob"};
129:
130: if ( $LatestVersion ) {
131: for ( my $Version=1; $Version<=$LatestVersion; $Version++ ) {
132: my $vkeys = $result{"$Version:keys:$Prob"};
133: my @keys = split(/\:/,$vkeys);
134:
135: foreach my $Key (@keys) {
136: if (($Key=~/\.(\w+)\.solved$/) && ($Key!~/^\d+\:/)) {
137: my $Part = $1;
138: $Tries = $result{"$Version:$Prob:resource.$Part.tries"};
139: $TempHash{"$Part.Tries"}=($Tries) ? $Tries : 0;
140: my $Val = $result{"$Version:$Prob:resource.$Part.solved"};
141: if ($Val eq 'correct_by_student'){$Code='*';}
142: elsif ($Val eq 'correct_by_override'){$Code = '+';}
143: elsif ($Val eq 'incorrect_attempted'){$Code = '.';}
144: elsif ($Val eq 'incorrect_by_override'){$Code = '-';}
145: elsif ($Val eq 'excused'){$Code = 'x';}
146: elsif ($Val eq 'ungraded_attempted'){$Code = '#';}
147: else {$Code = ' ';}
148: $TempHash{"$Part.Code"} = $Code;
149: }
150: }
151: }
152: for ( my $n = 0; $n < $PartNo; $n++ ) {
153: my $part = $TempHash{$n};
154: my $Code = $TempHash{"$part.Code"};
155: if ( $Code eq '*') {
156: $ProbNo++;
157: if (($TempHash{"$part.Tries"}<10) ||
158: ($TempHash{"$part.Tries"} eq '')) {
159: $TempHash{"$part.Code"}=$TempHash{"$part.Tries"};
160: }
161: }
162: elsif ( $Code eq '+' ) {$ProbNo++;}
163: $Str .= $TempHash{"$part.Code"};
164: if ( $Code ne 'x' ) {$ProbTotal++;}
165: }
166: }
167: else {
168: for(my $n=0; $n<$PartNo; $n++) {
169: $Str.=' ';
170: $ProbTotal++;
171: }
172: }
173: }
174: }
175: my $PrTot = sprintf( "%5d", $ProbTotal );
176: my $PrSvd = sprintf( "%5d", $ProbSolved );
177: $Str .= ' '.'<font color="#000088">'.$PrSvd.' /'.$PrTot.'</font> ';
178:
179: return $Str ;
180: }
181:
182:
183: # ------------------------------------------------------------ Build page table
184:
185: sub tracetable {
186: my ($rid,$beenhere)=@_;
187: unless ($beenhere=~/\&$rid\&/) {
188: $beenhere.=$rid.'&';
189: # new ... updating the map according to sequence and page
190: if (defined($hash{'is_map_'.$rid})) {
191: my $cmap=$hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$rid}}};
192: if ( $cmap eq 'sequence' || $cmap eq 'page' ) {
193: $cols[$#cols+1]=0;
194: }
195: if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
196: (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
197: my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
198:
199: &tracetable($hash{'map_start_'.$hash{'src_'.$rid}},
200: '&'.$frid.'&');
201:
202: if ($hash{'src_'.$frid}) {
203: if ($hash{'src_'.$frid}=~
204: /\.(problem|exam|quiz|assess|survey|form)$/) {
205: $cols[$#cols+1]=$frid;
206: }
207: }
208:
209: }
210: } else {
211: if ($hash{'src_'.$rid}) {
212: if ($hash{'src_'.$rid}=~
213: /\.(problem|exam|quiz|assess|survey|form)$/) {
214: $cols[$#cols+1]=$rid;
215: }
216: }
217: }
218: if (defined($hash{'to_'.$rid})) {
219: foreach (split(/\,/,$hash{'to_'.$rid})){
220: &tracetable($hash{'goesto_'.$_},$beenhere);
221: }
222: }
223: }
224: }
225:
226: sub usection {
227: my ($udom,$unam,$courseid,$ActiveFlag)=@_;
228: $courseid=~s/\_/\//g;
229: $courseid=~s/^(\w)/\/$1/;
230: foreach (split(/\&/,&Apache::lonnet::reply('dump:'.
231: $udom.':'.$unam.':roles',
232: &Apache::lonnet::homeserver($unam,$udom)))){
233: my ($key,$value)=split(/\=/,$_);
234: $key=&Apache::lonnet::unescape($key);
235: if ($key=~/^$courseid(?:\/)*(\w+)*\_st$/) {
236: my $section=$1;
237: if ($key eq $courseid.'_st') { $section=''; }
238: my ($dummy,$end,$start)=split(/\_/,&Apache::lonnet::unescape($value));
239: if ( $ActiveFlag ne 'Any' ) {
240: my $now=time;
241: my $notactive=0;
242: if ($start) {
243: if ($now<$start) { $notactive=1; }
244: }
245: if ($end) {
246: if ($now>$end) { $notactive=1; }
247: }
248: if ((($ActiveFlag eq 'Expired') && $notactive == 1) ||
249: (($ActiveFlag eq 'Active') && $notactive == 0 ) ) {
250: return $section;
251: }
252: else { return '-1'; }
253: }
254: return $section;
255: }
256: }
257: return '-1';
258: }
259:
260: sub BuildChart {
261: # ----------------------- Get first and last resource, see if there is anything
262: my $firstres=$hash{'map_start_/res/'.$ENV{'request.course.uri'}};
263: my $lastres=$hash{'map_finish_/res/'.$ENV{'request.course.uri'}};
264: if (($firstres) && ($lastres)) {
265: # ----------------------------------------------------------------- Render page
266: my $cid=$ENV{'request.course.id'};
267: my $chome=$ENV{'course.'.$cid.'.home'};
268: my ($cdom,$cnum)=split(/\_/,$cid);
269: # ---------------------------------------------- Read class list and row labels
270: my $classlst=&Apache::lonnet::reply
271: ('dump:'.$cdom.':'.$cnum.':classlist',$chome);
272: my $now=time;
273: unless ($classlst=~/^error\:/) {
274: foreach my $KeyPoint(sort split(/\&/,$classlst)) {
275: my ($name,$value)=split(/\=/,$KeyPoint);
276: my ($end,$start)=split(/\:/,&Apache::lonnet::unescape($value));
277: my $active=1;
278: my $Status=$ENV{'form.status'};
279: $Status = ($Status) ? $Status : 'Active';
280: if ( ( ($end) && $now > $end ) &&
281: ( ($Status eq 'Active') ) ) { $active=0; }
282:
283: if ( ($Status eq 'Expired') &&
284: ($end == 0 || $now < $end) ) { $active=0; }
285:
286: if ($active) {
287: my $thisindex=$#students+1;
288: $name=&Apache::lonnet::unescape($name);
289: $students[$thisindex]=$name;
290: my ($sname,$sdom)=split(/\:/,$name);
291: $PreCol[$thisindex]=$sname.':';
292: my $ssec=&usection($sdom,$sname,$cid,$Status);
293: if ($ssec==-1) {
294: $rowlabels[$thisindex]=
295: 'Data not available: '.$name;
296: }
297: else {
298: my %reply=&Apache::lonnet::idrget($sdom,$sname);
299: my $reply=&Apache::lonnet::reply('get:'.$sdom.':'.$sname.
300: ':environment:lastname&generation&firstname&middlename',
301: &Apache::lonnet::homeserver($sname,$sdom));
302: #$ssec=(int($ssec)) ? int($ssec) : $ssec;
303: my $sec=sprintf('%3s',$ssec);
304: $rowlabels[$thisindex]=$sec.' '.$reply{$sname}.' ';
305: $PreCol[$thisindex] .= $reply.':'.$sec;
306: my $i=0;
307: foreach (split(/\&/,$reply)) {
308: $i++;
309: if ( $_ ne '') {
310: $rowlabels[$thisindex].=&Apache::lonnet::unescape($_).' ';
311: }
312: if ($i == 2) {
313: chop($rowlabels[$thisindex]);
314: $rowlabels[$thisindex].=', ';
315: }
316: }
317: }
318: }
319: }
320:
321: } else {
322: $r->print('<h1>Could not access course data</h1>');
323: }
324:
325: my $allstudents=$#students+1;
326: $r->print('<h3>'.$allstudents.' students</h3>');
327: &CreateForm();
328: $r->rflush();
329:
330: # --------------- Find all assessments and put them into some linear-like order
331: &tracetable($firstres,'&'.$lastres.'&');
332: # ----------------------------------------------------------------- Start table
333:
334: $r->print('<p><pre>');
335: my $index;
336: for ($index=0;$index<=$#students;$index++) {
337: my $Str=&ExtractStudentData($index,$cid);
338: $r->print($Str.'<br>');
339: $r->rflush();
340: $CachData{$PreCol[$index]}=$Str;
341: }
342: $r->print('</pre>');
343: } else {
344: $r->print('<h3>Undefined course sequence</h3>');
345: }
346: }
347:
348: sub CreateForm {
349: my $OpSel1='';
350: my $OpSel2='';
351: my $OpSel3='';
352: my $Status = $ENV{'form.status'};
353: if ( $Status eq 'Any' ) { $OpSel3='selected'; }
354: elsif ($Status eq 'Expired' ) { $OpSel2 = 'selected'; }
355: else { $OpSel1 = 'selected'; }
356:
357: my $Ptr = '<form name=stat method=post action="/adm/chart" >'."\n";
358: $Ptr .= '<b> Sort by: </b>'."\n";
359: $Ptr .= ' ';
360: $Ptr .= '<input type=submit name=sort value="User Name" />'."\n";
361: $Ptr .= ' ';
362: $Ptr .= '<input type=submit name=sort value="Last Name" />'."\n";
363: $Ptr .= ' ';
364: $Ptr .= '<input type=submit name=sort value="Section"/>'."\n";
365: $Ptr .= '<br><br>';
366: $Ptr .= '<b> Student Status: </b>'."\n".
367: '<select name="status">'.
368: '<option '.$OpSel1.' >Active</option>'."\n".
369: '<option '.$OpSel2.' >Expired</option>'."\n".
370: '<option '.$OpSel3.' >Any</option> </select> '."\n";
371: $Ptr .= ' ';
372: $Ptr .= '<input type=submit name=sort value="Recalculate Chart"/>'."\n";
373: $Ptr .= '</form>'."\n";
374: $r->print( $Ptr );
375: }
376:
377: sub CacheChart {
378: my %list = ();
379: my $count=0;
380:
381: my $Pos = $ENV{'form.sort'};
382: if ( $Pos eq 'Last Name' ) {$Pos=1;}
383: elsif ( $Pos eq 'Section' ) {$Pos=2;}
384: else {$Pos=0;}
385:
386: foreach my $key( keys %CachData) {
387: my @Temp=split(/\:/,$key);
388: my $Use = $Temp[$Pos];
389: $list{$Use.$key}=$key;
390: $count++;
391: }
392:
393: my @order = sort(keys(%list));
394:
395: $r->print('<h3>'.$count.' students</h3>');
396: &CreateForm();
397: $r->rflush();
398:
399: $r->print('<p><pre>');
400: for ( my $n; $n < $count; $n++) {
401: $r->print($CachData{$list{$order[$n]}}.'<br>');
402: }
403: $r->print('</pre>');
404: }
405:
406: sub Start {
407: undef %hash;
408: undef %CachData;
409: undef @students;
410: undef @cols;
411: undef @rowlabels;
412: undef @PreCol;
413:
414: $r->print('<html><head><title>'.
415: 'LON-CAPA Assessment Chart</title></head>');
416: $r->print('<body bgcolor="#FFFFFF">'.
417: '<script>window.focus();</script>'.
418: '<img align=right src=/adm/lonIcons/lonlogos.gif>'.
419: '<h1>Assessment Chart</h1>');
420: # ---------------------------------------------------------------- Course title
421: $r->print('<h1>'.$ENV{'course.'.$ENV{'request.course.id'}.
422: '.description'}.'</h1><h3>'.localtime().
423: "</h3><p><pre>1..9: correct by student in 1..9 tries\n".
424: " *: correct by student in more than 9 tries\n".
425: " +: correct by override\n".
426: " -: incorrect by override\n".
427: " .: incorrect attempted\n".
428: " #: ungraded attempted\n".
429: " : not attempted\n".
430: " x: excused</pre><p>");
431: # ------------------------------- This is going to take a while, produce output
432: $r->rflush();
433:
434: my $cid=$ENV{'request.course.id'};
435: my $ChartDB = "/home/httpd/perl/tmp/$ENV{'user.name'}".
436: "_$ENV{'user.domain'}_$cid\_chart.db";
437:
438: if ((-e "$ChartDB") && ($ENV{'form.sort'} ne 'Recalculate Chart')) {
439: if (tie(%CachData,'GDBM_File',"$ChartDB",&GDBM_READER,0640)) {
440: &CacheChart();
441: }
442: else {
443: $r->print("Unable to tie hash to db file");
444: }
445: }
446: else {
447: if (tie(%CachData,'GDBM_File',$ChartDB,&GDBM_WRCREAT,0640)) {
448: foreach (keys %CachData) {delete $CachData{$_};}
449: &BuildChart();
450: }
451: else {
452: $r->print("Unable to tie hash to db file");
453: }
454: }
455: untie(%CachData);
456: }
457:
458: # ================================================================ Main Handler
459:
460: sub handler {
461: $r=shift;
462: if (&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) {
463: # ------------------------------------------- Set document type for header only
464: if ($r->header_only) {
465: if ($ENV{'browser.mathml'}) {
466: $r->content_type('text/xml');
467: } else {
468: $r->content_type('text/html');
469: }
470: &Apache::loncommon::no_cache($r);
471: $r->send_http_header;
472: return OK;
473: }
474:
475: my $requrl=$r->uri;
476: # ----------------------------------------------------------------- Tie db file
477: if ($ENV{'request.course.fn'}) {
478: my $fn=$ENV{'request.course.fn'};
479: if (-e "$fn.db") {
480: if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
481: # ------------------------------------------------------------------- Hash tied
482: # ---------------------------------------------------------------- Send headers
483: $r->content_type('text/html');
484: $r->send_http_header;
485: &Start();
486: $r->print('</body></html>');
487: # ------------------------------------------------------------- End render page
488: } else {
489: $r->content_type('text/html');
490: $r->send_http_header;
491: $r->print('<html><body>Coursemap undefined.</body></html>');
492: }
493: # ------------------------------------------------------------------ Untie hash
494: unless (untie(%hash)) {
495: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
496: "Could not untie coursemap $fn (browse).</font>");
497: }
498:
499: # -------------------------------------------------------------------- All done
500: return OK;
501: # ----------------------------------------------- Errors, hash could no be tied
502: }
503: } else {
504: $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
505: return HTTP_NOT_ACCEPTABLE;
506: }
507: } else {
508: $ENV{'user.error.msg'}=
509: $r->uri.":vgr:0:0:Cannot view grades for complete course";
510: return HTTP_NOT_ACCEPTABLE;
511: }
512: }
513: 1;
514: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>