Merge branch 'MDL-51445_m30v1' of https://github.com/sbourget/moodle into MOODLE_30_S...
[moodle.git] / mod / feedback / analysis_course.php
blobd5f4c1745f6e3025abc8f0199d18518953298591
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.
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/>.
17 /**
18 * shows an analysed view of a feedback on the mainsite
20 * @author Andreas Grabs
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22 * @package mod_feedback
25 require_once("../../config.php");
26 require_once("lib.php");
28 $current_tab = 'analysis';
30 $id = required_param('id', PARAM_INT); //the POST dominated the GET
31 $coursefilter = optional_param('coursefilter', '0', PARAM_INT);
32 $courseitemfilter = optional_param('courseitemfilter', '0', PARAM_INT);
33 $courseitemfiltertyp = optional_param('courseitemfiltertyp', '0', PARAM_ALPHANUM);
34 $searchcourse = optional_param('searchcourse', '', PARAM_RAW);
35 $courseid = optional_param('courseid', false, PARAM_INT);
37 $url = new moodle_url('/mod/feedback/analysis_course.php', array('id'=>$id));
38 if ($courseid !== false) {
39 $url->param('courseid', $courseid);
41 if ($coursefilter !== '0') {
42 $url->param('coursefilter', $coursefilter);
44 if ($courseitemfilter !== '0') {
45 $url->param('courseitemfilter', $courseitemfilter);
47 if ($courseitemfiltertyp !== '0') {
48 $url->param('courseitemfiltertyp', $courseitemfiltertyp);
50 if ($searchcourse !== '') {
51 $url->param('searchcourse', $searchcourse);
53 $PAGE->set_url($url);
55 if (($searchcourse OR $courseitemfilter OR $coursefilter) AND !confirm_sesskey()) {
56 print_error('invalidsesskey');
59 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
60 print_error('invalidcoursemodule');
63 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
64 print_error('coursemisconf');
67 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
68 print_error('invalidcoursemodule');
71 $context = context_module::instance($cm->id);
73 require_login($course, true, $cm);
75 if (!($feedback->publish_stats OR has_capability('mod/feedback:viewreports', $context))) {
76 print_error('error');
79 /// Print the page header
80 $strfeedbacks = get_string("modulenameplural", "feedback");
81 $strfeedback = get_string("modulename", "feedback");
83 $PAGE->set_heading($course->fullname);
84 $PAGE->set_title($feedback->name);
85 echo $OUTPUT->header();
87 /// print the tabs
88 require('tabs.php');
90 //print the analysed items
91 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
93 if (has_capability('mod/feedback:viewreports', $context)) {
94 //button "export to excel"
95 echo $OUTPUT->container_start('mdl-align');
96 $aurl = new moodle_url('analysis_to_excel.php',
97 array('sesskey' => sesskey(),
98 'id' => $id,
99 'coursefilter' => $coursefilter));
101 echo $OUTPUT->single_button($aurl, get_string('export_to_excel', 'feedback'));
102 echo $OUTPUT->container_end();
105 //get the groupid
106 //lstgroupid is the choosen id
107 $mygroupid = false;
108 //get completed feedbacks
109 $completedscount = feedback_get_completeds_group_count($feedback, $mygroupid, $coursefilter);
111 //show the count
112 echo '<b>'.get_string('completed_feedbacks', 'feedback').': '.$completedscount. '</b><br />';
114 // get the items of the feedback
115 $params = array('feedback' => $feedback->id, 'hasvalue' => 1);
116 $items = $DB->get_records('feedback_item', $params, 'position');
117 //show the count
118 if (is_array($items)) {
119 echo '<b>'.get_string('questions', 'feedback').': ' .count($items). ' </b><hr />';
120 echo '<a href="analysis_course.php?id=' . $id . '&courseid='.$courseid.'">';
121 echo get_string('show_all', 'feedback');
122 echo '</a>';
123 } else {
124 $items=array();
127 echo '<form name="report" method="post" id="analysis-form">';
128 echo '<div class="mdl-align"><table width="80%" cellpadding="10">';
129 if ($courseitemfilter > 0) {
130 $avgvalue = 'avg(value)';
131 if ($DB->get_dbfamily() == 'postgres') { // TODO: this should be moved to standard sql DML function ;-)
132 $avgvalue = 'avg(cast (value as integer))';
135 $sql = "SELECT fv.course_id, c.shortname, $avgvalue AS avgvalue
136 FROM {feedback_value} fv, {course} c, {feedback_item} fi
137 WHERE fv.course_id = c.id AND fi.id = fv.item AND fi.typ = ? AND fv.item = ?
138 GROUP BY course_id, shortname
139 ORDER BY avgvalue desc";
141 if ($courses = $DB->get_records_sql($sql, array($courseitemfiltertyp, $courseitemfilter))) {
142 $item = $DB->get_record('feedback_item', array('id'=>$courseitemfilter));
143 echo '<tr><th colspan="2">'.$item->name.'</th></tr>';
144 echo '<tr><td><table align="left">';
145 echo '<tr><th>Course</th><th>Average</th></tr>';
146 $sep_dec = get_string('separator_decimal', 'feedback');
147 $sep_thous = get_string('separator_thousand', 'feedback');
149 foreach ($courses as $c) {
150 $coursecontext = context_course::instance($c->course_id);
151 $shortname = format_string($c->shortname, true, array('context' => $coursecontext));
153 echo '<tr>';
154 echo '<td>'.$shortname.'</td>';
155 echo '<td align="right">';
156 echo number_format(($c->avgvalue), 2, $sep_dec, $sep_thous);
157 echo '</td>';
158 echo '</tr>';
160 echo '</table></td></tr>';
161 } else {
162 echo '<tr><td>'.get_string('noresults').'</td></tr>';
164 } else {
166 echo html_writer::label(get_string('search_course', 'feedback') . ': ', 'searchcourse');
167 echo '<input id="searchcourse" type="text" name="searchcourse" value="'.s($searchcourse).'"/> ';
168 echo '<input type="submit" value="'.get_string('search').'"/>';
169 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
170 echo '<input type="hidden" name="id" value="'.$id.'" />';
171 echo '<input type="hidden" name="courseitemfilter" value="'.$courseitemfilter.'" />';
172 echo '<input type="hidden" name="courseitemfiltertyp" value="'.$courseitemfiltertyp.'" />';
173 echo '<input type="hidden" name="courseid" value="'.$courseid.'" />';
174 echo html_writer::script('', $CFG->wwwroot.'/mod/feedback/feedback.js');
175 $sql = 'select DISTINCT c.id, c.shortname from {course} c, '.
176 '{feedback_value} fv, {feedback_item} fi '.
177 'where c.id = fv.course_id and fv.item = fi.id '.
178 'and fi.feedback = ? '.
179 'and
180 ('.$DB->sql_like('c.shortname', '?', false).'
181 OR '.$DB->sql_like('c.fullname', '?', false).')';
182 $params = array($feedback->id, "%$searchcourse%", "%$searchcourse%");
184 if ($courses = $DB->get_records_sql_menu($sql, $params)) {
186 echo ' '. html_writer::label(get_string('filter_by_course', 'feedback'), 'coursefilterid'). ': ';
187 echo html_writer::select($courses, 'coursefilter', $coursefilter,
188 null, array('id'=>'coursefilterid', 'class' => 'autosubmit'));
190 $PAGE->requires->yui_module('moodle-core-formautosubmit',
191 'M.core.init_formautosubmit',
192 array(array('selectid' => 'coursefilterid', 'nothing' => false))
195 echo '<hr />';
196 $itemnr = 0;
197 //print the items in an analysed form
198 echo '<tr><td>';
199 foreach ($items as $item) {
200 if ($item->hasvalue == 0) {
201 continue;
203 echo '<table width="100%" class="generalbox">';
204 //get the class from item-typ
205 $itemobj = feedback_get_item_class($item->typ);
206 $itemnr++;
207 if ($feedback->autonumbering) {
208 $printnr = $itemnr.'.';
209 } else {
210 $printnr = '';
212 $itemobj->print_analysed($item, $printnr, $mygroupid, $coursefilter);
213 if (preg_match('/rated$/i', $item->typ)) {
214 $onclick = 'onclick="setcourseitemfilter'.
215 "(".$item->id.",'".$item->typ."');".
216 ' return false;"';
218 $anker = '<a href="#" '.$onclick.'>'.
219 get_string('sort_by_course', 'feedback').
220 '</a>';
222 echo '<tr><td colspan="2">'.$anker.'</td></tr>';
224 echo '</table>';
226 echo '</td></tr>';
228 echo '</table></div>';
229 echo '</form>';
230 echo $OUTPUT->box_end();
232 echo $OUTPUT->footer();