1: # The LearningOnline Network with CAPA
2: # (Publication Handler
3: #
4: # $Id: lonchart.pm,v 1.34 2002/02/28 23:25:50 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, 2/28 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 usection2 {
229: my ($udom,$unam,$courseid,$ActiveFlag)=@_;
230: $courseid=~s/\_/\//g;
231: $courseid=~s/^(\w)/\/$1/;
232: foreach my $elem(split(/\&/,&Apache::lonnet::reply('dump:'.$udom.':'.$unam.':roles',
233: &Apache::lonnet::homeserver($unam,$udom)))) {
234: my ($key,$value)=split(/\=/,$elem);
235: $key=&Apache::lonnet::unescape($key);
236: if ($key=~/^$courseid(?:\/)*(\w+)*\_st$/) {
237: my $section=$1;
238: if ($key eq $courseid.'_st') { $section=''; }
239: my ($dummy,$end,$start)=split(/\_/,&Apache::lonnet::unescape($value));
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 == 1) { $notactive=0; }
249: unless ($notactive) { return $section; }
250: }
251: }
252: return '-1';
253: }
254:
255:
256: sub usection {
257: my ($udom,$unam,$courseid)=@_;
258: $courseid=~s/\_/\//g;
259: $courseid=~s/^(\w)/\/$1/;
260: foreach (split(/\&/,&Apache::lonnet::reply('dump:'.
261: $udom.':'.$unam.':roles',
262: &Apache::lonnet::homeserver($unam,$udom)))){
263: my ($key,$value)=split(/\=/,$_);
264: $key=&Apache::lonnet::unescape($key);
265: if ($key=~/^$courseid(?:\/)*(\w+)*\_st$/) {
266: my $section=$1;
267: if ($key eq $courseid.'_st') { $section=''; }
268: my ($dummy,$end,$start)=split(/\_/,&Apache::lonnet::unescape($value));
269: # $section=($section) ? $section : '(none)';
270: # $section=(int($section)) ? int($section) : $section;
271: # $r->print($unam.'...'.$section.'<br>');
272: return $section;
273: }
274: }
275: #} split(/\&/,&Apache::lonnet::reply('dump:'.$udom.':'.$unam.':roles',
276: # &Apache::lonnet::homeserver($unam,$udom)));
277: return '';
278: }
279:
280: sub BuildChart {
281: # ----------------------- Get first and last resource, see if there is anything
282: my $firstres=$hash{'map_start_/res/'.$ENV{'request.course.uri'}};
283: my $lastres=$hash{'map_finish_/res/'.$ENV{'request.course.uri'}};
284: if (($firstres) && ($lastres)) {
285: # ----------------------------------------------------------------- Render page
286: my $cid=$ENV{'request.course.id'};
287: my $chome=$ENV{'course.'.$cid.'.home'};
288: my ($cdom,$cnum)=split(/\_/,$cid);
289: # ---------------------------------------------- Read class list and row labels
290: my $classlst=&Apache::lonnet::reply
291: ('dump:'.$cdom.':'.$cnum.':classlist',$chome);
292: my $now=time;
293: unless ($classlst=~/^error\:/) {
294: foreach my $KeyPoint(sort split(/\&/,$classlst)) {
295: my ($name,$value)=split(/\=/,$KeyPoint);
296: my ($end,$start)=split(/\:/,&Apache::lonnet::unescape($value));
297: my $active=1;
298: if (($end) && ($now>$end)) { $active=0; }
299:
300: if ($ENV{'form.active'} eq 'All Students') { $active=1; }
301:
302: if ($active) {
303: my $thisindex=$#students+1;
304: $name=&Apache::lonnet::unescape($name);
305: $students[$thisindex]=$name;
306: my ($sname,$sdom)=split(/\:/,$name);
307: $PreCol[$thisindex]=$sname.':';
308:
309: my $ssec=&usection($sdom,$sname,$cid);#,$active);
310:
311: if ($ssec==-1) {
312: $rowlabels[$thisindex]=
313: 'Data not available: '.$name;
314: } else {
315: my %reply=&Apache::lonnet::idrget($sdom,$sname);
316: my $reply=&Apache::lonnet::reply('get:'.$sdom.':'.$sname.
317: ':environment:lastname&generation&firstname&middlename',
318: &Apache::lonnet::homeserver($sname,$sdom));
319: #$ssec=(int($ssec)) ? int($ssec) : $ssec;
320: my $sec=sprintf('%3s',$ssec);
321: $rowlabels[$thisindex]=$sec.' '.$reply{$sname}.' ';
322: $PreCol[$thisindex] .= $reply.':'.$sec;
323: my $i=0;
324: foreach (split(/\&/,$reply)) {
325: $i++;
326: if ( $_ ne '') {
327: $rowlabels[$thisindex].=&Apache::lonnet::unescape($_).' ';
328: }
329: if ($i == 2) {
330: chop($rowlabels[$thisindex]);
331: $rowlabels[$thisindex].=', ';
332: }
333: }
334: }
335: }
336: }
337:
338: } else {
339: $r->print('<h1>Could not access course data</h1>');
340: }
341:
342: my $allstudents=$#students+1;
343: $r->print('<h3>'.$allstudents.' students</h3>');
344: &CreateForm();
345: $r->rflush();
346:
347: # --------------- Find all assessments and put them into some linear-like order
348: &tracetable($firstres,'&'.$lastres.'&');
349: # ----------------------------------------------------------------- Start table
350:
351: $r->print('<p><pre>');
352: my $index;
353: for ($index=0;$index<=$#students;$index++) {
354: my $Str=&ExtractStudentData($index,$cid);
355: $r->print($Str.'<br>');
356: $r->rflush();
357: $CachData{$PreCol[$index]}=$Str;
358: }
359: $r->print('</pre>');
360: } else {
361: $r->print('<h3>Undefined course sequence</h3>');
362: }
363: }
364:
365: sub CreateForm {
366: my $OpSel1='';
367: my $OpSel2='';
368: if ( $ENV{'form.active'} eq 'All Students' ) { $OpSel2='selected'; }
369: else { $OpSel1 = 'selected'; }
370:
371: my $Ptr = '<form name=stat method=post action="/adm/chart" >'."\n";
372: $Ptr .= '<b> Sort by: </b>'."\n";
373: $Ptr .= ' ';
374: $Ptr .= '<input type=submit name=sort value="User Name" />'."\n";
375: $Ptr .= ' ';
376: $Ptr .= '<input type=submit name=sort value="Last Name" />'."\n";
377: $Ptr .= ' ';
378: $Ptr .= '<input type=submit name=sort value="Section"/>'."\n";
379: $Ptr .= '<br><br>';
380: $Ptr .= '<b> Activation Area: </b>'."\n".
381: '<select name="active"> <option '.$OpSel1.' >Active Students</option>'."\n".
382: '<option '.$OpSel2.'>All Students</option> </select> '."\n";
383: $Ptr .= ' ';
384: $Ptr .= '<input type=submit name=sort value="Recalculate Chart"/>'."\n";
385: $Ptr .= '</form>'."\n";
386: $r->print( $Ptr );
387: }
388:
389: sub CacheChart {
390: my @list = ();
391: my $count=0;
392:
393: my $Pos = $ENV{'form.sort'};
394: if ( $Pos eq 'Last Name' ) {$Pos=1;}
395: elsif ( $Pos eq 'Section' ) {$Pos=2;}
396: else {$Pos=0;}
397:
398: foreach my $key( keys %CachData) {
399: my @Temp=split(/\:/,$key);
400: my $Use = $Temp[$Pos];
401: $list[$count]=$Use.$key.'*'.$CachData{$key};
402: $count++;
403: }
404:
405: @list = sort (@list);
406:
407: $r->print('<h3>'.$count.' students</h3>');
408: &CreateForm();
409: $r->rflush();
410:
411: $r->print('<p><pre>');
412: for ( my $n; $n < $count; $n++) {
413: my ($dummy, $Line) = split(/\*/,$list[$n]);
414: $r->print($Line.'<br>');
415: }
416: $r->print('</pre>');
417: }
418:
419: sub Start {
420: undef %hash;
421: undef %CachData;
422: undef @students;
423: undef @cols;
424: undef @rowlabels;
425: undef @PreCol;
426:
427: $r->print('<html><head><title>'.
428: 'LON-CAPA Assessment Chart</title></head>');
429: $r->print('<body bgcolor="#FFFFFF">'.
430: '<script>window.focus();</script>'.
431: '<img align=right src=/adm/lonIcons/lonlogos.gif>'.
432: '<h1>Assessment Chart</h1>');
433: # ---------------------------------------------------------------- Course title
434: $r->print('<h1>'.$ENV{'course.'.$ENV{'request.course.id'}.
435: '.description'}.'</h1><h3>'.localtime().
436: "</h3><p><pre>1..9: correct by student in 1..9 tries\n".
437: " *: correct by student in more than 9 tries\n".
438: " +: correct by override\n".
439: " -: incorrect by override\n".
440: " .: incorrect attempted\n".
441: " #: ungraded attempted\n".
442: " : not attempted\n".
443: " x: excused</pre><p>");
444: # ------------------------------- This is going to take a while, produce output
445: $r->rflush();
446:
447: my $cid=$ENV{'request.course.id'};
448: my $ChartDB = "/home/httpd/perl/tmp/$ENV{'user.name'}".
449: "_$ENV{'user.domain'}_$cid\_chart.db";
450:
451: if ((-e "$ChartDB") && ($ENV{'form.sort'} ne 'Recalculate Chart')) {
452: if (tie(%CachData,'GDBM_File',"$ChartDB",&GDBM_READER,0640)) {
453: &CacheChart();
454: }
455: else {
456: $r->print("Unable to tie hash to db file");
457: }
458: }
459: else {
460: if (tie(%CachData,'GDBM_File',$ChartDB,&GDBM_WRCREAT,0640)) {
461: foreach (keys %CachData) {delete $CachData{$_};}
462: &BuildChart();
463: }
464: else {
465: $r->print("Unable to tie hash to db file");
466: }
467: }
468: untie(%CachData);
469: }
470:
471: # ================================================================ Main Handler
472:
473: sub handler {
474: $r=shift;
475: if (&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) {
476: # ------------------------------------------- Set document type for header only
477: if ($r->header_only) {
478: if ($ENV{'browser.mathml'}) {
479: $r->content_type('text/xml');
480: } else {
481: $r->content_type('text/html');
482: }
483: &Apache::loncommon::no_cache($r);
484: $r->send_http_header;
485: return OK;
486: }
487:
488: my $requrl=$r->uri;
489: # ----------------------------------------------------------------- Tie db file
490: if ($ENV{'request.course.fn'}) {
491: my $fn=$ENV{'request.course.fn'};
492: if (-e "$fn.db") {
493: if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
494: # ------------------------------------------------------------------- Hash tied
495: # ---------------------------------------------------------------- Send headers
496: $r->content_type('text/html');
497: $r->send_http_header;
498: &Start();
499: $r->print('</body></html>');
500: # ------------------------------------------------------------- End render page
501: } else {
502: $r->content_type('text/html');
503: $r->send_http_header;
504: $r->print('<html><body>Coursemap undefined.</body></html>');
505: }
506: # ------------------------------------------------------------------ Untie hash
507: unless (untie(%hash)) {
508: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
509: "Could not untie coursemap $fn (browse).</font>");
510: }
511:
512: # -------------------------------------------------------------------- All done
513: return OK;
514: # ----------------------------------------------- Errors, hash could no be tied
515: }
516: } else {
517: $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
518: return HTTP_NOT_ACCEPTABLE;
519: }
520: } else {
521: $ENV{'user.error.msg'}=
522: $r->uri.":vgr:0:0:Cannot view grades for complete course";
523: return HTTP_NOT_ACCEPTABLE;
524: }
525: }
526: 1;
527: __END__
528:
529:
530:
531:
532:
533:
534:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>