MDL-27550 workshop: fixed XHTML strictness of H3 and A combo
[moodle.git] / mod / workshop / renderer.php
CommitLineData
66c9894d 1<?php
53fad4b9
DM
2
3// This file is part of Moodle - http://moodle.org/
4//
66c9894d
DM
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
53fad4b9 14//
66c9894d
DM
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
53fad4b9 17
66c9894d 18/**
c2a35266 19 * Workshop module renderering methods are defined here
66c9894d 20 *
65601f04
DM
21 * @package mod
22 * @subpackage workshop
23 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
66c9894d
DM
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
53fad4b9
DM
30 * Workshop module renderer class
31 *
32 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
66c9894d 34 */
6adbcb80 35class mod_workshop_renderer extends plugin_renderer_base {
66c9894d 36
81b22887 37 ////////////////////////////////////////////////////////////////////////////
c2a35266 38 // External API - methods to render workshop renderable components
81b22887 39 ////////////////////////////////////////////////////////////////////////////
66c9894d 40
a8b309a3
DM
41 /**
42 * Renders workshop message
43 *
44 * @param workshop_message $message to display
45 * @return string html code
46 */
47 protected function render_workshop_message(workshop_message $message) {
48
49 $text = $message->get_message();
50 $url = $message->get_action_url();
51 $label = $message->get_action_label();
52
53 if (empty($text) and empty($label)) {
54 return '';
55 }
56
57 switch ($message->get_type()) {
58 case workshop_message::TYPE_OK:
59 $sty = 'ok';
60 break;
61 case workshop_message::TYPE_ERROR:
62 $sty = 'error';
63 break;
64 default:
65 $sty = 'info';
66 }
67
68 $o = html_writer::tag('span', $message->get_message());
69
70 if (!is_null($url) and !is_null($label)) {
71 $o .= $this->output->single_button($url, $label, 'get');
72 }
73
74 return $this->output->container($o, array('message', $sty));
75 }
76
77
e9b0f0ab 78 /**
81b22887 79 * Renders full workshop submission
ddb59c77 80 *
81b22887
DM
81 * @param workshop_submission $submission
82 * @return string HTML
e9b0f0ab 83 */
81b22887 84 protected function render_workshop_submission(workshop_submission $submission) {
e9b0f0ab
DM
85
86 $o = ''; // output HTML code
81b22887
DM
87 $anonymous = $submission->is_anonymous();
88 $classes = 'submission-full';
89 if ($anonymous) {
e9b0f0ab
DM
90 $classes .= ' anonymous';
91 }
81b22887
DM
92 $o .= $this->output->container_start($classes);
93 $o .= $this->output->container_start('header');
2c75324e 94
64033588 95 $title = format_string($submission->title);
2c75324e
DM
96
97 if ($this->page->url != $submission->url) {
64033588 98 $title = html_writer::link($submission->url, $title);
2c75324e 99 }
81b22887 100
64033588
DM
101 $o .= $this->output->heading($title, 3, 'title');
102
81b22887 103 if (!$anonymous) {
7a789aa8 104 $author = new stdclass();
e9b0f0ab
DM
105 $author->id = $submission->authorid;
106 $author->firstname = $submission->authorfirstname;
107 $author->lastname = $submission->authorlastname;
108 $author->picture = $submission->authorpicture;
109 $author->imagealt = $submission->authorimagealt;
3a11c09f 110 $author->email = $submission->authoremail;
81b22887 111 $userpic = $this->output->user_picture($author, array('courseid' => $this->page->course->id, 'size' => 64));
a6855934 112 $userurl = new moodle_url('/user/view.php',
e9b0f0ab 113 array('id' => $author->id, 'course' => $this->page->course->id));
7a789aa8 114 $a = new stdclass();
e9b0f0ab
DM
115 $a->name = fullname($author);
116 $a->url = $userurl->out();
117 $byfullname = get_string('byfullname', 'workshop', $a);
e9b0f0ab
DM
118 $oo = $this->output->container($userpic, 'picture');
119 $oo .= $this->output->container($byfullname, 'fullname');
81b22887 120
e9b0f0ab
DM
121 $o .= $this->output->container($oo, 'author');
122 }
81b22887 123
e9b0f0ab
DM
124 $created = get_string('userdatecreated', 'workshop', userdate($submission->timecreated));
125 $o .= $this->output->container($created, 'userdate created');
81b22887 126
e9b0f0ab
DM
127 if ($submission->timemodified > $submission->timecreated) {
128 $modified = get_string('userdatemodified', 'workshop', userdate($submission->timemodified));
129 $o .= $this->output->container($modified, 'userdate modified');
130 }
4d63c194 131
81b22887
DM
132 $o .= $this->output->container_end(); // end of header
133
367a75fa 134 $content = format_text($submission->content, $submission->contentformat, array('overflowdiv'=>true));
81b22887
DM
135 $content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $this->page->context->id,
136 'mod_workshop', 'submission_content', $submission->id);
137 $o .= $this->output->container($content, 'content');
138
139 $o .= $this->helper_submission_attachments($submission->id, 'html');
140
141 $o .= $this->output->container_end(); // end of submission-full
e9b0f0ab
DM
142
143 return $o;
144 }
145
b8ead2e6 146 /**
81b22887 147 * Renders short summary of the submission
b8ead2e6 148 *
81b22887
DM
149 * @param workshop_submission_summary $summary
150 * @return string text to be echo'ed
b8ead2e6 151 */
81b22887 152 protected function render_workshop_submission_summary(workshop_submission_summary $summary) {
b8ead2e6 153
e9b0f0ab 154 $o = ''; // output HTML code
81b22887
DM
155 $anonymous = $summary->is_anonymous();
156 $classes = 'submission-summary';
157
158 if ($anonymous) {
e9b0f0ab 159 $classes .= ' anonymous';
b8ead2e6 160 }
81b22887
DM
161
162 $gradestatus = '';
163
164 if ($summary->status == 'notgraded') {
165 $classes .= ' notgraded';
166 $gradestatus = $this->output->container(get_string('nogradeyet', 'workshop'), 'grade-status');
167
168 } else if ($summary->status == 'graded') {
169 $classes .= ' graded';
170 $gradestatus = $this->output->container(get_string('alreadygraded', 'workshop'), 'grade-status');
171 }
172
173 $o .= $this->output->container_start($classes); // main wrapper
174 $o .= html_writer::link($summary->url, format_string($summary->title), array('class' => 'title'));
175
176 if (!$anonymous) {
177 $author = new stdClass();
178 $author->id = $summary->authorid;
179 $author->firstname = $summary->authorfirstname;
180 $author->lastname = $summary->authorlastname;
181 $author->picture = $summary->authorpicture;
182 $author->imagealt = $summary->authorimagealt;
183 $author->email = $summary->authoremail;
184 $userpic = $this->output->user_picture($author, array('courseid' => $this->page->course->id, 'size' => 35));
a6855934 185 $userurl = new moodle_url('/user/view.php',
b8ead2e6 186 array('id' => $author->id, 'course' => $this->page->course->id));
81b22887 187 $a = new stdClass();
e9b0f0ab
DM
188 $a->name = fullname($author);
189 $a->url = $userurl->out();
190 $byfullname = get_string('byfullname', 'workshop', $a);
81b22887 191
e9b0f0ab
DM
192 $oo = $this->output->container($userpic, 'picture');
193 $oo .= $this->output->container($byfullname, 'fullname');
81b22887 194 $o .= $this->output->container($oo, 'author');
b8ead2e6 195 }
81b22887
DM
196
197 $created = get_string('userdatecreated', 'workshop', userdate($summary->timecreated));
e9b0f0ab 198 $o .= $this->output->container($created, 'userdate created');
81b22887
DM
199
200 if ($summary->timemodified > $summary->timecreated) {
201 $modified = get_string('userdatemodified', 'workshop', userdate($summary->timemodified));
e9b0f0ab 202 $o .= $this->output->container($modified, 'userdate modified');
b8ead2e6 203 }
b8ead2e6 204
81b22887
DM
205 $o .= $gradestatus;
206 $o .= $this->output->container_end(); // end of the main wrapper
b8ead2e6
DM
207 return $o;
208 }
209
210 /**
81b22887 211 * Renders full workshop example submission
b8ead2e6 212 *
81b22887
DM
213 * @param workshop_example_submission $example
214 * @return string HTML
b8ead2e6 215 */
81b22887 216 protected function render_workshop_example_submission(workshop_example_submission $example) {
b8ead2e6 217
81b22887
DM
218 $o = ''; // output HTML code
219 $classes = 'submission-full example';
220 $o .= $this->output->container_start($classes);
221 $o .= $this->output->container_start('header');
222 $o .= $this->output->heading(format_string($example->title), 3, 'title');
223 $o .= $this->output->container_end(); // end of header
b8ead2e6 224
367a75fa 225 $content = format_text($example->content, $example->contentformat, array('overflowdiv'=>true));
81b22887
DM
226 $content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $this->page->context->id,
227 'mod_workshop', 'submission_content', $example->id);
228 $o .= $this->output->container($content, 'content');
b8ead2e6 229
81b22887 230 $o .= $this->helper_submission_attachments($example->id, 'html');
b8ead2e6 231
81b22887 232 $o .= $this->output->container_end(); // end of submission-full
b8ead2e6 233
81b22887 234 return $o;
b8ead2e6
DM
235 }
236
81eccf0a 237 /**
81b22887 238 * Renders short summary of the example submission
81eccf0a 239 *
81b22887
DM
240 * @param workshop_example_submission_summary $summary
241 * @return string text to be echo'ed
81eccf0a 242 */
81b22887 243 protected function render_workshop_example_submission_summary(workshop_example_submission_summary $summary) {
81eccf0a
DM
244
245 $o = ''; // output HTML code
cbf87967
DM
246
247 // wrapping box
248 $o .= $this->output->box_start('generalbox example-summary ' . $summary->status);
249
250 // title
251 $o .= $this->output->container_start('example-title');
81b22887
DM
252 $o .= html_writer::link($summary->url, format_string($summary->title), array('class' => 'title'));
253
254 if ($summary->editable) {
255 $o .= $this->output->action_icon($summary->editurl, new pix_icon('i/edit', get_string('edit')));
cbf87967
DM
256 }
257 $o .= $this->output->container_end();
81eccf0a 258
cbf87967
DM
259 // additional info
260 if ($summary->status == 'notgraded') {
261 $o .= $this->output->container(get_string('nogradeyet', 'workshop'), 'example-info nograde');
262 } else {
5a372494 263 $o .= $this->output->container(get_string('gradeinfo', 'workshop' , $summary->gradeinfo), 'example-info grade');
cbf87967
DM
264 }
265
266 // button to assess
81b22887
DM
267 $button = new single_button($summary->assessurl, $summary->assesslabel, 'get');
268 $o .= $this->output->container($this->output->render($button), 'example-actions');
cbf87967
DM
269
270 // end of wrapping box
271 $o .= $this->output->box_end();
81eccf0a 272
81eccf0a
DM
273 return $o;
274 }
275
b761e6d9 276 /**
29dc43e7 277 * Renders the user plannner tool
b761e6d9 278 *
55fc1e59 279 * @param workshop_user_plan $plan prepared for the user
29dc43e7 280 * @return string html code to be displayed
b761e6d9 281 */
55fc1e59 282 protected function render_workshop_user_plan(workshop_user_plan $plan) {
b761e6d9 283 $table = new html_table();
16be8974 284 $table->attributes['class'] = 'userplan';
b761e6d9
DM
285 $table->head = array();
286 $table->colclasses = array();
287 $row = new html_table_row();
16be8974 288 $row->attributes['class'] = 'phasetasks';
55fc1e59 289 foreach ($plan->phases as $phasecode => $phase) {
26acc814 290 $title = html_writer::tag('span', $phase->title);
454e8dd9
DM
291 $actions = '';
292 foreach ($phase->actions as $action) {
293 switch ($action->type) {
294 case 'switchphase':
c63923bd 295 $actions .= $this->output->action_icon($action->url, new pix_icon('i/marker', get_string('switchphase', 'workshop')));
454e8dd9
DM
296 break;
297 }
298 }
299 if (!empty($actions)) {
300 $actions = $this->output->container($actions, 'actions');
301 }
302 $table->head[] = $this->output->container($title . $actions);
b761e6d9
DM
303 $classes = 'phase' . $phasecode;
304 if ($phase->active) {
305 $classes .= ' active';
306 } else {
307 $classes .= ' nonactive';
308 }
309 $table->colclasses[] = $classes;
310 $cell = new html_table_cell();
81b22887 311 $cell->text = $this->helper_user_plan_tasks($phase->tasks);
b761e6d9
DM
312 $row->cells[] = $cell;
313 }
314 $table->data = array($row);
315
16be8974 316 return html_writer::table($table);
b761e6d9
DM
317 }
318
a8b309a3
DM
319 /**
320 * Renders the result of the submissions allocation process
321 *
322 * @param workshop_allocation_init_result
323 * @return string html to be echoed
324 */
325 protected function render_workshop_allocation_init_result(workshop_allocation_init_result $result) {
326
327 // start with the message
328 $o = $this->render($result->get_message());
329
330 // display the details about the process if available
331 $info = $result->get_info();
332 if (is_array($info) and !empty($info)) {
333 $o .= html_writer::start_tag('ul', array('class' => 'allocation-init-results'));
334 foreach ($info as $message) {
335 $parts = explode('::', $message);
336 $text = array_pop($parts);
337 $class = implode(' ', $parts);
338 if (in_array('debug', $parts) && !debugging('', DEBUG_DEVELOPER)) {
339 // do not display allocation debugging messages
340 continue;
341 }
342 $o .= html_writer::tag('li', $text, array('class' => $class)) . "\n";
343 }
344 $o .= html_writer::end_tag('ul');
345 }
346
347 $o .= $this->output->continue_button($result->get_continue_url());
348
349 return $o;
350 }
351
29dc43e7
DM
352 /**
353 * Renders the workshop grading report
354 *
c2a35266 355 * @param workshop_grading_report $gradingreport
29dc43e7
DM
356 * @return string html code
357 */
c2a35266
DM
358 protected function render_workshop_grading_report(workshop_grading_report $gradingreport) {
359
360 $data = $gradingreport->get_data();
361 $options = $gradingreport->get_options();
362 $grades = $data->grades;
363 $userinfo = $data->userinfo;
29dc43e7
DM
364
365 if (empty($grades)) {
366 return '';
367 }
368
d10ee0be 369 $table = new html_table();
16be8974 370 $table->attributes['class'] = 'grading-report';
d10ee0be 371
c2a35266
DM
372 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'), 'firstname', $options->sortby, $options->sorthow);
373 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'), 'lastname', $options->sortby, $options->sorthow);
d10ee0be
DM
374 if (self::fullname_format() == 'lf') {
375 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
376 } else {
377 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
378 }
379
d183140d
DM
380 $table->head = array();
381 $table->head[] = $sortbyname;
c2a35266 382 $table->head[] = $this->helper_sortable_heading(get_string('submission', 'workshop'), 'submissiontitle',
d183140d 383 $options->sortby, $options->sorthow);
c2a35266 384 $table->head[] = $this->helper_sortable_heading(get_string('receivedgrades', 'workshop'));
d183140d 385 if ($options->showsubmissiongrade) {
c2a35266 386 $table->head[] = $this->helper_sortable_heading(get_string('submissiongradeof', 'workshop', $data->maxgrade),
d183140d
DM
387 'submissiongrade', $options->sortby, $options->sorthow);
388 }
c2a35266 389 $table->head[] = $this->helper_sortable_heading(get_string('givengrades', 'workshop'));
d183140d 390 if ($options->showgradinggrade) {
c2a35266 391 $table->head[] = $this->helper_sortable_heading(get_string('gradinggradeof', 'workshop', $data->maxgradinggrade),
d183140d
DM
392 'gradinggrade', $options->sortby, $options->sorthow);
393 }
d183140d 394
29dc43e7 395 $table->rowclasses = array();
f55650e6 396 $table->colclasses = array();
29dc43e7
DM
397 $table->data = array();
398
399 foreach ($grades as $participant) {
400 $numofreceived = count($participant->reviewedby);
401 $numofgiven = count($participant->reviewerof);
232175e4 402 $published = $participant->submissionpublished;
29dc43e7
DM
403
404 // compute the number of <tr> table rows needed to display this participant
405 if ($numofreceived > 0 and $numofgiven > 0) {
d5506aac 406 $numoftrs = workshop::lcm($numofreceived, $numofgiven);
29dc43e7
DM
407 $spanreceived = $numoftrs / $numofreceived;
408 $spangiven = $numoftrs / $numofgiven;
409 } elseif ($numofreceived == 0 and $numofgiven > 0) {
410 $numoftrs = $numofgiven;
411 $spanreceived = $numoftrs;
412 $spangiven = $numoftrs / $numofgiven;
413 } elseif ($numofreceived > 0 and $numofgiven == 0) {
414 $numoftrs = $numofreceived;
415 $spanreceived = $numoftrs / $numofreceived;
416 $spangiven = $numoftrs;
417 } else {
418 $numoftrs = 1;
419 $spanreceived = 1;
420 $spangiven = 1;
421 }
422
423 for ($tr = 0; $tr < $numoftrs; $tr++) {
424 $row = new html_table_row();
232175e4
DM
425 if ($published) {
426 $row->attributes['class'] = 'published';
427 }
29dc43e7
DM
428 // column #1 - participant - spans over all rows
429 if ($tr == 0) {
430 $cell = new html_table_cell();
c2a35266 431 $cell->text = $this->helper_grading_report_participant($participant, $userinfo);
29dc43e7 432 $cell->rowspan = $numoftrs;
16be8974 433 $cell->attributes['class'] = 'participant';
29dc43e7
DM
434 $row->cells[] = $cell;
435 }
436 // column #2 - submission - spans over all rows
437 if ($tr == 0) {
438 $cell = new html_table_cell();
c2a35266 439 $cell->text = $this->helper_grading_report_submission($participant);
29dc43e7 440 $cell->rowspan = $numoftrs;
16be8974 441 $cell->attributes['class'] = 'submission';
29dc43e7
DM
442 $row->cells[] = $cell;
443 }
444 // column #3 - received grades
445 if ($tr % $spanreceived == 0) {
446 $idx = intval($tr / $spanreceived);
d183140d 447 $assessment = self::array_nth($participant->reviewedby, $idx);
29dc43e7 448 $cell = new html_table_cell();
c2a35266 449 $cell->text = $this->helper_grading_report_assessment($assessment, $options->showreviewernames, $userinfo,
d183140d 450 get_string('gradereceivedfrom', 'workshop'));
29dc43e7 451 $cell->rowspan = $spanreceived;
16be8974 452 $cell->attributes['class'] = 'receivedgrade';
d183140d 453 if (is_null($assessment) or is_null($assessment->grade)) {
16be8974 454 $cell->attributes['class'] .= ' null';
d183140d 455 } else {
16be8974 456 $cell->attributes['class'] .= ' notnull';
d183140d 457 }
29dc43e7
DM
458 $row->cells[] = $cell;
459 }
460 // column #4 - total grade for submission
d183140d 461 if ($options->showsubmissiongrade and $tr == 0) {
29dc43e7 462 $cell = new html_table_cell();
c2a35266 463 $cell->text = $this->helper_grading_report_grade($participant->submissiongrade, $participant->submissiongradeover);
29dc43e7 464 $cell->rowspan = $numoftrs;
16be8974 465 $cell->attributes['class'] = 'submissiongrade';
29dc43e7
DM
466 $row->cells[] = $cell;
467 }
468 // column #5 - given grades
469 if ($tr % $spangiven == 0) {
470 $idx = intval($tr / $spangiven);
d183140d 471 $assessment = self::array_nth($participant->reviewerof, $idx);
29dc43e7 472 $cell = new html_table_cell();
c2a35266 473 $cell->text = $this->helper_grading_report_assessment($assessment, $options->showauthornames, $userinfo,
d183140d 474 get_string('gradegivento', 'workshop'));
29dc43e7 475 $cell->rowspan = $spangiven;
16be8974 476 $cell->attributes['class'] = 'givengrade';
d183140d 477 if (is_null($assessment) or is_null($assessment->grade)) {
16be8974 478 $cell->attributes['class'] .= ' null';
d183140d 479 } else {
16be8974 480 $cell->attributes['class'] .= ' notnull';
d183140d 481 }
29dc43e7
DM
482 $row->cells[] = $cell;
483 }
484 // column #6 - total grade for assessment
d183140d 485 if ($options->showgradinggrade and $tr == 0) {
29dc43e7 486 $cell = new html_table_cell();
c2a35266 487 $cell->text = $this->helper_grading_report_grade($participant->gradinggrade);
29dc43e7 488 $cell->rowspan = $numoftrs;
16be8974 489 $cell->attributes['class'] = 'gradinggrade';
29dc43e7
DM
490 $row->cells[] = $cell;
491 }
f27b70fb 492
29dc43e7
DM
493 $table->data[] = $row;
494 }
495 }
496
16be8974 497 return html_writer::table($table);
29dc43e7
DM
498 }
499
0dfb4bad
DM
500 /**
501 * Renders the feedback for the author of the submission
502 *
503 * @param workshop_feedback_author $feedback
504 * @return string HTML
505 */
506 protected function render_workshop_feedback_author(workshop_feedback_author $feedback) {
f68648e9
DM
507 return $this->helper_render_feedback($feedback);
508 }
509
510 /**
511 * Renders the feedback for the reviewer of the submission
512 *
513 * @param workshop_feedback_reviewer $feedback
514 * @return string HTML
515 */
516 protected function render_workshop_feedback_reviewer(workshop_feedback_reviewer $feedback) {
517 return $this->helper_render_feedback($feedback);
518 }
519
520 /**
521 * Helper method to rendering feedback
522 *
523 * @param workshop_feedback_author|workshop_feedback_reviewer $feedback
524 * @return string HTML
525 */
526 private function helper_render_feedback($feedback) {
0dfb4bad
DM
527
528 $o = ''; // output HTML code
529 $o .= $this->output->container_start('feedback feedbackforauthor');
530 $o .= $this->output->container_start('header');
531 $o .= $this->output->heading(get_string('feedbackby', 'workshop', s(fullname($feedback->get_provider()))), 3, 'title');
532
533 $userpic = $this->output->user_picture($feedback->get_provider(), array('courseid' => $this->page->course->id, 'size' => 32));
534 $o .= $this->output->container($userpic, 'picture');
535 $o .= $this->output->container_end(); // end of header
536
537 $content = format_text($feedback->get_content(), $feedback->get_format(), array('overflowdiv' => true));
538 $o .= $this->output->container($content, 'content');
539
f68648e9 540 $o .= $this->output->container_end();
0dfb4bad
DM
541
542 return $o;
543 }
c2a35266 544
38504a44
DM
545 /**
546 * Renders the full assessment
547 *
548 * @param workshop_assessment $assessment
549 * @return string HTML
550 */
551 protected function render_workshop_assessment(workshop_assessment $assessment) {
552
553 $o = ''; // output HTML code
554 $anonymous = is_null($assessment->reviewer);
555 $classes = 'assessment-full';
556 if ($anonymous) {
557 $classes .= ' anonymous';
558 }
559
560 $o .= $this->output->container_start($classes);
561 $o .= $this->output->container_start('header');
562
563 if (!empty($assessment->title)) {
2c75324e
DM
564 $title = s($assessment->title);
565 } else {
566 $title = get_string('assessment', 'workshop');
567 }
568 if ($this->page->url != $assessment->url) {
569 $o .= $this->output->container(html_writer::link($assessment->url, $title), 'title');
38504a44 570 } else {
2c75324e 571 $o .= $this->output->container($title, 'title');
38504a44
DM
572 }
573
574 if (!$anonymous) {
575 $reviewer = $assessment->reviewer;
576 $userpic = $this->output->user_picture($reviewer, array('courseid' => $this->page->course->id, 'size' => 32));
577
578 $userurl = new moodle_url('/user/view.php',
579 array('id' => $reviewer->id, 'course' => $this->page->course->id));
580 $a = new stdClass();
581 $a->name = fullname($reviewer);
582 $a->url = $userurl->out();
583 $byfullname = get_string('assessmentby', 'workshop', $a);
584 $oo = $this->output->container($userpic, 'picture');
585 $oo .= $this->output->container($byfullname, 'fullname');
586
587 $o .= $this->output->container($oo, 'reviewer');
588 }
589
590 if (is_null($assessment->realgrade)) {
591 $o .= $this->output->container(
592 get_string('notassessed', 'workshop'),
593 'grade nograde'
594 );
595 } else {
596 $a = new stdClass();
597 $a->max = $assessment->maxgrade;
598 $a->received = $assessment->realgrade;
599 $o .= $this->output->container(
600 get_string('gradeinfo', 'workshop', $a),
601 'grade'
602 );
603
604 if (!is_null($assessment->weight) and $assessment->weight != 1) {
605 $o .= $this->output->container(
606 get_string('weightinfo', 'workshop', $assessment->weight),
607 'weight'
608 );
609 }
610 }
611
612 $o .= $this->output->container_start('actions');
613 foreach ($assessment->actions as $action) {
614 $o .= $this->output->single_button($action->url, $action->label, $action->method);
615 }
616 $o .= $this->output->container_end(); // actions
617
618 $o .= $this->output->container_end(); // header
619
620 if (!is_null($assessment->form)) {
621 $o .= print_collapsible_region_start('assessment-form-wrapper', uniqid('workshop-assessment'),
622 get_string('assessmentform', 'workshop'), '', false, true);
623 $o .= $this->output->container(self::moodleform($assessment->form), 'assessment-form');
624 $o .= print_collapsible_region_end(true);
625 }
626
627 $o .= $this->output->container_end(); // main wrapper
628
629 return $o;
630 }
631
c2a35266
DM
632 ////////////////////////////////////////////////////////////////////////////
633 // Internal rendering helper methods
634 ////////////////////////////////////////////////////////////////////////////
635
636 /**
637 * Renders a list of files attached to the submission
638 *
639 * If format==html, then format a html string. If format==text, then format a text-only string.
640 * Otherwise, returns html for non-images and html to display the image inline.
641 *
642 * @param int $submissionid submission identifier
643 * @param string format the format of the returned string - html|text
644 * @return string formatted text to be echoed
645 */
646 protected function helper_submission_attachments($submissionid, $format = 'html') {
647 global $CFG;
648 require_once($CFG->libdir.'/filelib.php');
649
650 $fs = get_file_storage();
651 $ctx = $this->page->context;
652 $files = $fs->get_area_files($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid);
653
654 $outputimgs = ''; // images to be displayed inline
655 $outputfiles = ''; // list of attachment files
656
657 foreach ($files as $file) {
658 if ($file->is_directory()) {
659 continue;
660 }
661
662 $filepath = $file->get_filepath();
663 $filename = $file->get_filename();
664 $fileurl = file_encode_url($CFG->wwwroot . '/pluginfile.php',
665 '/' . $ctx->id . '/mod_workshop/submission_attachment/' . $submissionid . $filepath . $filename, true);
666 $type = $file->get_mimetype();
667 $type = mimeinfo_from_type('type', $type);
668 $image = html_writer::empty_tag('img', array('src'=>$this->output->pix_url(file_mimetype_icon($type)), 'alt'=>$type, 'class'=>'icon'));
669
670 $linkhtml = html_writer::link($fileurl, $image) . substr($filepath, 1) . html_writer::link($fileurl, $filename);
671 $linktxt = "$filename [$fileurl]";
672
673 if ($format == 'html') {
674 if (in_array($type, array('image/gif', 'image/jpeg', 'image/png'))) {
675 $preview = html_writer::empty_tag('img', array('src' => $fileurl, 'alt' => '', 'class' => 'preview'));
676 $preview = html_writer::tag('a', $preview, array('href' => $fileurl));
677 $outputimgs .= $this->output->container($preview);
678
679 } else {
680 $outputfiles .= html_writer::tag('li', $linkhtml, array('class' => $type));
681 }
682
683 } else if ($format == 'text') {
684 $outputfiles .= $linktxt . PHP_EOL;
685 }
686 }
687
688 if ($format == 'html') {
689 if ($outputimgs) {
690 $outputimgs = $this->output->container($outputimgs, 'images');
691 }
692
693 if ($outputfiles) {
694 $outputfiles = html_writer::tag('ul', $outputfiles, array('class' => 'files'));
695 }
696
697 return $this->output->container($outputimgs . $outputfiles, 'attachments');
698
699 } else {
700 return $outputfiles;
701 }
702 }
703
704 /**
705 * Renders the tasks for the single phase in the user plan
706 *
707 * @param stdClass $tasks
708 * @return string html code
709 */
710 protected function helper_user_plan_tasks(array $tasks) {
711 $out = '';
712 foreach ($tasks as $taskcode => $task) {
713 $classes = '';
714 $icon = null;
715 if ($task->completed === true) {
716 $classes .= ' completed';
717 } elseif ($task->completed === false) {
718 $classes .= ' fail';
719 } elseif ($task->completed === 'info') {
720 $classes .= ' info';
721 }
722 if (is_null($task->link)) {
723 $title = $task->title;
724 } else {
725 $title = html_writer::link($task->link, $task->title);
726 }
727 $title = $this->output->container($title, 'title');
728 $details = $this->output->container($task->details, 'details');
729 $out .= html_writer::tag('li', $title . $details, array('class' => $classes));
730 }
731 if ($out) {
732 $out = html_writer::tag('ul', $out, array('class' => 'tasks'));
733 }
734 return $out;
735 }
736
d10ee0be
DM
737 /**
738 * Renders a text with icons to sort by the given column
739 *
740 * This is intended for table headings.
741 *
742 * @param string $text The heading text
743 * @param string $sortid The column id used for sorting
744 * @param string $sortby Currently sorted by (column id)
745 * @param string $sorthow Currently sorted how (ASC|DESC)
746 *
747 * @return string
748 */
c2a35266 749 protected function helper_sortable_heading($text, $sortid=null, $sortby=null, $sorthow=null) {
d10ee0be
DM
750 global $PAGE;
751
26acc814 752 $out = html_writer::tag('span', $text, array('class'=>'text'));
d10ee0be
DM
753
754 if (!is_null($sortid)) {
d10ee0be 755 if ($sortby !== $sortid or $sorthow !== 'ASC') {
0a5ddea5
DM
756 $url = new moodle_url($PAGE->url);
757 $url->params(array('sortby' => $sortid, 'sorthow' => 'ASC'));
c63923bd 758 $out .= $this->output->action_icon($url, new pix_icon('t/up', get_string('sortasc', 'workshop')), null, array('class' => 'sort asc'));
d10ee0be
DM
759 }
760 if ($sortby !== $sortid or $sorthow !== 'DESC') {
0a5ddea5
DM
761 $url = new moodle_url($PAGE->url);
762 $url->params(array('sortby' => $sortid, 'sorthow' => 'DESC'));
c63923bd 763 $out .= $this->output->action_icon($url, new pix_icon('t/down', get_string('sortdesc', 'workshop')), null, array('class' => 'sort desc'));
d10ee0be
DM
764 }
765 }
766 return $out;
767}
768
29dc43e7 769 /**
5924db72 770 * @param stdClass $participant
29dc43e7
DM
771 * @param array $userinfo
772 * @return string
773 */
c2a35266 774 protected function helper_grading_report_participant(stdclass $participant, array $userinfo) {
29dc43e7 775 $userid = $participant->userid;
dd15f80e 776 $out = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 35));
26acc814 777 $out .= html_writer::tag('span', fullname($userinfo[$userid]));
29dc43e7
DM
778
779 return $out;
780 }
781
782 /**
5924db72 783 * @param stdClass $participant
29dc43e7
DM
784 * @return string
785 */
c2a35266 786 protected function helper_grading_report_submission(stdclass $participant) {
f55650e6
DM
787 global $CFG;
788
29dc43e7
DM
789 if (is_null($participant->submissionid)) {
790 $out = $this->output->container(get_string('nosubmissionfound', 'workshop'), 'info');
791 } else {
a6855934 792 $url = new moodle_url('/mod/workshop/submission.php',
0f4c64b7
PS
793 array('cmid' => $this->page->context->instanceid, 'id' => $participant->submissionid));
794 $out = html_writer::link($url, format_string($participant->submissiontitle), array('class'=>'title'));
29dc43e7
DM
795 }
796
797 return $out;
798 }
799
800 /**
5e71cefb 801 * @todo Highlight the nulls
5924db72 802 * @param stdClass|null $assessment
f55650e6
DM
803 * @param bool $shownames
804 * @param string $separator between the grade and the reviewer/author
29dc43e7
DM
805 * @return string
806 */
c2a35266 807 protected function helper_grading_report_assessment($assessment, $shownames, array $userinfo, $separator) {
407b1e91
DM
808 global $CFG;
809
29dc43e7 810 if (is_null($assessment)) {
5e71cefb 811 return get_string('nullgrade', 'workshop');
29dc43e7 812 }
7a789aa8 813 $a = new stdclass();
5e71cefb
DM
814 $a->grade = is_null($assessment->grade) ? get_string('nullgrade', 'workshop') : $assessment->grade;
815 $a->gradinggrade = is_null($assessment->gradinggrade) ? get_string('nullgrade', 'workshop') : $assessment->gradinggrade;
581878b8 816 $a->weight = $assessment->weight;
a8b6fc0e 817 // grrr the following logic should really be handled by a future language pack feature
29dc43e7 818 if (is_null($assessment->gradinggradeover)) {
a8b6fc0e
DM
819 if ($a->weight == 1) {
820 $grade = get_string('formatpeergrade', 'workshop', $a);
821 } else {
822 $grade = get_string('formatpeergradeweighted', 'workshop', $a);
823 }
29dc43e7
DM
824 } else {
825 $a->gradinggradeover = $assessment->gradinggradeover;
a8b6fc0e
DM
826 if ($a->weight == 1) {
827 $grade = get_string('formatpeergradeover', 'workshop', $a);
828 } else {
829 $grade = get_string('formatpeergradeoverweighted', 'workshop', $a);
830 }
29dc43e7 831 }
a6855934 832 $url = new moodle_url('/mod/workshop/assessment.php',
0f4c64b7
PS
833 array('asid' => $assessment->assessmentid));
834 $grade = html_writer::link($url, $grade, array('class'=>'grade'));
f55650e6
DM
835
836 if ($shownames) {
837 $userid = $assessment->userid;
dd15f80e 838 $name = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 16));
26acc814
PS
839 $name .= html_writer::tag('span', fullname($userinfo[$userid]), array('class' => 'fullname'));
840 $name = $separator . html_writer::tag('span', $name, array('class' => 'user'));
f55650e6 841 } else {
dd15f80e 842 $name = '';
f55650e6
DM
843 }
844
845 return $this->output->container($grade . $name, 'assessmentdetails');
846 }
29dc43e7 847
f55650e6
DM
848 /**
849 * Formats the aggreagated grades
850 */
c2a35266 851 protected function helper_grading_report_grade($grade, $over=null) {
7a789aa8 852 $a = new stdclass();
f55650e6
DM
853 $a->grade = is_null($grade) ? get_string('nullgrade', 'workshop') : $grade;
854 if (is_null($over)) {
855 $text = get_string('formataggregatedgrade', 'workshop', $a);
856 } else {
857 $a->over = is_null($over) ? get_string('nullgrade', 'workshop') : $over;
858 $text = get_string('formataggregatedgradeover', 'workshop', $a);
859 }
860 return $text;
29dc43e7
DM
861 }
862
5e71cefb 863 ////////////////////////////////////////////////////////////////////////////
c2a35266 864 // Static helpers
5e71cefb 865 ////////////////////////////////////////////////////////////////////////////
29dc43e7 866
38504a44
DM
867 /**
868 * Helper method dealing with the fact we can not just fetch the output of moodleforms
869 *
870 * @param moodleform $mform
871 * @return string HTML
872 */
873 protected static function moodleform(moodleform $mform) {
874
875 ob_start();
876 $mform->display();
877 $o = ob_get_contents();
878 ob_end_clean();
879
880 return $o;
881 }
882
29dc43e7
DM
883 /**
884 * Helper function returning the n-th item of the array
885 *
886 * @param array $a
887 * @param int $n from 0 to m, where m is th number of items in the array
888 * @return mixed the $n-th element of $a
889 */
890 protected static function array_nth(array $a, $n) {
891 $keys = array_keys($a);
892 if ($n < 0 or $n > count($keys) - 1) {
893 return null;
894 }
895 $key = $keys[$n];
896 return $a[$key];
897 }
898
d10ee0be
DM
899 /**
900 * Tries to guess the fullname format set at the site
901 *
902 * @return string fl|lf
903 */
904 protected static function fullname_format() {
7a789aa8 905 $fake = new stdclass(); // fake user
d10ee0be
DM
906 $fake->lastname = 'LLLL';
907 $fake->firstname = 'FFFF';
908 $fullname = get_string('fullnamedisplay', '', $fake);
909 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
910 return 'lf';
911 } else {
912 return 'fl';
913 }
914 }
66c9894d 915}