MDL-11515:
[moodle-linuxchix.git] / grade / report / outcomes / index.php
blob7d19b463927266d9de9918f1e19b8db132481b83
1 <?php //$Id$
3 include_once('../../../config.php');
4 require_once($CFG->libdir . '/gradelib.php');
5 require_once $CFG->dirroot.'/grade/lib.php';
7 $courseid = required_param('id', PARAM_INT); // course id
9 if (!$course = get_record('course', 'id', $courseid)) {
10 print_error('nocourseid');
13 require_login($course->id);
14 $context = get_context_instance(CONTEXT_COURSE, $course->id);
16 require_capability('gradereport/outcomes:view', $context);
18 // Build navigation
19 $strgrades = get_string('grades');
20 $stroutcomes = get_string('outcomes', 'grades');
22 $navigation = grade_build_nav(__FILE__, $stroutcomes, $course->id);
24 /// Print header
25 print_header_simple($strgrades.':'.$stroutcomes, ':'.$strgrades, $navigation, '', '', true);
26 print_grade_plugin_selector($courseid, 'report', 'outcomes');
28 //first make sure we have proper final grades
29 grade_regrade_final_grades($courseid);
31 // Grab all outcomes used in course
32 $report_info = array();
33 $outcomes = grade_outcome::fetch_all_available($courseid);
35 // Get grade_items that use each outcome
36 foreach ($outcomes as $outcomeid => $outcome) {
37 $report_info[$outcomeid]['items'] = get_records_select('grade_items', "outcomeid = $outcomeid AND courseid = $courseid");
38 $report_info[$outcomeid]['outcome'] = $outcome;
40 // Get average grades for each item
41 if (is_array($report_info[$outcomeid]['items'])) {
42 foreach ($report_info[$outcomeid]['items'] as $itemid => $item) {
43 $sql = "SELECT id, AVG(finalgrade) AS `avg`, COUNT(finalgrade) AS `count`
44 FROM {$CFG->prefix}grade_grades
45 WHERE itemid = $itemid
46 GROUP BY itemid";
47 $info = get_records_sql($sql);
49 if (!$info) {
50 unset($report_info[$outcomeid]['items'][$itemid]);
51 continue;
52 } else {
53 $info = reset($info);
54 $avg = round($info->avg, 2);
55 $count = $info->count;
58 $report_info[$outcomeid]['items'][$itemid]->avg = $avg;
59 $report_info[$outcomeid]['items'][$itemid]->count = $count;
64 $html = '<table class="generaltable boxaligncenter" width="90%" cellspacing="1" cellpadding="5" summary="Outcomes Report">' . "\n";
65 $html .= '<tr><th class="header c0" scope="col">' . get_string('outcomename', 'grades') . '</th>';
66 $html .= '<th class="header c1" scope="col">' . get_string('overallavg', 'grades') . '</th>';
67 $html .= '<th class="header c2" scope="col">' . get_string('sitewide', 'grades') . '</th>';
68 $html .= '<th class="header c3" scope="col">' . get_string('activities', 'grades') . '</th>';
69 $html .= '<th class="header c4" scope="col">' . get_string('average', 'grades') . '</th>';
70 $html .= '<th class="header c5" scope="col">' . get_string('numberofgrades', 'grades') . '</th></tr>' . "\n";
72 $row = 0;
73 foreach ($report_info as $outcomeid => $outcomedata) {
74 $rowspan = count($outcomedata['items']);
75 $shortname_html = '<tr class="r' . $row . '"><td class="cell c0" rowspan="' . $rowspan . '">' . $outcomedata['outcome']->shortname . "</td>\n";
77 $sitewide = get_string('no');
78 if (empty($outcomedata['outcome']->courseid)) {
79 $sitewide = get_string('yes');
82 $sitewide_html = '<td class="cell c2" rowspan="' . $rowspan . '">' . $sitewide . "</td>\n";
84 $outcomedata['outcome']->sum = 0;
85 $scale = new grade_scale(array('id' => $outcomedata['outcome']->scaleid), false);
87 $print_tr = false;
88 $items_html = '';
90 if (is_array($outcomedata['items'])) {
91 foreach ($outcomedata['items'] as $itemid => $item) {
92 if ($print_tr) {
93 $row++;
94 $items_html .= "<tr class=\"r$row\">\n";
97 $grade_item = new grade_item($item, false);
99 if ($item->itemtype == 'mod') {
100 $cm = get_coursemodule_from_instance($item->itemmodule, $item->iteminstance, $item->courseid);
101 $itemname = '<a href="'.$CFG->wwwroot.'/mod/'.$item->itemmodule.'/view.php?id='.$cm->id.'">'.$grade_item->get_name().'</a>';
102 } else {
103 $itemname = $grade_item->get_name();
106 $outcomedata['outcome']->sum += $item->avg;
107 $gradehtml = $scale->get_nearest_item($item->avg);
109 $items_html .= "<td class=\"cell c3\">$itemname</td>"
110 . "<td class=\"cell c4\">$gradehtml ($item->avg)</td>"
111 . "<td class=\"cell c5\">$item->count</td></tr>\n";
112 $print_tr = true;
114 } else {
115 $items_html .= "<td class=\"cell c3\"> - </td><td class=\"cell c4\"> - </td><td class=\"cell c5\"> 0 </td></tr>\n";
118 // Calculate outcome average
119 if (is_array($outcomedata['items'])) {
120 $count = count($outcomedata['items']);
121 if ($count > 0) {
122 $avg = $outcomedata['outcome']->sum / $count;
123 } else {
124 $avg = $outcomedata['outcome']->sum;
126 $avg_html = $scale->get_nearest_item($avg) . " (" . round($avg, 2) . ")\n";
127 } else {
128 $avg_html = ' - ';
131 $outcomeavg_html = '<td class="cell c1" rowspan="' . $rowspan . '">' . $avg_html . "</td>\n";
133 $html .= $shortname_html . $outcomeavg_html . $sitewide_html . $items_html;
134 $row++;
139 $html .= '</table>';
140 print_heading($stroutcomes);
142 echo $html;
143 print_footer($course);