add time restriction MDL-12588 - add ability to restrict the viewing of SCORM objects...
[moodle.git] / mod / quiz / report / responses / responses_table.php
CommitLineData
7f29a7db 1<?php // $Id$
e84cb615 2define ('QUIZ_REPORT_RESPONSES_MAX_LEN_TO_DISPLAY', 150);
7f29a7db 3
4class quiz_report_responses_table extends table_sql {
5
6 var $useridfield = 'userid';
7
8 var $reporturl;
9 var $displayoptions;
10
11 function quiz_report_responses_table($quiz , $qmsubselect, $groupstudents,
12 $students, $questions, $candelete, $reporturl, $displayoptions){
13 parent::table_sql('mod-quiz-report-responses-report');
14 $this->quiz = $quiz;
15 $this->qmsubselect = $qmsubselect;
16 $this->groupstudents = $groupstudents;
17 $this->students = $students;
18 $this->questions = $questions;
19 $this->candelete = $candelete;
20 $this->reporturl = $reporturl;
21 $this->displayoptions = $displayoptions;
22 }
23 function build_table(){
7f29a7db 24 if ($this->rawdata) {
3b518020 25 $this->strtimeformat = str_replace(',', ' ', get_string('strftimedatetime'));
7f29a7db 26 parent::build_table();
7f29a7db 27 }
28 }
29
30 function wrap_html_start(){
31 if (!$this->is_downloading()) {
32 if ($this->candelete) {
33 // Start form
34 $strreallydel = addslashes_js(get_string('deleteattemptcheck','quiz'));
35 echo '<div id="tablecontainer">';
36 echo '<form id="attemptsform" method="post" action="' . $this->reporturl->out(true) .
37 '" onsubmit="confirm(\''.$strreallydel.'\');">';
38 echo '<div style="display: none;">';
39 echo $this->reporturl->hidden_params_out(array(), 0, $this->displayoptions);
40 echo '</div>';
41 echo '<div>';
42 }
43 }
44 }
45 function wrap_html_finish(){
46 if (!$this->is_downloading()) {
47 // Print "Select all" etc.
48 if ($this->candelete) {
670b8954 49 echo '<div id="commands">';
7f29a7db 50 echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.
51 get_string('selectall', 'quiz').'</a> / ';
52 echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.
53 get_string('selectnone', 'quiz').'</a> ';
54 echo '&nbsp;&nbsp;';
6559096f 55 echo '<input type="submit" value="'.get_string('deleteselected', 'quiz_overview').'"/>';
670b8954 56 echo '</div>';
7f29a7db 57 // Close form
58 echo '</div>';
59 echo '</form></div>';
60 }
61 }
62 }
63
64
65 function col_checkbox($attempt){
66 if ($attempt->attempt){
67 return '<input type="checkbox" name="attemptid[]" value="'.$attempt->attempt.'" />';
68 } else {
69 return '';
70 }
71 }
72
73 function col_picture($attempt){
74 global $COURSE;
35bf6352 75 $user = new object();
76 $user->id = $attempt->userid;
77 $user->lastname = $attempt->lastname;
78 $user->firstname = $attempt->firstname;
79 $user->imagealt = $attempt->imagealt;
80 $user->picture = $attempt->picture;
81 return print_user_picture($user, $COURSE->id, $attempt->picture, false, true);
7f29a7db 82 }
83
84
85 function col_timestart($attempt){
86 if ($attempt->attempt) {
87 $startdate = userdate($attempt->timestart, $this->strtimeformat);
88 if (!$this->is_downloading()) {
89 return '<a href="review.php?q='.$this->quiz->id.'&amp;attempt='.$attempt->attempt.'">'.$startdate.'</a>';
90 } else {
91 return $startdate;
92 }
93 } else {
94 return '-';
95 }
96 }
97 function col_timefinish($attempt){
98 if ($attempt->attempt) {
99 if ($attempt->timefinish) {
100 $timefinish = userdate($attempt->timefinish, $this->strtimeformat);
101 if (!$this->is_downloading()) {
102 return '<a href="review.php?q='.$this->quiz->id.'&amp;attempt='.$attempt->attempt.'">'.$timefinish.'</a>';
103 } else {
104 return $timefinish;
105 }
106 } else {
107 return '-';
108 }
109 } else {
110 return '-';
111 }
112 }
113
114 function col_duration($attempt){
115 if ($attempt->timefinish) {
116 return format_time($attempt->duration);
117 } elseif ($attempt->timestart) {
118 return get_string('unfinished', 'quiz');
119 } else {
120 return '-';
121 }
122 }
123 function col_sumgrades($attempt){
124 if ($attempt->timefinish) {
125 $grade = quiz_rescale_grade($attempt->sumgrades, $this->quiz);
126 if (!$this->is_downloading()) {
127 $gradehtml = '<a href="review.php?q='.$this->quiz->id.'&amp;attempt='.$attempt->attempt.'">'.$grade.'</a>';
128 if ($this->qmsubselect && $attempt->gradedattempt){
129 $gradehtml = '<div class="highlight">'.$gradehtml.'</div>';
130 }
131 return $gradehtml;
132 } else {
133 return $grade;
134 }
135 } else {
136 return '-';
137 }
138 }
139 function other_cols($colname, $attempt){
3e71541f 140 static $states =array();
7f29a7db 141 if (preg_match('/^qsanswer([0-9]+)$/', $colname, $matches)){
3f8e526e 142 if ($attempt->uniqueid == 0) {
143 return '-';
144 }
7f29a7db 145 $questionid = $matches[1];
2ea2c1f5 146 if (isset($this->gradedstatesbyattempt[$attempt->uniqueid][$questionid])){
147 $stateforqinattempt = $this->gradedstatesbyattempt[$attempt->uniqueid][$questionid];
148 } else {
149 return '-';
150 }
151
7f29a7db 152 $question = $this->questions[$questionid];
2ea2c1f5 153 restore_question_state($question, $stateforqinattempt);
154
8968f51f 155 if ($responses = get_question_actual_response($question, $stateforqinattempt)){
156 $response = (!empty($responses)? implode('; ',$responses) : '-');
157 } else {
158 $response = '';
159 }
7f29a7db 160 if (!$this->is_downloading()) {
199b946f 161 if ($response){
8968f51f 162 $format_options = new stdClass;
163 $format_options->para = false;
8968f51f 164 $format_options->newlines = false;
165 $response = format_text($response, FORMAT_MOODLE, $format_options);
e84cb615 166 if (strlen($response) > QUIZ_REPORT_RESPONSES_MAX_LEN_TO_DISPLAY){
167 $response = shorten_text($response, QUIZ_REPORT_RESPONSES_MAX_LEN_TO_DISPLAY);
168 }
169 $response = link_to_popup_window('/mod/quiz/reviewquestion.php?attempt=' .
170 $attempt->attempt . '&amp;question=' . $question->id,
171 'reviewquestion', $response, 450, 650, get_string('reviewresponse', 'quiz'),
172 'none', true);
8968f51f 173 if (question_state_is_graded($stateforqinattempt)
174 && ($this->questions[$questionid]->maxgrade != 0)){
175 $grade = $stateforqinattempt->grade
176 / $this->questions[$questionid]->maxgrade;
177 $qclass = question_get_feedback_class($grade);
178 $feedbackimg = question_get_feedback_image($grade);
179 $questionclass = "que";
e1d2278b 180 return "<span class=\"$questionclass\"><span class=\"$qclass\">".$response."</span></span>$feedbackimg";
181 } else {
182 return $response;
183 }
7f29a7db 184 } else {
199b946f 185 return '';
186 }
187
7f29a7db 188 } else {
ef27e742 189 return $this->format_text($response);
7f29a7db 190 }
191 } else {
192 return NULL;
193 }
194 }
195
196 function col_feedbacktext($attempt){
197 if ($attempt->timefinish) {
198 if (!$this->is_downloading()) {
199 return quiz_report_feedback_for_grade(quiz_rescale_grade($attempt->sumgrades, $this->quiz), $this->quiz->id);
200 } else {
201 return strip_tags(quiz_report_feedback_for_grade(quiz_rescale_grade($attempt->sumgrades, $this->quiz), $this->quiz->id));
202 }
203 } else {
204 return '-';
205 }
206
207 }
208
209 function query_db($pagesize, $useinitialsbar=true){
210 // Add table joins so we can sort by question answer
211 // unfortunately can't join all tables necessary to fetch all answers
212 // to get the state for one question per attempt row we must join two tables
213 // and there is a limit to how many joins you can have in one query. In MySQL it
214 // is 61. This means that when having more than 29 questions the query will fail.
215 // So we join just the tables needed to sort the attempts.
216 if($sort = $this->get_sql_sort()) {
217 $this->sql->from .= ' ';
218 $sortparts = explode(',', $sort);
219 $matches = array();
220 foreach($sortparts as $sortpart) {
221 $sortpart = trim($sortpart);
222 if (preg_match('/^qsanswer([0-9]+)/', $sortpart, $matches)){
223 $qid = intval($matches[1]);
224 $this->sql->fields .= ", qs$qid.grade AS qsgrade$qid, qs$qid.answer AS qsanswer$qid, qs$qid.event AS qsevent$qid, qs$qid.id AS qsid$qid";
225 $this->sql->from .= "LEFT JOIN {question_sessions} qns$qid ON qns$qid.attemptid = qa.uniqueid AND qns$qid.questionid = :qid$qid ";
226 $this->sql->from .= "LEFT JOIN {question_states} qs$qid ON qs$qid.id = qns$qid.newgraded ";
227 $this->sql->params['qid'.$qid] = $qid;
228 }
229 }
230 }
231 parent::query_db($pagesize, $useinitialsbar);
2ea2c1f5 232 $qsfields = 'qs.id, qs.grade, qs.event, qs.question, qs.answer, qs.attempt';
233 if (!$this->is_downloading()) {
234 $attemptids = array();
235 foreach ($this->rawdata as $attempt){
236 if ($attempt->uniqueid > 0){
237 $attemptids[] = $attempt->uniqueid;
238 }
239 }
240 $this->gradedstatesbyattempt = quiz_get_newgraded_states($attemptids, true, $qsfields);
241 } else {
242 $this->gradedstatesbyattempt = quiz_get_newgraded_states($this->sql, true, $qsfields);
243 }
7f29a7db 244 }
245}
246?>