1: # The LearningOnline Network with CAPA
2: # Quick Student Grades Display
3: #
4: # $Id: lonquickgrades.pm,v 1.70 2011/02/20 23:38:00 www 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: package Apache::lonquickgrades;
30:
31: use strict;
32: use Apache::Constants qw(:common :http);
33: use POSIX;
34: use Apache::loncommon;
35: use Apache::lonlocal;
36: use Apache::lonnet;
37: use Apache::grades;
38:
39: sub handler {
40: my $r = shift;
41: return real_handler($r);
42: }
43:
44: sub real_handler {
45: my $r = shift;
46:
47: &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
48:
49: # Handle header-only request
50: if ($env{'browser.mathml'}) {
51: &Apache::loncommon::content_type($r,'text/xml');
52: } else {
53: &Apache::loncommon::content_type($r,'text/html');
54: }
55: if ($r->header_only) {
56: $r->send_http_header;
57: return OK;
58: }
59:
60: # Send header, don't cache this page
61: &Apache::loncommon::no_cache($r);
62: $r->send_http_header;
63:
64: my $showPoints =
65: $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'standard';
66: my $notshowSPRSlink =
67: (($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'external')
68: || ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'externalnototals'));
69: my $notshowTotals=
70: $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'externalnototals';
71: my $showCategories=
72: $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'categories';
73:
74:
75: my $title = "Grading and Statistics";#$showPoints ? "Points Display" : "Completed Problems Display";
76: my $brcrum = [{href=>"/adm/quickgrades",text => "Points Display"}];
77: $r->print(&Apache::loncommon::start_page($title,undef,
78: {'bread_crumbs' => $brcrum})
79: );
80:
81: &startGradeScreen($r,'quick');
82:
83: $r->rflush();
84:
85: # my $uname='korte';
86: # my $udom='gerd';
87:
88: my $uname;
89: my $udom;
90:
91: my ($navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=
92: &getData($showPoints,$uname,$udom);
93:
94: if ($showCategories) {
95: &outputCategories($r,$showPoints,$notshowTotals,
96: $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted);
97: } else {
98: &outputTable($r,$showPoints,$notshowTotals,
99: $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted);
100: }
101: &endGradeScreen($r);
102: return OK;
103:
104: }
105:
106: sub startGradeScreen {
107: my ($r,$mode)=@_;
108:
109: my $showPoints =
110: $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'standard';
111: my $notshowSPRSlink =
112: (($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'external')
113: || ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'externalnototals')
114: || ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'categories'));
115: my $notshowTotals=
116: $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'externalnototals';
117: my $showCategories=
118: $env{'course.'.$env{'request.course.id'}.'.grading'} eq 'categories';
119:
120: my $allowed_to_view = &Apache::lonnet::allowed('vgr',$env{'request.course.id'});
121: my $allowed_to_edit = &Apache::lonnet::allowed('mgr',$env{'request.course.id'});
122:
123: if ($allowed_to_view) {
124: my @notes;
125: push(@notes,&mt('Students do not see total points.')) if ($notshowTotals);
126: push(@notes,&mt('Students do not see link to spreadsheet.')) if ($notshowSPRSlink);
127: push(@notes,&mt('Students will see points based on problem weights.')) if ($showPoints);
128: push(@notes,&mt('Students will see points based on categories.')) if ($showCategories);
129: push(@notes, &Apache::lonhtmlcommon::coursepreflink(&mt('Grade display settings'),'grading'));
130: $r->print(&Apache::loncommon::head_subbox(join(' ',@notes)));
131: }
132:
133:
134: $r->print("\n".'<ul class="LC_TabContentBigger" id="main">');
135: $r->print("\n".'<li'.($mode eq 'quick'?' class="active"':'').'><a href="/adm/quickgrades"><b> '.
136: ($showPoints?&mt('Individual Points Overview'):($showCategories?&mt('Grades Overview'):&mt('Completion Overview'))).
137: ' </b></a></li>');
138:
139: if (!($showPoints || $notshowSPRSlink) || ($allowed_to_view)) {
140: $r->print("\n".'<li'.($mode eq 'spreadsheet'?' class="active"':'').'><a href="/adm/'.($allowed_to_view?'classcalc':'studentcalc').'"><b>'.
141: &mt('Spreadsheet (Detailed)').'</b></a></li>');
142: }
143: if ($allowed_to_view) {
144: $r->print("\n".'<li'.($mode eq 'statistics'?' class="active"':'').'><a href="/adm/statistics"><b>'.
145: &mt('Statistics and Reports').'</b></a></li>');
146:
147: $r->print("\n".'<li'.($mode eq 'chart'?' class="active"':'').'><a href="/adm/statistics?reportSelected=student_assessment"><b>'.
148: &mt('Assessment Overview Chart').'</b></a></li>');
149:
150: }
151: if ($allowed_to_edit) {
152: $r->print("\n".'<li'.($mode eq 'grading'?' class="active"':'').'><a href="/adm/grades"><b> '.
153: &mt('Content Grading').' </b></a></li>');
154: if ($env{'form.symb'}) {
155: $r->print("\n".'<li'.($mode eq 'probgrading'?' class="active"':'').'><a href="/adm/grades?symb='.
156: &Apache::lonhtmlcommon::entity_encode($env{'form.symb'}).
157: '&command=gradingmenu"><b> '.
158: &mt('Problem Grading').' </b></a></li>');
159:
160: }
161: }
162: $r->print("\n".'</ul>'."\n");
163: $r->print('<div class="LC_Box" style="clear:both;margin:0;"><div id="maincoursedoc" style="margin:0 0;padding:0 0;"><div class="LC_ContentBox" id="mainCourseDocuments" style="display: block;">');
164: }
165:
166: sub endGradeScreen {
167: my ($r)=@_;
168: $r->print('</div></div></div>');
169: }
170:
171:
172: sub getData {
173:
174: my ($showPoints,$uname,$udom)=@_;
175:
176: # Create the nav map
177: my $navmap = Apache::lonnavmaps::navmap->new($uname,$udom);
178:
179: my $res = $navmap->firstResource(); # temp resource to access constants
180:
181: my $iterator = $navmap->getIterator(undef, undef, undef, 1);
182: my $depth = 1;
183: $iterator->next(); # ignore first BEGIN_MAP
184: my $curRes = $iterator->next();
185:
186: # General overview of the following: Walk along the course resources.
187: # For every problem in the resource, tell its parent maps how many
188: # parts and how many parts correct it has. After that, each map will
189: # have a count of the total parts underneath it, correct and otherwise.
190: # After that, we will walk through the course again and read off
191: # maps in order, with their data.
192: # (If in the future people decide not to be cumulative, only add
193: # the counts to the parent map.)
194: # For convenience, "totalParts" is also "totalPoints" when we're looking
195: # at points; I can't come up with a variable name that makes sense
196: # equally for both cases.
197:
198: my $totalParts = 0; my $totalPossible = 0; my $totalRight = 0;
199: my $totalAttempted = 0;
200: my $now = time();
201: my $topLevelParts = 0; my $topLevelRight = 0; my $topLevelAttempted = 0;
202:
203: # Pre-run: Count parts correct
204: while ( $depth > 0 ) {
205: if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
206: if ($curRes == $iterator->END_MAP()) { $depth--; }
207:
208: if (ref($curRes) && $curRes->is_problem() && !$curRes->randomout)
209: {
210: # Get number of correct, incorrect parts
211: my $parts = $curRes->parts();
212: my $partsRight = 0;
213: my $partsCount = 0;
214: my $partsAttempted = 0;
215: my $stack = $iterator->getStack();
216:
217: for my $part (@{$parts}) {
218: my $completionStatus = $curRes->getCompletionStatus($part);
219: my $dateStatus = $curRes->getDateStatus($part);
220:
221: if ($completionStatus == $curRes->EXCUSED()) {
222: next;
223: }
224: if ($showPoints) {
225: my $score = 0;
226: # If we're not telling status and the answer date isn't passed yet,
227: # it's an "attempted" point
228: if ((($curRes->problemstatus($part) eq 'no') ||
229: ($curRes->problemstatus($part) eq 'no_feedback_ever')) &&
230: ($dateStatus != $curRes->ANSWER_OPEN)) {
231: my $status = $curRes->simpleStatus($part);
232: if ($status == $curRes->ATTEMPTED) {
233: $partsAttempted += $curRes->weight($part);
234: $totalAttempted += $partsAttempted;
235: }
236: } else {
237: $score = &Apache::grades::compute_points($curRes->weight($part), $curRes->awarded($part));
238: }
239: $partsRight += $score;
240: $totalRight += $score;
241: $partsCount += $curRes->weight($part);
242:
243: if ($curRes->opendate($part) < $now) {
244: $totalPossible += $curRes->weight($part);
245: }
246: $totalParts += $curRes->weight($part);
247: } else {
248: my $status = $curRes->simpleStatus($part);
249: my $thisright = 0;
250: $partsCount++;
251: if ($status == $curRes->CORRECT ||
252: $status == $curRes->PARTIALLY_CORRECT ) {
253: $partsRight++;
254: $totalRight++;
255: $thisright = 1;
256: }
257:
258: if ($status == $curRes->ATTEMPTED) {
259: $partsAttempted++;
260: $totalAttempted++;
261: }
262:
263: my $dateStatus = $curRes->getDateStatus($part);
264: $totalParts++;
265: if ($curRes->opendate($part) < $now) {
266: $totalPossible++;
267: }
268: }
269: }
270:
271: if ($depth == 1) { # in top-level only
272: $topLevelParts += $partsCount;
273: $topLevelRight += $partsRight;
274: $topLevelAttempted += $partsAttempted;
275: }
276:
277: # Crawl down stack and record parts correct and total
278: for my $res (@{$stack}) {
279: if (ref($res) && $res->is_map()) {
280: if (!defined($res->{DATA}->{CHILD_PARTS})) {
281: $res->{DATA}->{CHILD_PARTS} = 0;
282: $res->{DATA}->{CHILD_CORRECT} = 0;
283: $res->{DATA}->{CHILD_ATTEMPTED} = 0;
284: }
285:
286: $res->{DATA}->{CHILD_PARTS} += $partsCount;
287: $res->{DATA}->{CHILD_CORRECT} += $partsRight;
288: $res->{DATA}->{CHILD_ATTEMPTED} += $partsAttempted;
289: }
290: }
291: }
292: $curRes = $iterator->next();
293: }
294: return ($navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted);
295: }
296:
297: #
298: # Outputting everything.
299: #
300:
301: sub outputTable {
302:
303: my ($r,$showPoints,$notshowTotals,
304: $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=@_;
305:
306: my @start = (255, 255, 192);
307: my @end = (0, 192, 0);
308:
309: my $indentString = ' ';
310:
311: # Second pass: Print the maps.
312: $r->print(&Apache::loncommon::start_data_table()
313: .&Apache::loncommon::start_data_table_header_row()
314: .'<th>'.&mt('Folder').'</th>');
315: my $title = &mt($showPoints ? "Points Scored" : "Done");
316: if ($totalAttempted) {
317: $title .= " / " . &mt("Attempted");
318: }
319: $r->print("<th>$title".($notshowTotals?'':" / ".&mt('Total')).'</th>'
320: .&Apache::loncommon::end_data_table_header_row());
321: #
322: # Output of folder scores
323: #
324:
325: my $iterator = $navmap->getIterator(undef, undef, undef, 1);
326: my $depth = 1;
327: $iterator->next(); # ignore first BEGIN_MAP
328: my $curRes = $iterator->next();
329:
330: while ($depth > 0) {
331: if ($curRes == $iterator->BEGIN_MAP()) {$depth++;}
332: if ($curRes == $iterator->END_MAP()) { $depth--; }
333:
334: if (ref($curRes) && $curRes->is_map()) {
335: my $title = $curRes->compTitle();
336:
337: my $correct = $curRes->{DATA}->{CHILD_CORRECT};
338: my $total = $curRes->{DATA}->{CHILD_PARTS};
339: my $attempted = $curRes->{DATA}->{CHILD_ATTEMPTED};
340:
341: if ($total > 0) {
342: my $ratio;
343: $ratio = $correct / $total;
344: my $color = &mixColors(\@start, \@end, $ratio);
345: $r->print(&Apache::loncommon::start_data_table_row()
346: .'<td style="background-color:'.$color.';">');
347:
348: my $thisIndent = '';
349: for (my $i = 1; $i < $depth; $i++) { $thisIndent .= $indentString; }
350:
351: $r->print("$thisIndent$title</td>");
352: if ($totalAttempted) {
353: $r->print('<td valign="top">'
354: .$thisIndent
355: .'<span class="LC_nobreak">'
356: .$correct.' / '.$attempted.($notshowTotals?'':' / '.$total)
357: .'</span></td>'
358: .&Apache::loncommon::end_data_table_row()
359: );
360: } else {
361: $r->print('<td valign="top">'
362: .$thisIndent
363: .'<span class="LC_nobreak">'
364: .$correct.($notshowTotals?'':' / '.$total)
365: .'</span></td>'
366: .&Apache::loncommon::end_data_table_row());
367: }
368: }
369: }
370:
371: $curRes = $iterator->next();
372: }
373:
374: # If there were any problems at the top level, print an extra "catchall"
375: if ($topLevelParts > 0) {
376: my $ratio = $topLevelRight / $topLevelParts;
377: my $color = &mixColors(\@start, \@end, $ratio);
378: $r->print(&Apache::loncommon::start_data_table_row()
379: .'<td style="background-color:'.$color.';">');
380: $r->print(&mt("Problems Not Contained In A Folder")."</td><td>");
381: $r->print("$topLevelRight / $topLevelParts</td>"
382: .&Apache::loncommon::end_data_table_row());
383: }
384:
385: #
386: # show totals (if applicable), close table
387: #
388: if ($showPoints) {
389: my $maxHelpLink = &Apache::loncommon::help_open_topic("Quick_Grades_Possibly_Correct");
390:
391: $title = $showPoints ? "Points" : "Parts Done";
392: my $totaltitle = $showPoints ? &mt("Awarded Total Points") : &mt("Total Parts Done");
393: $r->print(&Apache::loncommon::start_data_table_row()
394: .'<td colspan="2" align="right">'.$totaltitle.': <b>'.$totalRight.'</b><br />');
395: $r->print(&mt('Max Possible To Date')." $maxHelpLink: <b>$totalPossible</b><br />");
396: $title = $showPoints ? "Points" : "Parts";
397: $r->print(&mt("Total $title In Course").': <b>'.$totalParts.'</b></td>'
398: .&Apache::loncommon::end_data_table_row());
399: }
400:
401: $r->print(&Apache::loncommon::end_data_table()
402: .&Apache::loncommon::end_page());
403:
404: }
405:
406: #
407: # === Outputting category-based grades.
408: #
409: # $category{'order'}: output order of categories by id
410: # $category{'all'}: complete list of all categories
411: # $category{$id.'_name'}: display-name of category
412: #
413:
414: sub outputCategories {
415:
416: my ($r,$showPoints,$notshowTotals,
417: $navmap,$totalParts,$totalPossible,$totalRight,$totalAttempted,$topLevelParts,$topLevelRight,$topLevelAttempted)=@_;
418: # Take care of storing and retrieving categories
419:
420: my $cangrade=&Apache::lonnet::allowed('mgr');
421:
422: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
423: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
424: my %categories=();
425: # Loading old categories
426: %categories=&Apache::lonnet::dump('grading_categories',$cdom,$cnum);
427: # Storing
428: if (($cangrade) && (($env{'form.storechanges'}) || ($env{'form.storemove'} ne ''))) {
429: # Process the changes
430: %categories=&process_category_edits($r,$cangrade,%categories);
431: # Actually store
432: &Apache::lonnet::logthis("Storing ".$categories{'order'});
433: &Apache::lonnet::put('grading_categories',\%categories,$cdom,$cnum);
434: }
435: # new categories loaded now
436: # Form only generated if user can change the grading categories
437: if ($cangrade) {
438: $r->print('<form method="post">');
439: }
440: #
441: &output_category_table($r,$cangrade,$navmap,%categories);
442: #
443: if ($cangrade) {
444: $r->print('<input type="hidden" name="storemove" value="" />'.
445: '<input type="submit" name="storechanges" value="'.&mt("Save changes to grading categories").'" /></form>');
446: }
447: }
448:
449: #
450: # Process editing commands, update category hash
451: #
452:
453: sub process_category_edits {
454: my ($r,$cangrade,%categories)=@_;
455: unless ($cangrade) { return %categories; }
456: my $cmd=$env{'form.cmd'};
457: if ($cmd eq 'createnewcat') {
458: %categories=&make_new_category($r,$cangrade,undef,%categories);
459: } elsif ($cmd=~/^delcat\_(.+)$/) {
460: %categories=&del_category($1,$cangrade,%categories);
461: } else {
462: # Simply store the rest of the stuff
463: foreach my $id (split(/\,/,$categories{'order'})) {
464: %categories=&set_category_name($cangrade,$id,$env{'form.name_'.$id},%categories);
465: %categories=&set_category_weight($cangrade,$id,$env{'form.weighttype_'.$id},$env{'form.weight_'.$id},%categories);
466: # More changes here
467: }
468: }
469: return %categories;
470: }
471:
472: #
473: # Output the table
474: #
475:
476: sub output_category_table {
477: my ($r,$cangrade,$navmaps,%categories)=@_;
478: my $sum=0;
479: my $total=0;
480: $r->print(&Apache::loncommon::start_data_table());
481: #
482: &output_category_table_header($r,$cangrade);
483: #
484: my @order=split(/\,/,$categories{'order'});
485: #
486: my $maxpos=$#order;
487: for (my $i=0;$i<=$maxpos;$i++) {
488: my ($value,$weight)=&output_and_calc_category($r,$cangrade,$navmaps,$order[$i],$i,$maxpos,%categories);
489: $sum+=$value*$weight;
490: $total+=$weight;
491: }
492: #
493: &bottom_line_category($r,$cangrade,$sum,$total);
494: #
495: $r->print(&Apache::loncommon::end_data_table());
496: return $sum;
497: }
498:
499: sub output_category_table_header {
500: my ($r,$cangrade)=@_;
501: $r->print(&Apache::loncommon::start_data_table_header_row());
502: if ($cangrade) {
503: $r->print('<th colspan="2">'.&mt("Move").'</th><th>'.&mt('Action').'</th>');
504: }
505: $r->print('<th>'.&mt('Category').'</th>'.
506: '<th>'.&mt('Contents').'</th>'.
507: '<th>'.&mt('Calculation').'</th>'.
508: '<th>'.&mt('Weight').'</th>'.
509: '<th>'.&mt('Percent Overall').'</th>');
510: $r->print(&Apache::loncommon::end_data_table_header_row());
511: }
512:
513:
514: #
515: # Output one category to table
516: #
517:
518: sub output_and_calc_category {
519: my ($r,$cangrade,$navmaps,$id,$currentpos,$maxpos,%categories)=@_;
520: my $value=0;
521: my $weight=0;
522: my $iconpath = &Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL') . "/");
523: my %lt=&Apache::lonlocal::texthash(
524: 'up' => 'Move Up',
525: 'dw' => 'Move Down');
526: $r->print("\n".&Apache::loncommon::start_data_table_row());
527:
528: if ($cangrade) {
529: $r->print(<<ENDMOVE);
530: <td>
531: <div class="LC_docs_entry_move">
532: <a href='/adm/quickgrades?cmd=up_$id&storechanges=1'>
533: <img src="${iconpath}move_up.gif" alt='$lt{'up'}' class="LC_icon" />
534: </a>
535: </div>
536: <div class="LC_docs_entry_move">
537: <a href='/adm/quickgrades?cmd=down_$id&storechanges=1'>
538: <img src="${iconpath}move_down.gif" alt='$lt{'dw'}' class="LC_icon" />
539: </a>
540: </div>
541: </td>
542: ENDMOVE
543: $r->print("\n<td>\n<select name='newpos_$id' onchange='this.form.storemove.value=\"$id\";this.form.submit()'>");
544: for (my $i=0;$i<=$maxpos;$i++) {
545: if ($i==$currentpos) {
546: $r->print('<option value="" selected="selected">('.$i.')</option>');
547: } else {
548: $r->print('<option value="'.$i.'">'.$i.'</option>');
549: }
550: }
551: $r->print("\n</select>\n</td>\n");
552: $r->print('<td><a href="/adm/quickgrades?cmd=delcat_'.$id.'&storechanges=1">'.&mt('Delete').'</a></td>');
553: $r->print('<td><input type="text" name="name_'.$id.
554: '" value="'.&Apache::lonhtmlcommon::entity_encode($categories{$id.'_name'}).'" /></td>');
555: } else {
556: $r->print('<td>'.$categories{$id.'_name'}.'</td>');
557: }
558: # Content
559: # FIXME: just placeholders
560: if ($cangrade) {
561: $r->print("<td>Content Edit</td>");
562: } else {
563: $r->print("<td>Content</td>");
564: }
565: # Calculation
566: # FIXME: just placeholders
567: if ($cangrade) {
568: $r->print("<td>Calculation Edit</td>");
569: } else {
570: $r->print("<td>Calculation</td>");
571: }
572: # Weight
573: if ($cangrade) {
574: $r->print('<td>'.
575: '<select name="weighttype_'.$id.'">'.
576: '<option value="default"'.($categories{$id.'_weighttype'} eq 'default'?' selected="selected"':'').'>'.&mt('default').'</option>'.
577: '<option value="typein"'.($categories{$id.'_weighttype'} eq 'typein'?' selected="selected"':'').'>'.&mt('Type-in value').'</option>'.
578: '</select>'.
579: '<input type="text" size="4" name="weight_'.$id.
580: '" value="'.&Apache::lonhtmlcommon::entity_encode($categories{$id.'_weight'}).'" /></td>');
581: } else {
582: $r->print('<td>'.($categories{$id.'_weighttype'} eq 'default'?&mt('default'):$categories{$id.'_weight'}).'</td>');
583: }
584:
585: return ($value,$weight);
586: }
587:
588: #
589: # Bottom line with grades
590: #
591:
592: sub bottom_line_category {
593: my ($r,$cangrade,$sum,$total)=@_;
594: $r->print(&Apache::loncommon::start_data_table_row());
595: if ($cangrade) {
596: $r->print('<td colspan="3"><a href="/adm/quickgrades?cmd=createnewcat&storechanges=1">'.&mt('Create New Category').'</a></td>');
597: }
598: $r->print('<td colspan="5">'.&mt('Current:').$sum.'<br />'.&mt('Total:').$total.'<br /></td>');
599: }
600:
601: #
602: # Make one new category
603: #
604:
605: sub make_new_category {
606: my ($r,$cangrade,$ordernum,%categories)=@_;
607: unless ($cangrade) { return %categories; }
608: # Generate new ID
609: my $id=time.'_'.$$.'_'.rand(10000);
610: # Add new ID to list of all IDs ever created in this course
611: $categories{'all'}.=','.$id;
612: $categories{'all'}=~s/^\,//;
613: # Add new ID to ordered list of displayed and evaluated categories
614: $categories{'order'}.=','.$id;
615: $categories{'order'}=~s/^\,//;
616: # Move it into desired space
617: if (defined($ordernum)) {
618: %categories=&move_category($id,$cangrade,$ordernum,%categories);
619: }
620: $categories{$id.'_weighttype'}='default';
621: return %categories;
622: }
623:
624: #
625: # Delete category
626: #
627:
628: sub del_category {
629: my ($id,$cangrade,%categories)=@_;
630: my @neworder=();
631: foreach my $currentid (split(/\,/,$categories{'order'})) {
632: unless ($currentid eq $id) {
633: push(@neworder,$currentid);
634: }
635: }
636: $categories{'order'}=join(',',@neworder);
637: return %categories;
638: }
639:
640: #
641: # Move a category to a desired position n the display order
642: #
643:
644: sub move_category {
645: my ($id,$cangrade,$ordernum,%categories)=@_;
646: unless ($cangrade) { return %categories; }
647: my @order=split(/\,/,$categories{'order'});
648: # Where is the index currently?
649: my $currentpos=¤t_pos_category($id,%categories);
650: if (defined($currentpos)) {
651: if ($currentpos<$ordernum) {
652: # This is moving to a higher index
653: # ....X1234....
654: # ....1234X....
655: for (my $i=$currentpos;$i<$ordernum;$i++) {
656: $order[$i]=$order[$i+1];
657: }
658: $order[$ordernum]=$id;
659: }
660: if ($currentpos>$ordernum) {
661: # This is moving to a lower index
662: # ....1234X....
663: # ....X1234....
664: for (my $i=$currentpos;$i>$ordernum;$i--) {
665: $order[$i]=$order[$i-1];
666: }
667: $order[$ordernum]=$id;
668: }
669: }
670: $categories{'order'}=join(',',@order);
671: return %categories;
672: }
673:
674: #
675: # Find current postion of a category in the order
676: #
677:
678: sub current_pos_category {
679: my ($id,%categories)=@_;
680: my @order=split(/\,/,$categories{'order'});
681: for (my $i=0;$i<=$#order;$i++) {
682: if ($order[$i] eq $id) { return $i; }
683: }
684: # not found
685: return undef;
686: }
687:
688: #
689: # Set name of a category
690: #
691: sub set_category_name {
692: my ($cangrade,$id,$name,%categories)=@_;
693: unless ($cangrade) { return %categories; }
694: $categories{$id.'_name'}=$name;
695: return %categories;
696: }
697:
698: #
699: # Set weight of a category
700: #
701: sub set_category_weight {
702: my ($cangrade,$id,$weighttype,$weight,%categories)=@_;
703: unless ($cangrade) { return %categories; }
704: if (($categories{$id.'_weight'} eq '') && ($weight=~/\d/)) {
705: $weighttype='typein';
706: }
707: $categories{$id.'_weighttype'}=$weighttype;
708: if ($weighttype eq 'default') {
709: $categories{$id.'_weight'}='';
710: } else {
711: $weight=~s/\D//gs;
712: unless ($weight) { $weight=0; }
713: $categories{$id.'_weight'}=$weight;
714: }
715: return %categories;
716: }
717:
718:
719: #
720: # === end category-related
721: #
722: #
723: # Pass this two refs to arrays for the start and end color, and a number
724: # from 0 to 1 for how much of the latter you want to mix in. It will
725: # return a string ready to show ("#FFC309");
726:
727: sub mixColors {
728: my $start = shift;
729: my $end = shift;
730: my $ratio = shift;
731:
732: my ($a,$b);
733: my $final = "";
734: $a = $start->[0]; $b = $end->[0];
735: my $mix1 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
736: $a = $start->[1]; $b = $end->[1];
737: my $mix2 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
738: $a = $start->[2]; $b = $end->[2];
739: my $mix3 = POSIX::floor((1-$ratio)*$a + $ratio*$b);
740:
741: $final = sprintf "%02x%02x%02x", $mix1, $mix2, $mix3;
742: return "#" . $final;
743: }
744:
745: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>