Fixing a couple of typos introduced likely at the time of dml conversion.
[moodle.git] / mod / quiz / report / overview / overview_table.php
CommitLineData
c35f3afc 1<?php // $Id$
2
3class quiz_report_overview_table extends table_sql {
9cf4a18b 4
c35f3afc 5 var $useridfield = 'userid';
9cf4a18b 6
c35f3afc 7 var $candelete;
8 var $reporturl;
9 var $displayoptions;
9cf4a18b 10
c35f3afc 11 function quiz_report_overview_table($quiz , $qmsubselect, $groupstudents,
57d6a267 12 $students, $detailedmarks, $questions, $candelete, $reporturl, $displayoptions, $context){
c35f3afc 13 parent::table_sql('mod-quiz-report-overview-report');
14 $this->quiz = $quiz;
15 $this->qmsubselect = $qmsubselect;
16 $this->groupstudents = $groupstudents;
17 $this->students = $students;
18 $this->detailedmarks = $detailedmarks;
19 $this->questions = $questions;
20 $this->candelete = $candelete;
21 $this->reporturl = $reporturl;
22 $this->displayoptions = $displayoptions;
57d6a267 23 $this->context = $context;
c35f3afc 24 }
25 function build_table(){
9cf4a18b 26 global $CFG, $DB;
c35f3afc 27 if ($this->rawdata) {
28 // Define some things we need later to process raw data from db.
29 $this->strtimeformat = get_string('strftimedatetime');
30 parent::build_table();
31 //end of adding data from attempts data to table / download
32 //now add averages at bottom of table :
9cf4a18b 33 $params = array($this->quiz->id);
c35f3afc 34 $averagesql = "SELECT AVG(qg.grade) AS grade " .
9cf4a18b 35 "FROM {quiz_grades} qg " .
36 "WHERE quiz=?";
37
c35f3afc 38 $this->add_separator();
39 if ($this->is_downloading()){
40 $namekey = 'lastname';
41 } else {
42 $namekey = 'fullname';
43 }
44 if ($this->groupstudents){
8673a566 45 list($g_usql, $g_params) = $DB->get_in_or_equal($this->groupstudents);
9cf4a18b 46
47 $groupaveragesql = $averagesql." AND qg.userid $g_usql";
48 $groupaverage = $DB->get_record_sql($groupaveragesql, array_merge($params, $g_params));
c35f3afc 49 $groupaveragerow = array($namekey => get_string('groupavg', 'grades'),
f88fb62c 50 'sumgrades' => quiz_format_grade($this->quiz, $groupaverage->grade),
c35f3afc 51 'feedbacktext'=> strip_tags(quiz_report_feedback_for_grade($groupaverage->grade, $this->quiz->id)));
52 if($this->detailedmarks && $this->qmsubselect) {
53 $avggradebyq = quiz_get_average_grade_for_questions($this->quiz, $this->groupstudents);
54 $groupaveragerow += quiz_format_average_grade_for_questions($avggradebyq, $this->questions, $this->quiz, $this->is_downloading());
55 }
56 $this->add_data_keyed($groupaveragerow);
57 }
9cf4a18b 58
8673a566 59 list($s_usql, $s_params) = $DB->get_in_or_equal($this->students);
9cf4a18b 60 $overallaverage = $DB->get_record_sql($averagesql." AND qg.userid $s_usql", array_merge($params, $s_params));
c35f3afc 61 $overallaveragerow = array($namekey => get_string('overallaverage', 'grades'),
f88fb62c 62 'sumgrades' => quiz_format_grade($this->quiz, $overallaverage->grade),
c35f3afc 63 'feedbacktext'=> strip_tags(quiz_report_feedback_for_grade($overallaverage->grade, $this->quiz->id)));
64 if($this->detailedmarks && $this->qmsubselect) {
65 $avggradebyq = quiz_get_average_grade_for_questions($this->quiz, $this->students);
66 $overallaveragerow += quiz_format_average_grade_for_questions($avggradebyq, $this->questions, $this->quiz, $this->is_downloading());
67 }
68 $this->add_data_keyed($overallaveragerow);
69 }
70 }
9cf4a18b 71
c35f3afc 72 function wrap_html_start(){
73 if (!$this->is_downloading()) {
74 if ($this->candelete) {
75 // Start form
c35f3afc 76 echo '<div id="tablecontainer">';
98f38217 77 echo '<form id="attemptsform" method="post" action="' . $this->reporturl->out(true) .'">';
5bb614fe 78 echo '<div style="display: none;">';
c35f3afc 79 echo $this->reporturl->hidden_params_out(array(), 0, $this->displayoptions);
5bb614fe 80 echo '</div>';
c35f3afc 81 echo '<div>';
82 }
83 }
84 }
85 function wrap_html_finish(){
86 if (!$this->is_downloading()) {
87 // Print "Select all" etc.
88 if ($this->candelete) {
98f38217 89 $strreallydel = addslashes_js(get_string('deleteattemptcheck','quiz'));
90 echo '<div id="commands">';
c35f3afc 91 echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.
92 get_string('selectall', 'quiz').'</a> / ';
93 echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.
94 get_string('selectnone', 'quiz').'</a> ';
95 echo '&nbsp;&nbsp;';
314b8a20 96 if (has_capability('mod/quiz:grade', $this->context)){
97 echo '<input type="submit" name="regrade" value="'.get_string('regradeselected', 'quiz_overview').'"/>';
98 }
98f38217 99 echo '<input type="submit" onclick="return confirm(\''.$strreallydel.'\');" name="delete" value="'.get_string('deleteselected', 'quiz_overview').'"/>';
100 echo '</div>';
c35f3afc 101 // Close form
102 echo '</div>';
103 echo '</form></div>';
104 }
105 }
106 }
107
9cf4a18b 108
c35f3afc 109 function col_checkbox($attempt){
110 if ($attempt->attempt){
111 return '<input type="checkbox" name="attemptid[]" value="'.$attempt->attempt.'" />';
112 } else {
113 return '';
114 }
115 }
9cf4a18b 116
c35f3afc 117 function col_picture($attempt){
118 global $COURSE;
375381a0 119 $user = new object();
120 $user->id = $attempt->userid;
121 $user->lastname = $attempt->lastname;
122 $user->firstname = $attempt->firstname;
123 $user->imagealt = $attempt->imagealt;
124 $user->picture = $attempt->picture;
125 return print_user_picture($user, $COURSE->id, $attempt->picture, false, true);
c35f3afc 126 }
127
9cf4a18b 128
c35f3afc 129 function col_timestart($attempt){
130 if ($attempt->attempt) {
131 $startdate = userdate($attempt->timestart, $this->strtimeformat);
132 if (!$this->is_downloading()) {
133 return '<a href="review.php?q='.$this->quiz->id.'&amp;attempt='.$attempt->attempt.'">'.$startdate.'</a>';
134 } else {
135 return $startdate;
136 }
137 } else {
138 return '-';
139 }
140 }
141 function col_timefinish($attempt){
142 if ($attempt->attempt) {
143 if ($attempt->timefinish) {
144 $timefinish = userdate($attempt->timefinish, $this->strtimeformat);
145 if (!$this->is_downloading()) {
146 return '<a href="review.php?q='.$this->quiz->id.'&amp;attempt='.$attempt->attempt.'">'.$timefinish.'</a>';
147 } else {
148 return $timefinish;
149 }
150 } else {
151 return '-';
152 }
153 } else {
154 return '-';
155 }
156 }
9cf4a18b 157
c35f3afc 158 function col_duration($attempt){
159 if ($attempt->timefinish) {
160 return format_time($attempt->duration);
161 } elseif ($attempt->timestart) {
162 return get_string('unfinished', 'quiz');
163 } else {
164 return '-';
165 }
166 }
167 function col_sumgrades($attempt){
168 if ($attempt->timefinish) {
169 $grade = quiz_rescale_grade($attempt->sumgrades, $this->quiz);
170 if (!$this->is_downloading()) {
98f38217 171 if (isset($this->regradedqs[$attempt->attemptuniqueid])){
172 $newsumgrade = 0;
173 $oldsumgrade = 0;
174 foreach ($this->questions as $question){
175 if (isset($this->regradedqs[$attempt->attemptuniqueid][$question->id])){
176 $newsumgrade += $this->regradedqs[$attempt->attemptuniqueid][$question->id]->newgrade;
177 $oldsumgrade += $this->regradedqs[$attempt->attemptuniqueid][$question->id]->oldgrade;
178 } else {
179 $newsumgrade += $this->gradedstatesbyattempt[$attempt->attemptuniqueid][$question->id]->grade;
180 $oldsumgrade += $this->gradedstatesbyattempt[$attempt->attemptuniqueid][$question->id]->grade;
181 }
182 }
183 $newsumgrade = quiz_rescale_grade($newsumgrade, $this->quiz);
184 $oldsumgrade = quiz_rescale_grade($oldsumgrade, $this->quiz);
185 $grade = "<del>$oldsumgrade</del><br />$newsumgrade";
186 }
c35f3afc 187 $gradehtml = '<a href="review.php?q='.$this->quiz->id.'&amp;attempt='.$attempt->attempt.'">'.$grade.'</a>';
188 if ($this->qmsubselect && $attempt->gradedattempt){
189 $gradehtml = '<div class="highlight">'.$gradehtml.'</div>';
190 }
191 return $gradehtml;
192 } else {
193 return $grade;
194 }
195 } else {
196 return '-';
197 }
198 }
b55797b8 199
200 /**
201 * @param string $colname the name of the column.
202 * @param object $attempt the row of data - see the SQL in display() in
203 * mod/quiz/report/overview/report.php to see what fields are present,
204 * and what they are called.
205 * @return string the contents of the cell.
206 */
c35f3afc 207 function other_cols($colname, $attempt){
98f38217 208
c35f3afc 209 if (preg_match('/^qsgrade([0-9]+)$/', $colname, $matches)){
210 $questionid = $matches[1];
211 $question = $this->questions[$questionid];
98f38217 212 if (isset($this->gradedstatesbyattempt[$attempt->attemptuniqueid][$questionid])){
213 $stateforqinattempt = $this->gradedstatesbyattempt[$attempt->attemptuniqueid][$questionid];
c35f3afc 214 } else {
98f38217 215 $stateforqinattempt = false;
c35f3afc 216 }
98f38217 217 if ($stateforqinattempt && question_state_is_graded($stateforqinattempt)) {
218 $grade = quiz_rescale_grade($stateforqinattempt->grade, $this->quiz);
219 if (!$this->is_downloading()) {
220 if (isset($this->regradedqs[$attempt->attemptuniqueid][$questionid])){
221 $gradefromdb = $grade;
222 $newgrade = quiz_rescale_grade($this->regradedqs[$attempt->attemptuniqueid][$questionid]->newgrade, $this->quiz);
223 $oldgrade = quiz_rescale_grade($this->regradedqs[$attempt->attemptuniqueid][$questionid]->oldgrade, $this->quiz);
224
225 $grade = '<del>'.$oldgrade.'</del><br />'.
226 $newgrade;
227 }
b55797b8 228 return link_to_popup_window('/mod/quiz/reviewquestion.php?attempt=' .
229 $attempt->attempt . '&amp;question=' . $question->id,
98f38217 230 'reviewquestion', $grade, 450, 650, get_string('reviewresponse', 'quiz'),
231 'none', true);
232 } else {
233 return $grade;
234 }
c35f3afc 235 } else {
98f38217 236 return '--';
9cf4a18b 237 }
c35f3afc 238 } else {
239 return NULL;
240 }
241 }
9cf4a18b 242
c35f3afc 243 function col_feedbacktext($attempt){
244 if ($attempt->timefinish) {
245 if (!$this->is_downloading()) {
246 return quiz_report_feedback_for_grade(quiz_rescale_grade($attempt->sumgrades, $this->quiz), $this->quiz->id);
247 } else {
248 return strip_tags(quiz_report_feedback_for_grade(quiz_rescale_grade($attempt->sumgrades, $this->quiz), $this->quiz->id));
249 }
250 } else {
251 return '-';
252 }
9cf4a18b 253
c35f3afc 254 }
98f38217 255 function col_regraded($attempt){
256 if ($attempt->regraded == '') {
257 return '';
258 } else if ($attempt->regraded == 0) {
259 return get_string('needed', 'quiz_overview');
260 } else if ($attempt->regraded == 1) {
261 return get_string('done', 'quiz_overview');
262 }
263 }
8673a566 264 function query_db($pagesize, $useinitialsbar=true){
265 // Add table joins so we can sort by question grade
266 // unfortunately can't join all tables necessary to fetch all grades
267 // to get the state for one question per attempt row we must join two tables
268 // and there is a limit to how many joins you can have in one query. In MySQL it
269 // is 61. This means that when having more than 29 questions the query will fail.
270 // So we join just the tables needed to sort the attempts.
271 if($sort = $this->get_sql_sort()) {
272 if ($this->detailedmarks) {
273 $this->sql->from .= ' ';
274 $sortparts = explode(',', $sort);
275 $matches = array();
276 foreach($sortparts as $sortpart) {
277 $sortpart = trim($sortpart);
278 if (preg_match('/^qsgrade([0-9]+)/', $sortpart, $matches)){
279 $qid = intval($matches[1]);
280 $this->sql->fields .= ", qs$qid.grade AS qsgrade$qid, qs$qid.event AS qsevent$qid, qs$qid.id AS qsid$qid";
281 $this->sql->from .= "LEFT JOIN {question_sessions} qns$qid ON qns$qid.attemptid = qa.uniqueid AND qns$qid.questionid = :qid$qid ";
282 $this->sql->from .= "LEFT JOIN {question_states} qs$qid ON qs$qid.id = qns$qid.newgraded ";
283 $this->sql->params['qid'.$qid] = $qid;
284 }
285 }
286 }
287 }
288 parent::query_db($pagesize, $useinitialsbar);
98f38217 289 if ($this->detailedmarks){
290 //get all the attempt ids we want to display on this page
291 //or to export for download.
292 if (!$this->is_downloading()) {
293 $attemptids = array();
294 foreach ($this->rawdata as $attempt){
295 if ($attempt->attemptuniqueid > 0){
296 $attemptids[] = $attempt->attemptuniqueid;
297 }
298 }
299 $this->gradedstatesbyattempt = quiz_get_newgraded_states($attemptids, true, 'qs.id, qs.grade, qs.event, qs.question, qs.attempt');
300 $this->regradedqs = quiz_get_regraded_qs($attemptids);
301 } else {
302 $this->gradedstatesbyattempt = quiz_get_newgraded_states($this->sql, true, 'qs.id, qs.grade, qs.event, qs.question, qs.attempt');
303 $this->regradedqs = quiz_get_regraded_qs($this->sql);
304 }
305 }
8673a566 306 }
c35f3afc 307}
308?>