1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * The gradebook outcomes report
19 *
20 * @package   gradereport_outcomes
21 * @copyright 2007 Nicolas Connault
22 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25include_once('../../../config.php');
26require_once($CFG->libdir . '/gradelib.php');
27require_once $CFG->dirroot.'/grade/lib.php';
28
29$courseid = required_param('id', PARAM_INT);                   // course id
30
31$PAGE->set_url('/grade/report/outcomes/index.php', array('id'=>$courseid));
32
33if (!$course = $DB->get_record('course', array('id' => $courseid))) {
34    print_error('invalidcourseid');
35}
36
37require_login($course);
38$context = context_course::instance($course->id);
39
40require_capability('gradereport/outcomes:view', $context);
41
42// First make sure we have proper final grades.
43grade_regrade_final_grades_if_required($course);
44
45// Grab all outcomes used in course.
46$report_info = array();
47$outcomes = grade_outcome::fetch_all_available($courseid);
48
49// Will exclude grades of suspended users if required.
50$defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
51$showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
52$showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $context);
53if ($showonlyactiveenrol) {
54    $suspendedusers = get_suspended_userids($context);
55}
56
57// Get grade_items that use each outcome.
58foreach ($outcomes as $outcomeid => $outcome) {
59    $report_info[$outcomeid]['items'] = $DB->get_records_select('grade_items', "outcomeid = ? AND courseid = ?", array($outcomeid, $courseid));
60    $report_info[$outcomeid]['outcome'] = $outcome;
61
62    // Get average grades for each item.
63    if (is_array($report_info[$outcomeid]['items'])) {
64        foreach ($report_info[$outcomeid]['items'] as $itemid => $item) {
65            $params = array();
66            $hidesuspendedsql = '';
67            if ($showonlyactiveenrol && !empty($suspendedusers)) {
68                list($notinusers, $params) = $DB->get_in_or_equal($suspendedusers, SQL_PARAMS_QM, null, false);
69                $hidesuspendedsql = ' AND userid ' . $notinusers;
70            }
71            $params = array_merge(array($itemid), $params);
72
73            $sql = "SELECT itemid, AVG(finalgrade) AS avg, COUNT(finalgrade) AS count
74                      FROM {grade_grades}
75                     WHERE itemid = ?".
76                     $hidesuspendedsql.
77                  " GROUP BY itemid";
78            $info = $DB->get_records_sql($sql, $params);
79
80            if (!$info) {
81                unset($report_info[$outcomeid]['items'][$itemid]);
82                continue;
83            } else {
84                $info = reset($info);
85                $avg = round($info->avg, 2);
86                $count = $info->count;
87            }
88
89            $report_info[$outcomeid]['items'][$itemid]->avg = $avg;
90            $report_info[$outcomeid]['items'][$itemid]->count = $count;
91        }
92    }
93}
94
95$html = '<table class="generaltable boxaligncenter" width="90%" cellspacing="1" cellpadding="5" summary="Outcomes Report">' . "\n";
96$html .= '<tr><th class="header c0" scope="col">' . get_string('outcomeshortname', 'grades') . '</th>';
97$html .= '<th class="header c1" scope="col">' . get_string('courseavg', 'grades') . '</th>';
98$html .= '<th class="header c2" scope="col">' . get_string('sitewide', 'grades') . '</th>';
99$html .= '<th class="header c3" scope="col">' . get_string('activities', 'grades') . '</th>';
100$html .= '<th class="header c4" scope="col">' . get_string('average', 'grades') . '</th>';
101$html .= '<th class="header c5" scope="col">' . get_string('numberofgrades', 'grades') . '</th></tr>' . "\n";
102
103$row = 0;
104foreach ($report_info as $outcomeid => $outcomedata) {
105    $rowspan = count($outcomedata['items']);
106    // If there are no items for this outcome, rowspan will equal 0, which is not good.
107    if ($rowspan == 0) {
108        $rowspan = 1;
109    }
110
111    $shortname_html = '<tr class="r' . $row . '"><td class="cell c0" rowspan="' . $rowspan . '">' . $outcomedata['outcome']->shortname . "</td>\n";
112
113    $sitewide = get_string('no');
114    if (empty($outcomedata['outcome']->courseid)) {
115        $sitewide = get_string('yes');
116    }
117
118    $sitewide_html = '<td class="cell c2" rowspan="' . $rowspan . '">' . $sitewide . "</td>\n";
119
120    $outcomedata['outcome']->sum = 0;
121    $scale = new grade_scale(array('id' => $outcomedata['outcome']->scaleid), false);
122
123    $print_tr = false;
124    $items_html = '';
125
126    if (!empty($outcomedata['items'])) {
127        foreach ($outcomedata['items'] as $itemid => $item) {
128            if ($print_tr) {
129                $row++;
130                $items_html .= "<tr class=\"r$row\">\n";
131            }
132
133            $grade_item = new grade_item($item, false);
134
135            if ($item->itemtype == 'mod') {
136                $cm = get_coursemodule_from_instance($item->itemmodule, $item->iteminstance, $item->courseid);
137                $itemname = '<a href="'.$CFG->wwwroot.'/mod/'.$item->itemmodule.'/view.php?id='.$cm->id.'">'.format_string($cm->name, true, $cm->course).'</a>';
138            } else {
139                $itemname = $grade_item->get_name();
140            }
141
142            $outcomedata['outcome']->sum += $item->avg;
143            $gradehtml = $scale->get_nearest_item($item->avg);
144
145            $items_html .= "<td class=\"cell c3\">$itemname</td>"
146                         . "<td class=\"cell c4\">$gradehtml ($item->avg)</td>"
147                         . "<td class=\"cell c5\">$item->count</td></tr>\n";
148            $print_tr = true;
149        }
150    } else {
151        $items_html .= "<td class=\"cell c3\"> - </td><td class=\"cell c4\"> - </td><td class=\"cell c5\"> 0 </td></tr>\n";
152    }
153
154    // Calculate outcome average.
155    if (is_array($outcomedata['items'])) {
156        $count = count($outcomedata['items']);
157        if ($count > 0) {
158            $avg = $outcomedata['outcome']->sum / $count;
159        } else {
160            $avg = $outcomedata['outcome']->sum;
161        }
162        $avg_html = $scale->get_nearest_item($avg) . " (" . round($avg, 2) . ")\n";
163    } else {
164        $avg_html = ' - ';
165    }
166
167    $outcomeavg_html = '<td class="cell c1" rowspan="' . $rowspan . '">' . $avg_html . "</td>\n";
168
169    $html .= $shortname_html . $outcomeavg_html . $sitewide_html . $items_html;
170    $row++;
171}
172
173$html .= '</table>';
174
175print_grade_page_head($courseid, 'report', 'outcomes');
176
177echo $html;
178
179$event = \gradereport_outcomes\event\grade_report_viewed::create(
180    array(
181        'context' => $context,
182        'courseid' => $courseid,
183    )
184);
185$event->trigger();
186
187echo $OUTPUT->footer();
188