1: # The LearningOnline Network with CAPA
2: #
3: # $Id: lontrackstudent.pm,v 1.22 2007/02/12 23:58:42 albertel Exp $
4: #
5: # Copyright Michigan State University Board of Trustees
6: #
7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
8: #
9: # LON-CAPA is free software; you can redistribute it and/or modify
10: # it under the terms of the GNU General Public License as published by
11: # the Free Software Foundation; either version 2 of the License, or
12: # (at your option) any later version.
13: #
14: # LON-CAPA is distributed in the hope that it will be useful,
15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: # GNU General Public License for more details.
18: #
19: # You should have received a copy of the GNU General Public License
20: # along with LON-CAPA; if not, write to the Free Software
21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: #
23: # /home/httpd/html/adm/gpl.txt
24: #
25: # http://www.lon-capa.org/
26: #
27: ###
28:
29: =pod
30:
31: =head1 NAME
32:
33: lontrackstudent
34:
35: =head1 SYNOPSIS
36:
37: Track student progress through course materials
38:
39: =over 4
40:
41: =cut
42:
43: package Apache::lontrackstudent;
44:
45: use strict;
46: use Apache::Constants qw(:common :http);
47: use Apache::lonmysql;
48: use Apache::lonnet;
49: use Apache::lonlocal;
50: use Time::HiRes;
51: use lib '/home/httpd/lib/perl/';
52: use LONCAPA;
53:
54: my $num_records=500;
55:
56: sub get_data {
57: my ($r,$prog_state,$navmap,$mode) = @_;
58: ##
59: ## Compose the query
60: &Apache::lonhtmlcommon::Update_PrgWin
61: ($r,$prog_state,&mt('Composing Query'));
62: #
63: # Allow the other server to begin processing the data before we ask for it.
64: sleep(5);
65: #
66: my $max_time = &get_max_time_in_db($r,$prog_state);
67: if (defined($max_time)) {
68: $r->print('<h3>'.&mt('Activity data compiled up to [_1]',
69: &Apache::lonlocal::locallocaltime($max_time)).
70: '</h3>'.&mt('While data is processed, periodically reload this page for more recent activity').'<br />');
71: $r->rflush();
72: } else {
73: $r->print('<h3>'.&mt('Unable to retrieve any data. Please reload this page and try again.').'</h3>');
74: return;
75: }
76: my $query = &build_query($mode);
77: ##
78: ## Send it along
79: my $home = $env{'course.'.$env{'request.course.id'}.'.home'};
80: my $reply=&Apache::lonnet::metadata_query($query,undef,undef,[$home]);
81: if (ref($reply) ne 'HASH') {
82: $r->print('<h2>'.
83: &mt('Error contacting home server for course: [_1]',
84: $reply).
85: '</h2>');
86: return;
87: }
88: my $results_file = $r->dir_config('lonDaemons').'/tmp/'.$reply->{$home};
89: my $endfile = $results_file.'.end';
90: ##
91: ## Check for the results
92: &Apache::lonhtmlcommon::Update_PrgWin
93: ($r,$prog_state,&mt('Waiting for results'));
94: my $maxtime = 500;
95: my $starttime = time;
96: while (! -e $endfile && (time-$starttime < $maxtime)) {
97: &Apache::lonhtmlcommon::Update_PrgWin
98: ($r,$prog_state,&mt('Waiting up to [_1] seconds for results',
99: $starttime+$maxtime-time));
100: sleep(1);
101: }
102: if (! -e $endfile) {
103: $r->print('<h2>'.
104: &mt('Unable to retrieve data.').'</h2>');
105: $r->print(&mt('Please try again in a few minutes.'));
106: return;
107: }
108: $r->rflush();
109: #
110: &Apache::lonhtmlcommon::Update_PrgWin
111: ($r,$prog_state,&mt('Parsing results'));
112: #
113: my $last = &output_results($r,$results_file,$navmap,$mode);
114: my ($sname,$sdom) = ($mode=~/^student:(.*):(.*)$/);
115:
116: my ($text,$inc);
117: if ( $last > 0 && (($last+1) >= $env{'form.start'}+$num_records) ) {
118: $text = 'View more activity by this student';
119: $inc = $num_records;
120: $r->print(&Apache::loncommon::track_student_link($text,$sname,$sdom,undef,
121: ($env{'form.start'}+$inc)
122: ));
123: $r->print('<br />');
124: }
125: $text = 'Resubmit last request to check for newer data';
126: $r->print(&Apache::loncommon::track_student_link($text,$sname,$sdom,undef,
127: $env{'form.start'}));
128:
129: &Apache::lonhtmlcommon::Update_PrgWin($r,$prog_state,&mt('Finished!'));
130: return;
131: }
132:
133: sub table_names {
134: my $cid = $env{'request.course.id'};
135: my $domain = $env{'course.'.$cid.'.domain'};
136: my $home = $env{'course.'.$cid.'.home'};
137: my $course = $env{'course.'.$cid.'.num'};
138: my $prefix = $course.'_'.$domain.'_';
139: #
140: my %tables =
141: ( student =>&Apache::lonmysql::fix_table_name($prefix.'students'),
142: res =>&Apache::lonmysql::fix_table_name($prefix.'resource'),
143: machine =>&Apache::lonmysql::fix_table_name($prefix.'machine_table'),
144: activity=>&Apache::lonmysql::fix_table_name($prefix.'activity'),
145: );
146: return %tables;
147: }
148:
149: sub get_max_time_in_db {
150: my ($r,$prog_state) = @_;
151: my %table = &table_names();
152: my $query = qq{SELECT MAX(time) FROM $table{'activity'} };
153: #
154: my $home = $env{'course.'.$env{'request.course.id'}.'.home'};
155: my $reply=&Apache::lonnet::metadata_query($query,undef,undef,[$home]);
156: if (ref($reply) ne 'HASH') {
157: return undef;
158: }
159: my $results_file = $r->dir_config('lonDaemons').'/tmp/'.$reply->{$home};
160: my $endfile = $results_file.'.end';
161: ##
162: ## Check for the results
163: &Apache::lonhtmlcommon::Update_PrgWin
164: ($r,$prog_state,&mt('Waiting for results'));
165: my $maxtime = 500;
166: my $starttime = time;
167: while (! -e $endfile && (time-$starttime < $maxtime)) {
168: &Apache::lonhtmlcommon::Update_PrgWin
169: ($r,$prog_state,&mt('Waiting up to [_1] seconds for results',
170: $starttime+$maxtime-time));
171: sleep(1);
172: }
173: if (! -e $endfile) {
174: $r->print('<h2>'.
175: &mt('Unable to retrieve data.').'</h2>');
176: $r->print(&mt('Please try again in a few minutes.'));
177: return undef;
178: }
179: $r->rflush();
180: #
181: &Apache::lonhtmlcommon::Update_PrgWin
182: ($r,$prog_state,&mt('Parsing results'));
183: #
184: if (! open(TIMEDATA,$results_file)) {
185: $r->print('<h2>'.&mt('Unable to read results file.').'</h2>'.
186: '<p>'.
187: &mt('This is a serious error and has been logged. '.
188: 'You should contact your system administrator '.
189: 'to resolve this issue.').
190: '</p>');
191: return;
192: }
193: #
194: my $timestr = '';
195: while (my $line = <TIMEDATA>) {
196: chomp($line);
197: $timestr = &unescape($line);
198: }
199: close(TIMEDATA);
200: return &Apache::lonmysql::unsqltime($timestr);
201: }
202:
203: sub build_query {
204: my ($mode) = @_;
205: my $cid = $env{'request.course.id'};
206: my $domain = $env{'course.'.$cid.'.domain'};
207: my $home = $env{'course.'.$cid.'.home'};
208: my $course = $env{'course.'.$cid.'.num'};
209: my $prefix = $course.'_'.$domain.'_';
210: my $start = ($env{'form.start'}+0);
211: #
212: my %table = &table_names();
213: #
214: my $query;
215: if ($mode eq 'full_class') {
216: $query = qq{
217: SELECT B.resource,A.time,C.student,A.action,E.machine,A.action_values
218: FROM $table{'activity'} AS A
219: LEFT JOIN $table{'res'} AS B ON B.res_id=A.res_id
220: LEFT JOIN $table{'student'} AS C ON C.student_id=A.student_id
221: LEFT JOIN $table{'machine'} AS E ON E.machine_id=A.machine_id
222: ORDER BY A.time DESC
223: LIMIT $start, $num_records
224: };
225: } elsif ($mode =~ /^student:(.*):(.*)$/) {
226: my $student = $1.':'.$2;
227: $query = qq{
228: SELECT B.resource,A.time,A.action,E.machine,A.action_values
229: FROM $table{'activity'} AS A
230: LEFT JOIN $table{'res'} AS B ON B.res_id=A.res_id
231: LEFT JOIN $table{'student'} AS C ON C.student_id=A.student_id
232: LEFT JOIN $table{'machine'} AS E ON E.machine_id=A.machine_id
233: WHERE C.student='$student'
234: ORDER BY A.time DESC
235: LIMIT $start, $num_records
236: };
237: }
238: $query =~ s|$/||g;
239: return $query;
240: }
241:
242: ###################################################################
243: ###################################################################
244: sub output_results {
245: my ($r,$results_file,$navmap,$mode) = @_;
246: ##
247: ##
248: if (! -s $results_file) {
249: # results file is empty, just let them know there is no data
250: $r->print('<h2>'.&mt('So far, no data has been returned for your request').'</h2>');
251: return -1;
252: }
253: if (! open(ACTIVITYDATA,$results_file)) {
254: $r->print('<h2>'.&mt('Unable to read results file.').'</h2>'.
255: '<p>'.
256: &mt('This is a serious error and has been logged. '.
257: 'You should contact your system administrator '.
258: 'to resolve this issue.').
259: '</p>');
260: return -2;
261: }
262: ##
263: ##
264: my $tableheader;
265: if ($mode eq 'full_class') {
266: $tableheader =
267: '<table><tr>'.
268: '<th>'.&mt('Resource').'</th>'.
269: '<th>'.&mt('Time').'</th>'.
270: '<th>'.&mt('Student').'</th>'.
271: '<th>'.&mt('Action').'</th>'.
272: # '<th>'.&mt('Originating Server').'</th>'.
273: '<th align="left">'.&mt('Data').'</th>'.
274: '</tr>'.$/;
275: } elsif ($mode =~ /^student:(.*):(.*)$/) {
276: $tableheader =
277: '<table><tr>'.
278: '<th>'.&mt('Resource').'</th>'.
279: '<th>'.&mt('Time').'</th>'.
280: '<th>'.&mt('Action').'</th>'.
281: # '<th>'.&mt('Originating Server').'</th>'.
282: '<th align="left">'.&mt('Data').'</th>'.
283: '</tr>'.$/;
284: }
285: my $count = $env{'form.start'}-1;
286: $r->rflush();
287: ##
288: ##
289: while (my $line = <ACTIVITYDATA>) {
290: # FIXME: does not pass symbs along :(
291: chomp($line);
292: $line = &unescape($line);
293: if (++$count % 50 == 0) {
294: if ($count != 0) {
295: $r->print('</table>'.$/);
296: $r->rflush();
297: }
298: $r->print($tableheader);
299: }
300: my ($symb,$timestamp,$student,$action,$machine,$values);
301: if ($mode eq 'full_class') {
302: ($symb,$timestamp,$student,$action,$machine,$values) = split(',',$line,6);
303: } else {
304: ($symb,$timestamp,$action,$machine,$values) = split(',',$line,5);
305: }
306: foreach ($symb,$timestamp,$student,$action,$machine) {
307: $_=&unescape($_);
308: }
309: my ($title,$src);
310: if ($symb =~ m:^/adm/:) {
311: $title = $symb;
312: $src = $symb;
313: } else {
314: my $nav_res = $navmap->getBySymb($symb);
315: if (defined($nav_res)) {
316: $title = $nav_res->compTitle();
317: $src = $nav_res->src();
318: } else {
319: if ($src =~ m|^/res|) {
320: $title = $src;
321: } elsif ($values =~ /^\s*$/ &&
322: (! defined($src) || $src =~ /^\s*$/)) {
323: next;
324: } elsif ($values =~ /^\s*$/) {
325: $values = $src;
326: } else {
327: $title = 'unable to retrieve title';
328: $src = '/dev/null';
329: }
330: }
331: }
332: my %classes;
333: my $class_count=0;
334: if (! exists($classes{$symb})) {
335: $classes{$symb} = $class_count++;
336: }
337: my $class = 'a';#.$classes{$symb};
338: #
339: if ($symb eq '/prtspool/') {
340: $class = 'print';
341: $title = 'retrieve printout';
342: } elsif ($symb =~ m|^/adm/([^/]+)|) {
343: $class = $1;
344: } elsif ($symb =~ m|^/adm/|) {
345: $class = 'adm';
346: }
347: if ($title eq 'unable to retrieve title') {
348: $title =~ s/ /\ /g;
349: $class = 'warning';
350: }
351: if (! defined($title) || $title eq '') {
352: $title = 'untitled';
353: $class = 'warning';
354: }
355: # Clean up the values
356: $values = &display_values($action,$values);
357: #
358: # Build the row for output
359: my $tablerow = qq{<tr class="$class"><td>}.($count+1).qq{</td>};
360: if ($src =~ m|^/adm/|) {
361: $tablerow .=
362: '<td valign="top"><nobr>'.$title.'</nobr></td>';
363: } else {
364: $tablerow .=
365: '<td valign="top"><nobr>'.
366: '<a href="'.$src.'">'.$title.'</a>'.
367: '</nobr></td>';
368: }
369: $tablerow .= '<td valign="top"><nobr>'.$timestamp.'</nobr></td>';
370: if ($mode eq 'full_class') {
371: $tablerow.='<td valign="top">'.$student.'</td>';
372: }
373: $tablerow .=
374: '<td valign="top">'.$action.'</td>'.
375: # '<td>'.$machine.'</td>'.
376: '<td valign="top">'.$values.'</td>'.
377: '</tr>';
378: $r->print($tablerow.$/);
379: }
380: $r->print('</table>'.$/);### if (! $count % 50);
381: close(ACTIVITYDATA);
382: return $count;
383: }
384:
385: ###################################################################
386: ###################################################################
387: sub display_values {
388: my ($action,$values)=@_;
389: my $result='<table>';
390: if ($action eq 'CSTORE') {
391: my %values=map {split('=',$_,-1)} split(/\&/,$values);
392: foreach my $key (sort(keys(%values))) {
393: $result.='<tr><td align="right">'.
394: &unescape($key).
395: '</td><td>=</td><td align="left">'.
396: &unescape($values{$key}).'</td></tr>';
397: }
398: $result.='</table>';
399: } elsif ($action eq 'POST') {
400: my %values=
401: map {split('=',&unescape($_),-1)} split(/\&/,$values);
402: foreach my $key (sort(keys(%values))) {
403: if ($key eq 'counter') { next; }
404: $result.='<tr><td align="right">'.$key.'</td>'.
405: '<td>=</td><td align="left">'.$values{$key}.'</td></tr>';
406: }
407: $result.='</table>';
408: } else {
409: $result=&unescape($values)
410: }
411: return $result;
412: }
413: ###################################################################
414: ###################################################################
415: sub request_data_update {
416: my $command = 'prepare activity log';
417: my $cid = $env{'request.course.id'};
418: my $domain = $env{'course.'.$cid.'.domain'};
419: my $home = $env{'course.'.$cid.'.home'};
420: my $course = $env{'course.'.$cid.'.num'};
421: # &Apache::lonnet::logthis($command.' '.$course.' '.$domain.' '.$home);
422: my $result = &Apache::lonnet::metadata_query($command,$course,$domain,
423: [$home]);
424: return $result;
425: }
426:
427: ###################################################################
428: ###################################################################
429: sub pick_student {
430: my ($r) = @_;
431: $r->print("Sorry, cannot display classlist at this time. Come back another time.");
432: return;
433: }
434:
435: ###################################################################
436: ###################################################################
437: sub styles {
438: return <<END;
439: <style type="text/css">
440: tr.warning { background-color: \#CCCCCC; }
441: tr.chat { background-color: \#CCCCCC; }
442: tr.chatfetch { background-color: \#CCCCCC; }
443: tr.navmaps { background-color: \#CCCCCC; }
444: tr.roles { background-color: \#CCCCCC; }
445: tr.flip { background-color: \#CCCCCC; }
446: tr.adm { background-color: \#CCCCCC; }
447: tr.print { background-color: \#CCCCCC; }
448: tr.printout { background-color: \#CCCCCC; }
449: tr.parmset { background-color: \#CCCCCC; }
450: tr.grades { background-color: \#CCCCCC; }
451: </style>
452: END
453: }
454:
455: sub developer_centric_styles {
456: return <<END;
457: <style type="text/css">
458: tr.warning { background-color: red; }
459: tr.chat { background-color: yellow; }
460: tr.chatfetch { background-color: yellow; }
461: tr.evaluate { background-color: red; }
462: tr.navmaps { background-color: \#777777; }
463: tr.roles { background-color: \#999999; }
464: tr.flip { background-color: \#BBBBBB; }
465: tr.adm { background-color: green; }
466: tr.print { background-color: blue; }
467: tr.parmset { background-color: \#000088; }
468: tr.printout { background-color: blue; }
469: tr.grades { background-color: \#CCCCCC; }
470: </style>
471: END
472: }
473:
474: ###################################################################
475: ###################################################################
476: sub handler {
477: my $r=shift;
478: my $c = $r->connection();
479: #
480: # Check for overloading here and on the course home server
481: my $loaderror=&Apache::lonnet::overloaderror($r);
482: if ($loaderror) { return $loaderror; }
483: $loaderror=
484: &Apache::lonnet::overloaderror
485: ($r,
486: $env{'course.'.$env{'request.course.id'}.'.home'});
487: if ($loaderror) { return $loaderror; }
488: #
489: # Check for access
490: if (! &Apache::lonnet::allowed('vsa',$env{'request.course.id'})) {
491: $env{'user.error.msg'}=
492: $r->uri.":vsa:0:0:Cannot student activity for complete course";
493: if (!
494: &Apache::lonnet::allowed('vsa',
495: $env{'request.course.id'}.'/'.
496: $env{'request.course.sec'})) {
497: $env{'user.error.msg'}=
498: $r->uri.":vsa:0:0:Cannot view student activity with given role";
499: return HTTP_NOT_ACCEPTABLE;
500: }
501: }
502: #
503: # Send the header
504: &Apache::loncommon::no_cache($r);
505: &Apache::loncommon::content_type($r,'text/html');
506: $r->send_http_header;
507: if ($r->header_only) { return OK; }
508: #
509: # Extract form elements from query string
510: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
511: ['selected_student','start']);
512: #
513: # We will almost always need this...
514: my $navmap = Apache::lonnavmaps::navmap->new();
515: #
516: &Apache::lonhtmlcommon::clear_breadcrumbs();
517: &Apache::lonhtmlcommon::add_breadcrumb({href=>'/adm/studentactivity',
518: title=>'Student Activity',
519: text =>'Student Activity',
520: faq=>139,
521: bug=>'instructor interface'});
522: #
523: # Give the LON-CAPA page header
524: $r->print(&Apache::loncommon::start_page('Student Activity',&styles()).
525: &Apache::lonhtmlcommon::breadcrumbs('Student Activity'));
526: $r->rflush();
527: #
528: # Begin form output
529: $r->print('<form name="trackstudent" method="post" action="/adm/trackstudent">');
530: $r->print('<br />');
531: $r->print('<div name="statusline">'.
532: &mt('Status:[_1]',
533: '<input type="text" name="status" size="60" value="" />').
534: '</div>');
535: $r->rflush();
536: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
537: ($r,&mt('Student Activity Retrieval'),
538: &mt('Student Activity Retrieval'),undef,'inline',undef,
539: 'trackstudent','status');
540: &Apache::lonhtmlcommon::Update_PrgWin
541: ($r,\%prog_state,&mt('Contacting course home server'));
542: #
543: my $result = &request_data_update();
544: #
545: if (exists($env{'form.selected_student'})) {
546: # For now, just show all the data, in the future allow selection of
547: # a student
548: my ($sname,$sdom) = split(':',$env{'form.selected_student'});
549: if ($sname =~ /^$LONCAPA::username_re$/
550: && $sdom =~ /^$LONCAPA::domain_re$/) {
551: $r->print('<h2>'.
552: &mt('Recent activity of [_1]:[_2]',$sname,$sdom).
553: '</h2>');
554: $r->print('<p>'.&mt(<<END).'</p>');
555: Compiling student activity data can take a long time.
556: Your request continues to be processed while results are displayed.
557: END
558: &get_data($r,\%prog_state,$navmap,
559: 'student:'.$env{'form.selected_student'});
560: } else {
561: $r->print('<h2>'.&mt('Unable to process for [_1]:[_2]',
562: $sname,$sdom).'</h2>');
563: }
564: } else {
565: # For now, just show all the data instead of limiting it to one student
566: &get_data($r,\%prog_state,$navmap,'full_class');
567: }
568: #
569: &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Done'));
570: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
571: #
572: $r->print("</form>\n");
573: $r->print(&Apache::loncommon::end_page());
574: $r->rflush();
575: #
576: return OK;
577: }
578:
579: 1;
580:
581: #######################################################
582: #######################################################
583:
584: =pod
585:
586: =back
587:
588: =cut
589:
590: #######################################################
591: #######################################################
592:
593: __END__
594:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>