insert calls to require_capability()
[moodle.git] / mod / feedback / analysis_course.php
CommitLineData
84973212 1<?php
2
c70ad9f7 3/**
4* shows an analysed view of a feedback on the mainsite
5*
c70ad9f7 6* @author Andreas Grabs
7* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8* @package feedback
9*/
10
84973212 11require_once("../../config.php");
12require_once("lib.php");
13
14// $SESSION->feedback->current_tab = 'analysis';
15$current_tab = 'analysis';
16
17$id = required_param('id', PARAM_INT); //the POST dominated the GET
18$coursefilter = optional_param('coursefilter', '0', PARAM_INT);
19$courseitemfilter = optional_param('courseitemfilter', '0', PARAM_INT);
20$courseitemfiltertyp = optional_param('courseitemfiltertyp', '0', PARAM_ALPHANUM);
21// $searchcourse = optional_param('searchcourse', '', PARAM_ALPHAEXT);
22$searchcourse = optional_param('searchcourse', '', PARAM_RAW);
23$courseid = optional_param('courseid', false, PARAM_INT);
24
a6855934 25$url = new moodle_url('/mod/feedback/analysis_course.php', array('id'=>$id));
84973212 26if ($courseid !== false) {
27 $url->param('courseid', $courseid);
28}
29if ($coursefilter !== '0') {
30 $url->param('coursefilter', $coursefilter);
31}
32if ($courseitemfilter !== '0') {
33 $url->param('courseitemfilter', $courseitemfilter);
34}
35if ($courseitemfiltertyp !== '0') {
36 $url->param('courseitemfiltertyp', $courseitemfiltertyp);
37}
38if ($searchcourse !== '') {
39 $url->param('searchcourse', $searchcourse);
40}
41$PAGE->set_url($url);
42
43if(($searchcourse OR $courseitemfilter OR $coursefilter) AND !confirm_sesskey()) {
44 print_error('invalidsesskey');
45}
46
47if ($id) {
48 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
49 print_error('invalidcoursemodule');
c70ad9f7 50 }
d4b1d58c 51
84973212 52 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
53 print_error('coursemisconf');
c70ad9f7 54 }
c70ad9f7 55
84973212 56 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
57 print_error('invalidcoursemodule');
c70ad9f7 58 }
84973212 59}
c987dbad
AG
60
61if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
62 print_error('badcontext');
63}
84973212 64
65require_login($course->id, true, $cm);
66
c987dbad 67if( !( (intval($feedback->publish_stats) == 1) OR has_capability('mod/feedback:viewreports', $context))) {
84973212 68 print_error('error');
69}
70
71/// Print the page header
72$strfeedbacks = get_string("modulenameplural", "feedback");
73$strfeedback = get_string("modulename", "feedback");
74
a6855934 75$PAGE->navbar->add($strfeedbacks, new moodle_url('/mod/feedback/index.php', array('id'=>$course->id)));
84973212 76$PAGE->navbar->add(format_string($feedback->name));
77
78$PAGE->set_title(format_string($feedback->name));
84973212 79echo $OUTPUT->header();
80
81/// print the tabs
82include('tabs.php');
83
84//print the analysed items
85echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
86
c987dbad 87if( has_capability('mod/feedback:viewreports', $context) ) {
84973212 88 //button "export to excel"
89 echo $OUTPUT->container_start('mdl-align');
5c2ed7e2
PS
90 $aurl = new moodle_url('analysis_to_excel.php', array('sesskey'=>sesskey(), 'id'=>$id, 'coursefilter'=>$coursefilter));
91 echo $OUTPUT->single_button($aurl, get_string('export_to_excel', 'feedback'));
84973212 92 echo $OUTPUT->container_end();
93}
94
95//get the groupid
96//lstgroupid is the choosen id
97$mygroupid = false;
98//get completed feedbacks
99$completedscount = feedback_get_completeds_group_count($feedback, $mygroupid, $coursefilter);
100
101//show the count
102echo '<b>'.get_string('completed_feedbacks', 'feedback').': '.$completedscount. '</b><br />';
103
104// get the items of the feedback
105$items = $DB->get_records('feedback_item', array('feedback'=>$feedback->id, 'hasvalue'=>1), 'position');
106//show the count
107if(is_array($items)){
108 echo '<b>'.get_string('questions', 'feedback').': ' .sizeof($items). ' </b><hr />';
109 echo '<a href="analysis_course.php?id=' . $id . '&courseid='.$courseid.'">'.get_string('show_all', 'feedback').'</a>';
110} else {
111 $items=array();
112}
113
114echo '<form name="report" method="post" id="analysis-form">';
115echo '<div class="mdl-align"><table width="80%" cellpadding="10">';
116if ($courseitemfilter > 0) {
117 $avgvalue = 'avg(value)';
118 if ($DB->get_dbfamily() == 'postgres') { // TODO: this should be moved to standard sql DML function ;-)
119 $avgvalue = 'avg(cast (value as integer))';
c70ad9f7 120 }
84973212 121 if ($courses = $DB->get_records_sql ("SELECT fv.course_id, c.shortname, $avgvalue AS avgvalue
122 FROM {feedback_value} fv, {course} c, {feedback_item} fi
123 WHERE fv.course_id = c.id AND fi.id = fv.item AND fi.typ = ? AND fv.item = ?
124 GROUP BY course_id, shortname
125 ORDER BY avgvalue desc",
126 array($courseitemfiltertyp, $courseitemfilter))) {
127 $item = $DB->get_record('feedback_item', array('id'=>$courseitemfilter));
128 echo '<tr><th colspan="2">'.$item->name.'</th></tr>';
129 echo '<tr><td><table align="left">';
130 echo '<tr><th>Course</th><th>Average</th></tr>';
131 $sep_dec = get_string('separator_decimal', 'feedback');
132 $sep_thous = get_string('separator_thousand', 'feedback');
133
134 foreach ($courses as $c) {
135 echo '<tr><td>'.$c->shortname.'</td><td align="right">'.number_format(($c->avgvalue), 2, $sep_dec, $sep_thous).'</td></tr>';
136 }
137 echo '</table></td></tr>';
c70ad9f7 138 } else {
84973212 139 echo '<tr><td>'.get_string('noresults').'</td></tr>';
c70ad9f7 140 }
84973212 141} else {
142
143 echo get_string('search_course', 'feedback') . ': ';
144 echo '<input type="text" name="searchcourse" value="'.s($searchcourse).'"/> <input type="submit" value="'.get_string('search').'"/>';
145 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
146 echo '<input type="hidden" name="id" value="'.$id.'" />';
147 echo '<input type="hidden" name="courseitemfilter" value="'.$courseitemfilter.'" />';
148 echo '<input type="hidden" name="courseitemfiltertyp" value="'.$courseitemfiltertyp.'" />';
149 echo '<input type="hidden" name="courseid" value="'.$courseid.'" />';
c17f05fb 150 echo html_writer::script('', $CFG->wwwroot.'/mod/feedback/feedback.js');
84973212 151 $sql = 'select c.id, c.shortname from {course} c, '.
152 '{feedback_value} fv, {feedback_item} fi '.
153 'where c.id = fv.course_id and fv.item = fi.id '.
154 'and fi.feedback = ?'.
155 'and
156 (c.shortname '.$DB->sql_ilike().' ?
157 OR c.fullname '.$DB->sql_ilike().' ?)';
158 $params = array($feedback->id, "%$searchcourse%", "%$searchcourse%");
159
160 if ($courses = $DB->get_records_sql_menu($sql, $params)) {
161
162 echo ' ' . get_string('filter_by_course', 'feedback') . ': ';
edc28287 163
18300e6b 164 echo html_writer::select($courses, 'coursefilter', $coursefilter, null, array('id'=>'coursefilterid'));
edc28287 165 $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('analysis-form', 'coursefilterid', false));
84973212 166 }
167 echo '<hr />';
168 $itemnr = 0;
169 //print the items in an analysed form
170 echo '<tr><td>';
171 foreach($items as $item) {
172 if($item->hasvalue == 0) continue;
173 echo '<table width="100%" class="generalbox">';
174 //get the class from item-typ
175 $itemclass = 'feedback_item_'.$item->typ;
176 //get the instance of the item-class
177 $itemobj = new $itemclass();
178 $itemnr++;
179 if($feedback->autonumbering) {
180 $printnr = $itemnr.'.';
c70ad9f7 181 } else {
84973212 182 $printnr = '';
c70ad9f7 183 }
84973212 184 $itemobj->print_analysed($item, $printnr, $mygroupid, $coursefilter);
185 if (preg_match('/rated$/i', $item->typ)) {
186 echo '<tr><td colspan="2"><a href="#" onclick="setcourseitemfilter('.$item->id.',\''.$item->typ.'\'); return false;">'.
187 get_string('sort_by_course', 'feedback').'</a></td></tr>';
c70ad9f7 188 }
84973212 189 echo '</table>';
c70ad9f7 190 }
84973212 191 echo '</td></tr>';
192}
193echo '</table></div>';
194echo '</form>';
195echo $OUTPUT->box_end();
d4b1d58c 196
84973212 197echo $OUTPUT->footer();
c70ad9f7 198