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