forked from learnweb/moodle-mod_ratingallocate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
787 lines (674 loc) · 33 KB
/
renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use ratingallocate\db as this_db;
/**
* @package mod
* @subpackage mod_ratingallocate
* @copyright 2014 M Schulze, T Reischmann, C Usener
* @copyright based on code by Stefan Koegel copyright (C) 2013 Stefan Koegel
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(dirname(__FILE__) . '/locallib.php');
class mod_ratingallocate_renderer extends plugin_renderer_base {
/**
* @var array rendered notifications to output for handle_view()
*/
private $notifications = array();
/**
* Render the header.
*
* @param ratingallocate_header $header
* @return string
*/
public function render_ratingallocate_header(ratingallocate_header $header) {
$o = '';
$this->page->set_heading($this->page->course->fullname);
$o .= $this->output->header();
$heading = format_string($header->ratingallocate->name, false, array('context' => $header->context));
$o .= $this->output->heading($heading);
if ($header->showintro) {
$introtext = format_module_intro('ratingallocate', $header->ratingallocate,
$header->coursemoduleid);
if ($introtext) {
$o .= $this->output->box_start('generalbox boxaligncenter', 'intro');
$o .= $introtext;
$o .= $this->output->box_end();
}
}
if (!empty($this->notifications)) {
$o .= $this->output->box_start('box generalbox boxaligncenter');
foreach ($this->notifications as $elem) {
$o .= html_writer::div(format_text($elem));
}
$o .= $this->output->box_end();
}
return $o;
}
public function render_ratingallocate_strategyform($mform) {
/* @var $mform ratingallocate_strategyform */
$o = '';
$o .= $this->heading(get_string('your_rating', ratingallocate_MOD_NAME), 2);
$o .= $this->format_text($mform->get_strategy_description_header() . '<br/>' . $mform->describe_strategy());
$o .= $mform->to_html();
return $o;
}
/**
* render current choice status
* @param ratingallocate_choice_status $status
* @return string
*/
public function render_ratingallocate_choice_status(ratingallocate_choice_status $status) {
$o = '';
$o .= $this->output->container_start('choicestatustable');
$o .= $this->output->heading(get_string('choicestatusheading', ratingallocate_MOD_NAME), 3);
$time = time();
$o .= $this->output->box_start('boxaligncenter choicesummarytable');
$t = new html_table();
$accesstimestart = $status->accesstimestart;
if ($accesstimestart > $time) {
// Access not yet available
$this->add_table_row_tuple($t, get_string('rating_begintime', ratingallocate_MOD_NAME), userdate($accesstimestart));
}
$duedate = $status->accesstimestop;
if ($duedate > 0) {
// Due date.
$this->add_table_row_tuple($t, get_string('rating_endtime', ratingallocate_MOD_NAME), userdate($duedate));
if($accesstimestart > 0 && $accesstimestart < $time) {
// Time remaining.
$row = new html_table_row();
$cell1 = new html_table_cell(get_string('timeremaining', ratingallocate_MOD_NAME));
if ($duedate - $time <= 0) {
$cell2 = new html_table_cell(get_string('rating_is_over', ratingallocate_MOD_NAME));
} else {
$cell2 = new html_table_cell(format_time($duedate - $time));
}
$row->cells = array(
$cell1,
$cell2
);
$t->data[] = $row;
}
}
if ($status->is_published && $status->publishdate) {
$this->add_table_row_tuple($t, get_string('publishdate', ratingallocate_MOD_NAME), userdate($status->publishdate));
} else if ($status->publishdate) {
$this->add_table_row_tuple($t, get_string('publishdate_estimated', ratingallocate_MOD_NAME), userdate($status->publishdate));
}
if ($status->show_distribution_info && $status->accesstimestop < $time) {
// Print algorithm status and last run time
if ($status->algorithmstarttime) {
$this->add_table_row_tuple($t, get_string('last_algorithm_run_date', ratingallocate_MOD_NAME), userdate($status->algorithmstarttime));
} else {
$this->add_table_row_tuple($t, get_string('last_algorithm_run_date', ratingallocate_MOD_NAME), "-");
}
$this->add_table_row_tuple($t, get_string('last_algorithm_run_status', ratingallocate_MOD_NAME),
get_string('last_algorithm_run_status_' . $status->algorithmstatus, ratingallocate_MOD_NAME));
}
// Print own choices or full list of available choices.
if (!empty($status->own_choices) && $status->show_user_info && $accesstimestart < $time) {
$row = new html_table_row();
$cell1 = new html_table_cell(get_string('your_rating', ratingallocate_MOD_NAME));
$choiceshtml = array();
foreach ($status->own_choices as $choice) {
array_push($choiceshtml, format_string($choice->title) .
' (' . s($this->get_option_title($choice->rating, $status->strategy)) . ')');
}
$cell2 = new html_table_cell(html_writer::alist($choiceshtml));
$row->cells = array(
$cell1,
$cell2
);
$t->data[] = $row;
} else if(!empty($status->available_choices)) {
$row = new html_table_row();
$cell1 = new html_table_cell(get_string('rateable_choices', ratingallocate_MOD_NAME));
$choiceshtml = array();
foreach ($status->own_choices as $choice) {
array_push($choiceshtml, format_string($choice->title));
}
$cell2 = new html_table_cell(html_writer::alist($choiceshtml));
$row->cells = array(
$cell1,
$cell2
);
$t->data[] = $row;
}
$has_rating = false;
// Check if the user has rated at least one choice.
foreach ($status->own_choices as $choice) {
if (object_property_exists($choice, 'ratingid') && $choice->ratingid != null) {
$has_rating = true;
break;
}
}
if ($status->is_published) {
if (!empty($status->allocations)) {
$row = new html_table_row();
$cell1 = new html_table_cell(
get_string('your_allocated_choice', ratingallocate_MOD_NAME));
$allocation_html = '';
foreach ($status->allocations as $allocation) {
$allocation_html .= html_writer::span(
format_string($allocation->{this_db\ratingallocate_choices::TITLE}),
'allocation tag tag-success');
$allocation_html .= '<br/>' . format_text($allocation->{this_db\ratingallocate_choices::EXPLANATION});
}
$cell2 = new html_table_cell($allocation_html);
$row->cells = array($cell1, $cell2);
$t->data[] = $row;
} else if (!empty($status->own_choices)) {
// Only print warning that user is not allocated if she has any rating.
if ($has_rating) {
$row = new html_table_row();
$cell1 = new html_table_cell(
get_string('your_allocated_choice', ratingallocate_MOD_NAME));
$allocation_html = html_writer::span(
get_string('you_are_not_allocated', ratingallocate_MOD_NAME),
'allocation tag tag-danger');
$cell2 = new html_table_cell($allocation_html);
$row->cells = array($cell1, $cell2);
$t->data[] = $row;
}
}
}
$o .= html_writer::table($t);
$o .= $this->output->box_end();
// Notifications if no choices exist or too few in comparison to strategy settings.
if (empty($status->available_choices)) {
$this->add_notification(get_string('no_choice_to_rate', ratingallocate_MOD_NAME));
} else if ($status->necessary_choices > count($status->available_choices)) {
if ($status->show_distribution_info) {
$this->add_notification(get_string('too_few_choices_to_rate', ratingallocate_MOD_NAME, $status->necessary_choices));
}
}
// To early to rate
if ($status->accesstimestart > $time) {
$this->add_notification(get_string('too_early_to_rate', ratingallocate_MOD_NAME), 'notifymessage');
}
// to late to rate
else if ($status->accesstimestop < $time) {
// if results already published
if ($status->is_published == true) {
if (count($status->allocations) > 0) {
$this->add_notification(get_string('rating_is_over_with_allocation', ratingallocate_MOD_NAME,
array_pop($status->allocations)->title), 'notifysuccess');
} else if ($has_rating) {
$this->add_notification(get_string('rating_is_over_no_allocation', ratingallocate_MOD_NAME),
'notifyproblem');
} else {
$this->add_notification(get_string('rating_is_over', ratingallocate_MOD_NAME),
'notifymessage');
}
} else {
$this->add_notification(get_string('results_not_yet_published', ratingallocate_MOD_NAME), 'notifymessage');
}
}
$o .= $this->output->container_end();
return $o;
}
public function format_text($text) {
$output = '';
$output .= $this->box_start();
$output .= format_text($text);
$output .= $this->box_end();
return $output;
}
/**
* Add a notification with the given $note to the renderer.
* This notification will be rendered in the header of the site.
* @param $note string to be viewed in the notification
* @param $classes string class for the formatting of the notification
*/
public function add_notification($note, $classes = 'notifyproblem') {
array_push($this->notifications, $this->notification($note, $classes));
}
/**
* Output the ratingallocate modfify choices links
*/
public function modify_choices_group($ratingallocateid, $coursemoduleid, $status) {
global $PAGE;
$output = '';
$output .= $this->heading(get_string('modify_choices_group', ratingallocate_MOD_NAME), 2);
$output .= $this->box_start();
$starturl = new moodle_url($PAGE->url->out(), array('action' => ACTION_SHOW_CHOICES));
// Get description dependent on status
$descriptionbaseid = 'modify_choices_group_desc_';
$description = get_string($descriptionbaseid.$status, ratingallocate_MOD_NAME);
$output .= $this->format_text($description);
$output .= html_writer::empty_tag('br', array());
$button = new single_button($starturl, get_string('modify_choices', ratingallocate_MOD_NAME), 'get');
$button->tooltip = get_string('modify_choices_explanation', ratingallocate_MOD_NAME);
$output .= $this->render($button);
$output .= $this->box_end();
return $output;
}
/**
* Output the ratingallocate modfify allocation
*/
public function modify_allocation_group($ratingallocateid, $coursemoduleid, $status, $algorithmstatus, $runalgorithmbycron) {
global $PAGE;
$output = '';
$output .= $this->heading(get_string('modify_allocation_group', ratingallocate_MOD_NAME), 2);
$output .= $this->box_start();
// The instance is called ready if it is in one of the two following status.
$ratingover = $status !== ratingallocate::DISTRIBUTION_STATUS_TOO_EARLY &&
$status !== ratingallocate::DISTRIBUTION_STATUS_RATING_IN_PROGRESS;
$starturl = new moodle_url($PAGE->url, array('action' => ACTION_START_DISTRIBUTION));
// Get description dependent on status
$descriptionbaseid = 'modify_allocation_group_desc_';
$description = get_string($descriptionbaseid.$status, ratingallocate_MOD_NAME);
$output .= $this->format_text($description);
$output .= html_writer::empty_tag('br', array());
$button = new single_button($starturl, get_string('start_distribution', ratingallocate_MOD_NAME), 'get');
// Enable only if the instance is ready and the algorithm may run manually
$button->disabled = !($ratingover);
$button->tooltip = get_string('start_distribution_explanation', ratingallocate_MOD_NAME);
$button->add_action(new confirm_action(get_string('confirm_start_distribution', ratingallocate_MOD_NAME)));
$output .= $this->render($button);
$output .= $this->single_button(new moodle_url('/mod/ratingallocate/view.php', array('id' => $coursemoduleid,
'action' => ACTION_MANUAL_ALLOCATION)), get_string('manual_allocation_form', ratingallocate_MOD_NAME), 'get',
array('disabled' => !$ratingover));
$output .= $this->box_end();
return $output;
}
/**
* Output the ratingallocate modfify allocation
*/
public function publish_allocation_group($ratingallocateid, $coursemoduleid, $status) {
$output = '';
$output .= $this->heading(get_string('publish_allocation_group', ratingallocate_MOD_NAME), 2);
$output .= $this->box_start();
$isready = $status === ratingallocate::DISTRIBUTION_STATUS_READY_ALLOC_STARTED;
// Get description dependent on status
$descriptionbaseid = 'publish_allocation_group_desc_';
$description = get_string($descriptionbaseid.$status, ratingallocate_MOD_NAME);
$output .= $this->format_text($description);
$output .= html_writer::empty_tag('br', array());
$output .= $this->single_button(new moodle_url('/mod/ratingallocate/view.php', array('id' => $coursemoduleid,
'ratingallocateid' => $ratingallocateid,
'action' => ACTION_PUBLISH_ALLOCATIONS)), get_string('publish_allocation', ratingallocate_MOD_NAME), 'get',
array('disabled' => !$isready));
$output .= $this->single_button(new moodle_url('/mod/ratingallocate/view.php', array('id' => $coursemoduleid,
'ratingallocateid' => $ratingallocateid,
'action' => ACTION_ALLOCATION_TO_GROUPING)), get_string('create_moodle_groups', ratingallocate_MOD_NAME), 'get');
$output .= $this->box_end();
return $output;
}
/**
* Output the ratingallocate modfify allocation
*/
public function reports_group($ratingallocateid, $coursemoduleid, $status, $context) {
global $PAGE;
$output = '';
$output .= $this->heading(get_string('reports_group', ratingallocate_MOD_NAME), 2);
$output .= $this->box_start();
$tableurl = new moodle_url($PAGE->url, array('action' => ACTION_SHOW_RATINGS_AND_ALLOCATION_TABLE));
// Button with link to display information about the allocations and ratings
$output .= $this->single_button($tableurl, get_string('show_table', ratingallocate_MOD_NAME), 'get');
$tableurl = new moodle_url($PAGE->url, array('action' => ACTION_SHOW_ALLOCATION_TABLE));
// Button with link to display information about the allocations and ratings
$output .= $this->single_button($tableurl, get_string('show_allocation_table', ratingallocate_MOD_NAME), 'get');
$tableurl = new moodle_url($PAGE->url, array('action' => ACTION_SHOW_STATISTICS));
// Buttton with link to display statistical information about the allocations
$output .= $this->single_button($tableurl, get_string('show_allocation_statistics', ratingallocate_MOD_NAME), 'get');
/* TODO: File not readable
$output .= html_writer::empty_tag('br', array());
if (has_capability('mod/ratingallocate:export_ratings', $context)) {
$output .= $this->action_link(new moodle_url('/mod/ratingallocate/solver/export_lp_solve.php', array('id' => $coursemoduleid,
'ratingallocateid' => $ratingallocateid)), get_string('download_problem_mps_format', ratingallocate_MOD_NAME));
}*/
$output .= $this->box_end();
return $output;
}
/**
* Shows table containing information about the result of the distribution algorithm.
*
* @return HTML code
*/
public function ratingallocate_show_choices_table(ratingallocate $ratingallocate, $choicesmodifiably) {
global $OUTPUT, $CFG, $PAGE;
require_once($CFG->libdir . '/tablelib.php');
$starturl = new moodle_url($PAGE->url->out(), array('action' => ACTION_EDIT_CHOICE));
// Set up the table.
echo $OUTPUT->single_button($starturl->out(), get_string('newchoice', 'mod_ratingallocate'), 'get');
$table = new \flexible_table('show_ratingallocate_options');
$table->define_baseurl($PAGE->url);
if ($choicesmodifiably) {
$table->define_columns(array('title', 'explanation', 'maxsize', 'active', 'tools'));
$table->define_headers(array(get_string('choice_table_title', 'mod_ratingallocate'),
get_string('choice_table_explanation', 'mod_ratingallocate'),
get_string('choice_table_maxsize', 'mod_ratingallocate'),
get_string('choice_table_active', 'mod_ratingallocate'),
get_string('choice_table_tools', 'mod_ratingallocate')));
} else {
$table->define_columns(array('title', 'explanation', 'maxsize', 'active'));
$table->define_headers(array(get_string('choice_table_title', 'mod_ratingallocate'),
get_string('choice_table_explanation', 'mod_ratingallocate'),
get_string('choice_table_maxsize', 'mod_ratingallocate'),
get_string('choice_table_tools', 'mod_ratingallocate')));
}
$table->set_attribute('id', 'mod_ratingallocateshowoptions');
$table->set_attribute('class', 'admintable generaltable');
$table->setup();
$choices = $ratingallocate->get_choices();
// When there are no choices, don't print the table.
if (count($choices) === 0) {
return;
}
foreach ($choices as $idx => $choice) {
$row = array();
$class = '';
$row[] = $choice->{this_db\ratingallocate_choices::TITLE};
$explanation = format_text($choice->{this_db\ratingallocate_choices::EXPLANATION});
$attachments = $ratingallocate->get_file_attachments_for_choice($choice->id);
if ($attachments) {
$explanation .= $this->render_attachments($attachments, true);
}
$row[] = $explanation;
$row[] = $choice->{this_db\ratingallocate_choices::MAXSIZE};
if ($choice->{this_db\ratingallocate_choices::ACTIVE}) {
$row[] = get_string('yes');
} else {
$row[] = get_string('no');
}
if ($choicesmodifiably) {
$row[] = $this->render_tools($idx, $choice->{this_db\ratingallocate_choices::ACTIVE},
$choice->{this_db\ratingallocate_choices::TITLE});
}
if (!$choice->{this_db\ratingallocate_choices::ACTIVE}) {
$class = 'dimmed_text';
}
$table->add_data($row, $class);
}
$table->finish_output();
}
/**
* Render file attachments for a certain choice entry
* @param array $files Array of file attachments
* @param bool $break Insert a line break on the first file attachment
* @return string HTML for the attachments
*/
public function render_attachments($files, $break=false) {
$entries = array();
foreach ($files as $f) {
$filename = $f->get_filename();
$url = moodle_url::make_pluginfile_url(
$f->get_contextid(),
$f->get_component(),
$f->get_filearea(),
$f->get_itemid(),
$f->get_filepath(),
$f->get_filename(),
false);
$a = array(
'href' => $url,
'title' => $filename,
);
$entry = '';
if (!$break) {
// Skip first line break; update flag for any subsequent attachments.
$break = true;
} else {
$entry .= html_writer::empty_tag('br');
}
$entry .= html_writer::start_tag('a', $a);
$entry .= $this->output->image_icon('t/right', $filename, 'moodle', array('title' => 'Download file'));
$entry .= $filename;
$entry .= html_writer::end_tag('a');
$entries[] = $entry;
}
return implode($entries);
}
/**
* Renders tools for a certain choice entry
* @param integer $id id of the choice
* @param boolean $active states if the choice is active
* @param string $title title of the choice
* @return string html of the tools for a specific choice
*/
private function render_tools($id, $active, $title) {
$tools = $this->format_icon_link(ACTION_EDIT_CHOICE, $id, 't/edit', get_string('edit_choice', ratingallocate_MOD_NAME));
if ($active) {
$tools .= $this->format_icon_link(ACTION_DISABLE_CHOICE, $id, 't/hide', get_string('disable'));
} else {
$tools .= $this->format_icon_link(ACTION_ENABLE_CHOICE, $id, 't/show', get_string('enable'));
}
$tools .= $this->format_icon_link(ACTION_DELETE_CHOICE, $id, 't/delete', get_string('delete_choice', ratingallocate_MOD_NAME),
new \confirm_action(get_string('deleteconfirm', ratingallocate_MOD_NAME, $title)));
return $tools;
}
/**
* Util function for writing an action icon link
*
* @param string $action URL parameter to include in the link
* @param string $choice URL parameter to include in the link
* @param string $icon The key to the icon to use (e.g. 't/up')
* @param string $alt The string description of the link used as the title and alt text
* @return string The icon/link
*/
private function format_icon_link($action, $choice, $icon, $alt, $confirm = null) {
global $OUTPUT, $PAGE;
$url = $PAGE->url;
return $OUTPUT->action_icon(new \moodle_url($url,
array('action' => $action, 'choiceid' => $choice, 'sesskey' => sesskey())),
new \pix_icon($icon, $alt, 'moodle', array('title' => $alt)),
$confirm , array('title' => $alt)) . ' ';
}
/**
* Finish the page (Since the header renders the notifications, it needs to be rendered after the actions)
*
* @return string
*/
public function render_header($ratingallocate, $context, $coursemodid) {
$headerinfo = new ratingallocate_header($ratingallocate, $context, true,
$coursemodid);
return $this->render($headerinfo);
}
/**
* Write the page footer
*
* @return string
*/
public function render_footer() {
global $OUTPUT;
return $OUTPUT->footer();
}
/**
* Shows table containing information about the result of the distribution algorithm.
*
* @return string html code representing the distribution table
*/
public function statistics_table_for_ratingallocate(ratingallocate $ratingallocate) {
// Count the number of allocations with a specific rating
$distributiondata = array();
$memberships = $ratingallocate->get_allocations();
foreach ($memberships as $id => $membership) {
$rating = $membership->rating;
if (key_exists($rating, $distributiondata)) {
$distributiondata[$rating] ++;
} else {
$distributiondata[$rating] = 1;
}
}
// get rating titles
$titles = $this->get_options_titles(array_keys($distributiondata),$ratingallocate);
// Although al indizes should be numeric or null,
// SORT_STRING cares for the correct comparison of null and 0
krsort($distributiondata, SORT_STRING);
$allocationrow = array();
$allocationhead = array();
foreach ($distributiondata as $rating => $count) {
$cell = new html_table_cell();
$cell->text = $count;
$cell->attributes['class'] = 'ratingallocate_rating_' . $rating;
$allocationrow[$rating] = $cell;
$cell = new html_table_cell();
$cell->text = $titles[$rating];
$allocationhead[$rating] = $cell;
}
$cell = new html_table_cell();
$usersinchoice = $ratingallocate->get_raters_in_course();
$cell->text = count($usersinchoice) - count($memberships);
$allocationrow[] = $cell;
$cell = new html_table_cell();
$cell->text = get_string('unassigned_users', ratingallocate_MOD_NAME);
$allocationhead[] = $cell;
$allocationtable = new html_table();
$allocationtable->data = array($allocationrow);
$allocationtable->head = $allocationhead;
$output = $this->heading(get_string('allocation_statistics', ratingallocate_MOD_NAME), 2);
$output .= $this->box_start();
// Get the number of users that have placed a vote.
$activeraters = $ratingallocate->get_number_of_active_raters();
$notrated = count($usersinchoice) - $activeraters;
if (count($distributiondata) == 0) {
$output .= $this->format_text(get_string('allocation_statistics_description_no_alloc',
ratingallocate_MOD_NAME,
array('notrated' => $notrated, 'rated' => $activeraters)));
} else {
$output .= $this->format_text(get_string('allocation_statistics_description', ratingallocate_MOD_NAME,
array('users' => $distributiondata[max(array_keys($distributiondata))],
'usersinchoice' => count($usersinchoice),
'total' => count($memberships),
'notrated' => $notrated,
'rated' => $activeraters,
'rating' => $titles[max(array_keys($distributiondata))],
'unassigned' => count($usersinchoice) - count($memberships))));
$output .= html_writer::table($allocationtable);
}
$output .= $this->box_end();
return $output;
}
/**
* Shows table containing information about the allocation of users.
*
* @return string html code representing ratings table
*/
public function allocation_table_for_ratingallocate($ratingallocate) {
// Create and set up the flextable for ratings and allocations.
$table = new mod_ratingallocate\allocations_table($ratingallocate);
$table->setup_table();
// The rest must be done through output buffering due to the way flextable works.
ob_start();
$table->build_table_by_sql();
$tableoutput = ob_get_contents();
ob_end_clean();
$output = $this->heading(get_string('allocations_table', ratingallocate_MOD_NAME), 2);
$output .= $this->format_text(get_string('allocation_table_description',
ratingallocate_MOD_NAME));
$output .= $this->box_start();
$output .= $this->box($tableoutput);
$output .= $this->box_end();
return $output;
}
/**
* Shows table containing information about the users' ratings
* and their distribution over the choices (allocations).
*
* @return string html code representing ratings table
*/
public function ratings_table_for_ratingallocate($choices, $ratings, $users, $memberships, $ratingallocate) {
// get rating titles
$titles = $this->get_options_titles(array_map(function($rating) {return $rating->rating;},$ratings), $ratingallocate);
// Create and set up the flextable for ratings and allocations.
$table = new mod_ratingallocate\ratings_and_allocations_table($this, $titles, $ratingallocate);
$table->setup_table($choices, false, false);
// The rest must be done through output buffering due to the way flextable works.
ob_start();
$table->build_table_by_sql($ratings, $memberships);
$tableoutput = ob_get_contents();
ob_end_clean();
$output = $this->heading(get_string('ratings_table', ratingallocate_MOD_NAME), 2);
$output .= $this->box_start();
$output .= $this->box($tableoutput);
$output .= $this->box_end();
return $output;
}
/**
* Formats the ratings
* @param unknown $ratings
* @return multitype:Ambigous <string, lang_string>
*/
private function get_options_titles($ratings, ratingallocate $ratingallocate){
$titles = array();
$unique_ratings = array_unique($ratings);
$options = $ratingallocate->get_options_titles($unique_ratings);
foreach ($options as $id => $option){
$titles[$id] = empty($option) ? get_string('no_rating_given', ratingallocate_MOD_NAME): get_string('rating_raw', ratingallocate_MOD_NAME, $option);
}
return $titles;
}
/**
* Formats the rating
* @param unknown $rating
* @return multitype:Ambigous <string, lang_string>
*/
private function get_option_title($rating, strategytemplate $strategy){
$option = $strategy->translate_rating_to_titles($rating);
return empty($option) ? get_string('no_rating_given', ratingallocate_MOD_NAME): get_string('rating_raw', ratingallocate_MOD_NAME, $option);
}
/**
* Format the users in the rating table
*/
public function format_user_data($data) {
global $CFG, $OUTPUT, $USER, $COURSE, $PAGE;
$output = '';
$output .= html_writer::start_tag('div', array('class' => 'ratingallocate_user'));
$output .= html_writer::start_tag('div', array('class' => 'name'));
$output .= fullname($data);
$output .= html_writer::end_tag('div');
$output .= html_writer::start_tag('div', array('class' => 'icons'));
if (has_capability('moodle/user:viewdetails', $PAGE->context)) {
$a = array();
$a['href'] = new moodle_url('/user/view.php', array('id' => $data->id, 'course' => $COURSE->id));
$a['title'] = get_string('viewprofile', 'core');
$output .= html_writer::start_tag('a', $a);
$src = array('src' => $OUTPUT->pix_url('i/user'), 'class' => 'icon', 'alt' => get_string('viewprofile', 'core'));
$output .= html_writer::empty_tag('img', $src);
$output .= html_writer::end_tag('a');
}
if ($CFG->messaging && has_capability('moodle/site:sendmessage', $PAGE->context) && $data->id != $USER->id) {
$a = array();
$a['href'] = new moodle_url('/message/index.php', array('id' => $data->id));
$a['title'] = get_string('sendmessageto', 'core_message', fullname($data));
$output .= html_writer::start_tag('a', $a);
$src = array('src' => $OUTPUT->pix_url('t/email'), 'class' => 'icon');
$src['alt'] = get_string('sendmessageto', 'core_message', fullname($data));
$output .= html_writer::empty_tag('img', $src);
$output .= html_writer::end_tag('a');
}
$output .= html_writer::end_tag('div');
$output .= html_writer::end_tag('div');
return $output;
}
/**
* Utility function to add a row of data to a table with 2 columns. Modified
* the table param and does not return a value
*
* @param html_table $table The table to append the row of data to
* @param string $first The first column text
* @param string $second The second column text
* @return void
*/
private function add_table_row_tuple(html_table $table, $first, $second) {
$row = new html_table_row();
$cell1 = new html_table_cell($first);
$cell2 = new html_table_cell($second);
$row->cells = array($cell1, $cell2);
$table->data[] = $row;
}
}