forked from studentquiz/moodle-mod_studentquiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
2486 lines (2248 loc) · 99.9 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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* Defines the renderers for the StudentQuiz module.
*
* @package mod_studentquiz
* @copyright 2017 HSR (http://www.hsr.ch)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_studentquiz\commentarea\container;
use mod_studentquiz\local\studentquiz_helper;
use mod_studentquiz\utils;
use mod_studentquiz\local\studentquiz_question;
use mod_studentquiz\question\bank\studentquiz_bank_view_pre_43;
use mod_studentquiz\question\bank\studentquiz_bank_view;
defined('MOODLE_INTERNAL') || die();
/**
* Base renderer for Studentquiz with helpers
*
* @package mod_studentquiz
* @copyright 2017 HSR (http://www.hsr.ch)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_studentquiz_renderer extends plugin_renderer_base {
/**
* Cached question preview link image.
* @var mixed
*/
protected $cachedquestionpreviewlinkimage;
/**
* Render data into table while enriching them with attributes.
*
* @param array $celldata
* @param array $rowattributes
* @return html_table_row[]
*/
public function render_table_data(array $celldata, array $rowattributes=array()) {
$rows = array();
foreach ($celldata as $num => $row) {
$cells = array();
foreach ($row as $cell) {
if (!empty($rowattributes[$num])) {
$cells[] = $this->render_table_cell($cell, $rowattributes[$num]);
} else {
$cells[] = $this->render_table_cell($cell);
}
}
$rows[] = $this->render_table_row($cells);
}
return $rows;
}
/**
* Render custom error message, so we can write the behat test for it.
*
* @param string $errormessage Error message.
* @param string $title Page's title.
* @return void
*/
public function render_error_message(string $errormessage, string $title) : void {
if ($title) {
$this->page->set_title($title);
}
// We need to remove settings menu for view page because we are using custom error message.
if ($settingmenu = $this->page->settingsnav->find('modulesettings', \navigation_node::TYPE_SETTING)) {
$settingmenu->remove();
}
echo $this->output->header();
echo $this->output->notification($errormessage, 'error', false);
$courseurl = new moodle_url('/course/view.php', ['id' => $this->page->course->id]);
$backtocourse = new single_button($courseurl, get_string('back_to_course_button', 'studentquiz'),
'get', 'primary');
echo html_writer::div($this->render($backtocourse), 'studentquizerrormessage');
echo $this->output->footer();
}
/**
* Render one table cell.
*
* @param string $text
* @param array $attributes
* @return html_table_cell
*/
public function render_table_cell($text, array $attributes=array()) {
$cell = new html_table_cell();
$cell->text = $text;
if (!empty($attributes)) {
$cell->attributes = $attributes;
}
return $cell;
}
/**
* Render stat block.
*
* @param mixed $report
* @return block_content
*/
public function render_stat_block($report) {
// TODO: Refactor: use mod_studentquiz_report_record_type!
$userstats = $report->get_user_stats();
$sqstats = $report->get_studentquiz_stats();
$cmid = $report->get_cm_id();
if (!$userstats) {
$bc = new block_contents();
$bc->attributes['id'] = 'mod_studentquiz_statblock';
$bc->attributes['role'] = 'navigation';
$bc->attributes['aria-labelledby'] = 'mod_studentquiz_navblock_title';
$bc->title = get_string('statistic_block_title', 'studentquiz');
$bc->content = get_string('please_enrole_message', 'studentquiz');
return $bc;
}
$bc = new block_contents();
$bc->attributes['id'] = 'mod_studentquiz_statblock';
$bc->attributes['role'] = 'navigation';
$bc->attributes['aria-labelledby'] = 'mod_studentquiz_navblock_title';
$bc->title = get_string('statistic_block_title', 'studentquiz');
$info1 = new stdClass();
$info1->total = $sqstats->questions_available;
$info1->group = $userstats->last_attempt_exists;
$info1->one = $userstats->last_attempt_correct;
$info2 = new stdClass();
$info2->total = $userstats->questions_created;
$info2->group = $userstats->questions_approved + $userstats->questions_disapproved;
$info2->one = $userstats->questions_approved;
$unansweredquestions = $sqstats->questions_available - $userstats->last_attempt_exists;
$bc->content = html_writer::div($this->render_progress_bar($info1), '', array('style' => 'width:inherit'))
. html_writer::div(
get_string('statistic_block_progress_last_attempt_correct', 'studentquiz')
.html_writer::span($userstats->last_attempt_correct, '', ['class' => "stat badge last-attempt-correct"]))
. html_writer::div(
get_string('statistic_block_progress_last_attempt_incorrect', 'studentquiz')
.html_writer::span($userstats->last_attempt_incorrect, '', ['class' => 'stat badge last-attempt-incorrect']))
. html_writer::div(
get_string('statistic_block_progress_never', 'studentquiz')
.html_writer::span($unansweredquestions, '', ['class' => 'stat badge never-answered']))
. html_writer::div($this->render_progress_bar($info2), '', array('style' => 'width:inherit'))
. html_writer::div(get_string('statistic_block_approvals', 'studentquiz')
. html_writer::span($userstats->questions_approved, '', ['class' => 'stat badge approvals']))
. html_writer::div(get_string('statistic_block_disapprovals', 'studentquiz')
. html_writer::span($userstats->questions_disapproved, '', ['class' => 'stat badge disapprovals']))
. html_writer::div(get_string('statistic_block_new_changed', 'studentquiz')
. html_writer::span(
($userstats->questions_created - $userstats->questions_approved - $userstats->questions_disapproved),
'', ['class' => 'stat badge changed']));
// Add More link to Stat block.
$reporturl = new moodle_url('/mod/studentquiz/reportstat.php', ['id' => $cmid]);
$readmorelink = $this->render_report_more_link($reporturl);
$bc->content .= $readmorelink;
return $bc;
}
/**
* Render ranking block.
*
* @param mixed $report
* @return block_content
*/
public function render_ranking_block($report) {
$ranking = $report->get_user_ranking_table(0, 10);
$currentuserid = $report->get_user_id();
$anonymname = get_string('creator_anonym_fullname', 'studentquiz');
$anonymise = $report->is_anonymized();
$studentquiz = mod_studentquiz_load_studentquiz($report->get_cm_id(), $this->page->context->id);
// We need to check this instead of using $report->is_anonymized()
// because we want to apply this text regardless of role.
$blocktitle = $studentquiz->anonymrank ? get_string('ranking_block_title_anonymised', 'studentquiz') :
get_string('ranking_block_title', 'studentquiz');
$cmid = $report->get_cm_id();
$rows = array();
$rank = 1;
foreach ($ranking as $row) {
if ($currentuserid == $row->userid || !$anonymise) {
$author = user_get_users_by_id(array($row->userid))[$row->userid];
$name = html_writer::link(utils::get_user_profile_url($author->id, $this->page->course->id), fullname($author));
} else {
$name = $anonymname;
}
$rankname = \html_writer::div($rank . '. ' . $name);
$rows[] = \html_writer::div($rankname .
html_writer::span(html_writer::tag('b' , round($row->points)),
'', array('style' => 'float: right;')));
$rank++;
if ($rank > 10) {
break;
}
}
$ranking->close();
$bc = new block_contents();
$bc->attributes['id'] = 'mod_studentquiz_rankingblock';
$bc->attributes['role'] = 'navigation';
$bc->attributes['aria-labelledby'] = 'mod_studentquiz_navblock_title';
$bc->title = $blocktitle;
$bc->content = implode('', $rows);
// Add More link to Ranking block.
$reporturl = new moodle_url('/mod/studentquiz/reportrank.php', ['id' => $cmid]);
$readmorelink = $this->render_report_more_link($reporturl);
$bc->content .= $readmorelink;
return $bc;
}
/**
* Render table row.
*
* @param array $cells
* @return html_table_row
*/
public function render_table_row($cells) {
$row = new html_table_row();
$row->cells = $cells;
return $row;
}
/**
* Render table with options.
*
* @param array $data 2-level array of content data
* @param array $size
* @param array $align
* @param array $head
* @param string $caption
* @param string $class
* @return string
*/
public function render_table($data, $size, $align, $head, $caption, $class='') {
$table = new html_table();
if (!empty($caption)) {
$table->caption = $caption;
}
$table->head = $head;
$table->align = $align;
$table->size = $size;
$table->data = $data;
$table->attributes['class'] = $class;
return html_writer::table($table);
}
/**
* Return a svg representing a progress bar filling 100% of is containing element.
*
* @param stdClass $info total, group, one
* @param string $texttotal text to be displayed in the center of the bar
* @param bool $bicolor only bicolor color scheme
* @return string
*/
public function render_progress_bar($info, $texttotal=null, $bicolor=false) {
// Check input.
$validinput = true;
if (!isset($info->total)) {
$validinput = false;
}
if (!isset($info->group)) {
$validinput = false;
}
if (!isset($info->one)) {
$validinput = false;
}
// Stylings.
$rgbstroke = 'rgb(200, 200, 200)';
$rgbyellow = 'rgb(255, 193, 7)';
$rgbgreen = 'rgb(40, 167, 69)';
$rgbblue = 'rgb(2, 117, 216)';
$rgbred = 'rgb(220, 53, 69)';
$rgbgrey = 'rgb(200, 200, 200)';
$barstroke = 'stroke-width:0.1;stroke:' . $rgbstroke .';';
$svgdims = array('width' => '100%', 'height' => 20);
$bardims = array('height' => '100%', 'rx' => 5, 'ry' => 5);
$idblue = 'blue';
$idgreen = 'green';
$idred = 'red';
$gradientdims = array('cx' => '50%', 'cy' => '50%', 'r' => '50%', 'fx' => '50%', 'fy' => '50%');
$stopcolorgreen = html_writer::tag('stop', null,
array('offset' => '100%', 'style' => 'stop-color:' . $rgbgreen . ';stop-opacity:1'));
$stopcolorred = html_writer::tag('stop', null,
array('offset' => '100%', 'style' => 'stop-color:' . $rgbred . ';stop-opacity:1'));
$stopcolorblue = html_writer::tag('stop', null,
array('offset' => '100%', 'style' => 'stop-color:' . $rgbblue . ';stop-opacity:1'));
$gradientblue = html_writer::tag('radialGradient', $stopcolorblue . $stopcolorblue,
array_merge($gradientdims, array('id' => $idblue)));
$gradientred = html_writer::tag('radialGradient', $stopcolorred . $stopcolorred,
array_merge($gradientdims, array('id' => $idred)));
$gradientgreen = html_writer::tag('radialGradient', $stopcolorgreen . $stopcolorgreen,
array_merge($gradientdims, array('id' => $idgreen)));
$gradients = array($gradientred, $gradientgreen, $gradientblue);
$defs = html_writer::tag('defs', implode($gradients));
// Background bar.
if ($bicolor) {
$barbackground = html_writer::tag('rect', null, array_merge($bardims,
array('width' => '100%', 'style' => $barstroke . 'fill:' . $rgbgrey )));
} else {
$barbackground = html_writer::tag('rect', null, array_merge($bardims,
array('width' => '100%', 'style' => $barstroke . 'fill:' . $rgbyellow)));
}
// Return empty bar if no questions are in StudentQuiz.
if (!$validinput || $info->total <= 0) {
return html_writer::tag('svg', $barbackground, $svgdims);
}
// Calculate Percentages to display.
$percentgroup = round(100 * ($info->group / $info->total));
$percentone = round(100 * ($info->one / $info->total));
if (!empty($texttotal)) {
$text = html_writer::tag('text', $texttotal, array('xml:space' => 'preserve', 'text-anchor' => 'start',
'font-family' => 'Helvetica, Arial, sans-serif', 'font-size' => '12', 'font-weight' => 'bold',
'id' => 'svg_text', 'x' => '50%', 'y' => '50%', 'alignment-baseline' => 'middle',
'text-anchor' => 'middle', 'stroke-width' => '0', 'stroke' => '#000', 'fill' => '#000'));
} else {
$text = '';
}
// Return stacked bars.
$bars = array($barbackground);
if ($bicolor) {
$bars[] = html_writer::tag('rect', null, array_merge($bardims,
array('width' => $percentone . '%', 'style' => $barstroke . 'fill:url(#' . $idblue .')')));
} else {
$bars[] = html_writer::tag('rect', null, array_merge($bardims,
array('width' => $percentgroup . '%', 'style' => $barstroke . 'fill:url(#' . $idred .')')));
$bars[] = html_writer::tag('rect', null, array_merge($bardims,
array('width' => $percentone . '%', 'style' => $barstroke . 'fill:url(#' . $idgreen .')')));
}
return html_writer::tag('svg', $defs . implode($bars) . $text, $svgdims);
}
/**
* Prints the error message.
*
* @param string $errormessage string error message
* @return string error as HTML
*/
public function show_error($errormessage) {
return html_writer::div($errormessage, 'error');
}
/**
* Render the content of creator column.
*
* @param bool $anonymize
* @param stdClass $question
* @param int $currentuserid
* @param string $anonymousname
* @param array $rowclasses
* @return string
*/
public function render_anonym_creator_name_column($anonymize, $question, $currentuserid, $anonymousname, $rowclasses) {
$output = '';
$date = userdate($question->timecreated, get_string('strftimedatetime', 'langconfig'));
if ($anonymize && $question->createdby != $currentuserid) {
$output .= html_writer::tag('span', $anonymousname);
$output .= html_writer::empty_tag('br');
$output .= html_writer::tag('span', $date, ['class' => 'date']);
} else {
$author = core_user::get_user($question->createdby);
if ($author) {
$userprofilelink = html_writer::link(utils::get_user_profile_url($author->id,
$this->page->course->id), fullname($author));
$output .= html_writer::tag('span', $userprofilelink);
} else {
// Cannot find the user. Leave it blank.
$output .= html_writer::tag('span', '');
}
$output .= html_writer::empty_tag('br');
$output .= html_writer::tag('span', $date, ['class' => 'date']);
}
return $output;
}
/**
* Render the content of approve column.
*
* @param stdClass $question
* @param array $rowclasses
* @return string
*/
public function render_state_column($question, $rowclasses) {
global $COURSE;
// Moodle doesn't process "empty" objects in restore. So questions from older backups can have no question state
// assigned. Need to figure out for the calculation, if it's fine to handle them just as new or if the question
// table has to have an entry. Ref: https://github.com/frankkoch/moodle-mod_studentquiz/issues/172.
if (is_null($question->state) || $question->state === "") {
$question->state = studentquiz_helper::STATE_NEW;
}
if (!in_array(intval($question->state), array(
studentquiz_helper::STATE_DISAPPROVED,
studentquiz_helper::STATE_APPROVED,
studentquiz_helper::STATE_NEW,
studentquiz_helper::STATE_CHANGED,
studentquiz_helper::STATE_REVIEWABLE,
))) {
throw new coding_exception('Invalid question state '.$question->state.' for question id '.$question->id.'');
}
$statename = studentquiz_helper::$statename[intval($question->state)];
$title = get_string('state_change_tooltip_'.$statename, 'studentquiz');
$content = $this->output->pix_icon('state_'.$statename, '', 'studentquiz');
if (has_capability('mod/studentquiz:changestate', $this->page->context)) {
$changestateurl = new \moodle_url('/mod/studentquiz/changestate.php', ['courseid' => $COURSE->id,
'approveselected' => $question->id,
'q' . $question->id => 1,
'sesskey' => sesskey(),
'returnurl' => $this->page->url,
'cmid' => $this->page->cm->id]);
$content = html_writer::link($changestateurl, $content, ['title' => $title]);
}
return $content;
}
/**
* Render the content of state pin column.
*
* @param stdClass $question The row from the $question table, augmented with extra information.
* @return string The html string.
*/
public function render_state_pin($question): string {
$content = '';
if ($question->pinned) {
$content = $this->output->pix_icon('i/pinned', get_string('state_pinned', 'studentquiz'), 'mod_forum');
}
return $content;
}
/**
* Render the content of comment column.
*
* @param stdClass $question
* @param array $rowclasses
* @param bool $privatecommenting Does this studentquiz enable private commenting?
* @return string
*/
public function render_comment_column($question, $rowclasses, $privatecommenting = false) {
$publiccontext = [
'tooltiptext' => get_string('commentcolumnexplainpublic', 'studentquiz'),
'sronlytext' => get_string('public', 'studentquiz') . ' ' .
utils::get_comment_plural_text($question->publiccomment),
'class' => 'public-comment'
];
if (!empty($question->publiccomment)) {
$publiccontext['numberofcomments'] = $question->publiccomment;
if ($question->lasteditpubliccomment > $question->lastreadpubliccomment) {
$publiccontext['sronlytext'] .= get_string('includingunread', 'studentquiz');
$publiccontext['unread'] = true;
}
} else {
$publiccontext['numberofcomments'] = get_string('no_comment', 'studentquiz');
}
$publiccomment = $this->render_from_template('mod_studentquiz/questionbank_comment_badge', $publiccontext);
$privatecomment = '';
if (utils::can_view_private_comment($this->page->cm->id, $question, $privatecommenting)) {
$privatecontext = [
'tooltiptext' => get_string('commentcolumnexplainprivate', 'studentquiz'),
'sronlytext' => get_string('private', 'studentquiz') . ' ' .
utils::get_comment_plural_text($question->privatecomment),
'class' => 'private-comment'
];
if (!empty($question->privatecomment)) {
$privatecontext['numberofcomments'] = $question->privatecomment;
if ($question->lasteditprivatecomment > $question->lastreadprivatecomment) {
$privatecontext['sronlytext'] .= get_string('includingunread', 'studentquiz');
$privatecontext['unread'] = true;
}
} else {
$privatecontext['numberofcomments'] = get_string('no_comment', 'studentquiz');
}
$privatecomment = ' | ' . $this->render_from_template('mod_studentquiz/questionbank_comment_badge',
$privatecontext);
}
return $publiccomment . $privatecomment;
}
/**
* Render the content of difficulty level column.
* The svg image is renderer later using javascript.
* See render_bar_javascript_snippet()
*
* @param stdClass $question
* @param array $rowclasses
* @return string
*/
public function render_difficulty_level_column($question, $rowclasses) {
$nodifficultylevel = get_string('no_difficulty_level', 'studentquiz');
$difficultytitle = get_string('difficulty_all_column_name', 'studentquiz');
$mydifficultytitle = get_string('mydifficulty_column_name', 'studentquiz');
$title = "";
if (!empty($question->difficultylevel) || !empty($question->mydifficulty)) {
if (!empty($question->difficultylevel)) {
$title = $difficultytitle . ': ' . (100 * round($question->difficultylevel, 2)) . '% ';
}
if (!empty($question->mydifficulty)) {
$title .= ', ' . $mydifficultytitle . ': ' . (100 * round($question->mydifficulty, 2)) . '%';
} else {
$title .= ', ' . $mydifficultytitle . ': ' . $nodifficultylevel;
}
}
$output = html_writer::tag("span", "",
array(
"class" => "mod_studentquiz_difficulty",
"data-difficultylevel" => $question->difficultylevel,
"data-mydifficulty" => $question->mydifficulty,
"title" => $title
));
return $output;
}
/**
* Render the difficulty bar.
*
* @param float $average
* @param float $mine
* @param string $fillboltson
* @param string $fillboltsoff
* @param string $fillbaron
* @param string $fillbaroff
* @return string
*/
public function render_difficultybar($average, $mine, $fillboltson = '#ffc107', $fillboltsoff = '#fff', $fillbaron = '#fff',
$fillbaroff = '#007bff') {
$output = '';
$mine = floatval($mine);
$average = floatval($average);
if ($average > 0 && $average <= 1) {
$width = round($average * 100, 0);
} else {
$width = 0;
}
if ($mine > 0 && $mine <= 1) {
$bolts = ceil($mine * 5);
} else {
$bolts = 0;
}
$output .= html_writer::start_tag('svg', [
'width' => 101,
'height' => 21,
'xmlns' => 'http://www.w3.org/2000/svg'
]);
$output .= html_writer::tag('svg', html_writer::tag('title', get_string('difficulty_title', 'studentquiz')));
$output .= html_writer::start_tag('g');
$output .= $this->render_fill_bar('svg_6', $fillbaron);
$output .= $this->render_fill_bar('svg_7', $fillbaroff, $width);
$boltpath = ',1.838819l3.59776,4.98423l-1.4835,0.58821l4.53027,4.2704l-1.48284,0.71317l5.60036,7.15099l-9.49921,'
. '-5.48006l1.81184,-0.76102l-5.90211,-3.51003l2.11492,-1.08472l-6.23178,-3.68217l6.94429,-3.189z';
for ($i = 1; $i <= $bolts; $i++) {
$output .= $this->render_fill_bolt($fillboltson, $i, $boltpath, $fillboltson);
}
for ($i = $bolts + 1; $i <= 5; $i++) {
$output .= $this->render_fill_bolt('#868e96', $i, $boltpath, $fillboltsoff);
}
$output .= html_writer::end_tag('g');
$output .= html_writer::end_tag('svg');
return $output;
}
/**
* Render the content of attempts column.
*
* @param stdClass $question
* @param array $rowclasses
* @return string
*/
public function render_attempts_column($question, $rowclasses) {
$output = '';
$attrs = ['tabindex' => 0];
if (!empty($question->myattempts)) {
$output .= $question->myattempts;
} else {
$output .= get_string('no_myattempts', 'studentquiz');
}
$output .= ' | ';
if (!empty($question->myattempts) && $question->mylastanswercorrect !== null) {
// TODO: Refactor magic constant.
if ($question->mylastanswercorrect == '1') {
$output .= get_string('lastattempt_right', 'studentquiz');
$attrs['aria-label'] = get_string('lastattempt_right_label', 'studentquiz');
} else {
$output .= get_string('lastattempt_wrong', 'studentquiz');
$attrs['aria-label'] = get_string('lastattempt_wrong_label', 'studentquiz');
}
} else {
$output .= get_string('no_mylastattempt', 'studentquiz');
$attrs['aria-label'] = get_string('no_mylastattempt_label', 'studentquiz');
}
return html_writer::span($output, 'pratice_info', $attrs);
}
/**
* Render the content of rate column.
* The svg image is renderer later using javascript.
* See render_bar_javascript_snippet()
*
* @param stdClass $question
* @param array $rowclasses
* @return string
*/
public function render_rate_column($question, $rowclasses) {
$myratingtitle = get_string('myrate_column_name', 'studentquiz');
$ratingtitle = get_string('rate_all_column_name', 'studentquiz');
$notavailable = get_string('no_rates', 'studentquiz');
$title = "";
if (!empty($question->rate) || !empty($question->myrate)) {
$title = $ratingtitle . ': ' . round($question->rate, 2) . ' ';
if (!empty($question->myrate)) {
$title .= ', ' . $myratingtitle . ': ' . round($question->myrate, 2);
} else {
$title .= ', ' . $myratingtitle . ': ' . $notavailable;
}
}
$output = html_writer::tag("span", "",
array(
"class" => "mod_studentquiz_ratingbar",
"data-rate" => $question->rate,
"data-myrate" => $question->myrate,
"title" => $title
));
return $output;
}
/**
* Renders a svg bar.
*
* @param float $average float between 1 to 5 for backgroud bar.
* @param float $mine between 1 to 5 for number of stars to be yellow
* @param string $fillstarson
* @param string $fillstarsoff
* @param string $fillbaron
* @param string $fillbaroff
*/
public function render_ratingbar($average, $mine, $fillstarson = '#ffc107', $fillstarsoff = '#fff', $fillbaron = '#fff',
$fillbaroff = '#007bff') {
$output = '';
$mine = intval($mine);
$average = floatval($average);
if ($average > 0 && $average <= 5) {
$width = round($average * 20, 0);
} else {
$width = 1;
}
if ($mine > 0 && $mine <= 5) {
$stars = $mine;
} else {
$stars = 0;
}
$output .= html_writer::start_tag('svg', [
'width' => 101,
'height' => 21,
'xmlns' => 'http://www.w3.org/2000/svg'
]);
$output .= html_writer::tag('svg', html_writer::tag('title', get_string('ratingbar_title', 'studentquiz')));
$output .= html_writer::start_tag('g');
$output .= $this->render_fill_bar('svg_6', $fillbaron);
$output .= $this->render_fill_bar('svg_7', $fillbaroff, $width);
$starpath = ',8.514401l5.348972,0l1.652874,-5.081501l1.652875,5.081501l5.348971,0l-4.327402,3.140505l1.652959,'
.'5.081501l-4.327403,-3.14059l-4.327402,3.14059l1.65296,-5.081501l-4.327403,-3.140505z';
for ($i = 1; $i <= $stars; $i++) {
$output .= $this->render_fill_star('#000', $i, $starpath, $fillstarson);
}
for ($i = $stars + 1; $i <= 5; $i++) {
$output .= $this->render_fill_star('#868e96', $i, $starpath, $fillstarsoff);
}
$output .= html_writer::end_tag('g');
$output .= html_writer::end_tag('svg');
return $output;
}
/**
* Render the content of tag column.
*
* @param stdClass $question
* @param array $rowclasses
* @return string
*/
public function render_tag_column($question, $rowclasses) {
$output = '';
if (!empty($question->tagarray)) {
foreach (explode(',', $question->tagarray) as $tag) {
$tag = $this->render_tag($tag);
$output .= $tag;
}
} else {
$output .= get_string('no_tags', 'studentquiz');
}
return $output;
}
/**
* Render tag element.
*
* @param string $tag
* @return string
*/
public function render_tag($tag) {
$output = html_writer::tag('span', $tag, [
'role' => 'listitem',
'data-value' => 'HELLO',
'aria-selected' => 'true',
'class' => 'tag tag-success text-truncate'
]);
$output .= ' ';
return $output;
}
/**
* Render fill bar.
*
* @param string $id
* @param string $fill
* @param int $width
* @return string
*/
public function render_fill_bar($id, $fill, $width = 100) {
$output = '';
$output .= html_writer::empty_tag('rect', [
'id' => $id,
'height' => 20,
'width' => $width,
'x' => 0.396847,
'y' => 0.397703,
'rx' => 5,
'ry' => 5,
'fill-opacity' => null,
'stroke-opacity' => null,
'stroke-width' => 0.5,
'stroke' => '#868e96',
'fill' => $fill
]);
return $output;
}
/**
* Render bolt icon.
*
* @param string $stroke
* @param string $id
* @param string $boltpath
* @param string $fill
* @return string
*/
public function render_fill_bolt($stroke, $id, $boltpath, $fill) {
$output = '';
$output .= html_writer::empty_tag('path', [
'stroke' => $stroke,
'id' => 'svg_' . $id,
'd' => 'm' . (($id * 20) - 12) . $boltpath,
'stroke-width' => 0.5,
'fill' => $fill
]);
return $output;
}
/**
* Render start icon.
*
* @param string $stroke
* @param string $id
* @param string $starpath
* @param string $fill
* @return string
*/
public function render_fill_star($stroke, $id, $starpath, $fill) {
$output = '';
$output .= html_writer::empty_tag('path', [
'stroke' => $stroke,
'id' => 'svg_' . $id,
'd' => 'm' . (($id * 20) - 15) . $starpath,
'stroke-width' => 0.5,
'fill' => $fill
]);
return $output;
}
/**
* Render the content of question name column.
*
* @param stdClass $question
* @param array $rowclasses
* @param string $labelfor
* @return string
*/
public function render_question_name_column($question, $rowclasses, $labelfor) {
$output = '';
if ($labelfor) {
$output .= html_writer::start_tag('label', ['for' => $labelfor]);
}
$output .= format_string($question->name);
if ($labelfor) {
$output .= html_writer::end_tag('label');
}
return $output;
}
/**
* Get sortable fields for difficulty level column.
*
* @return array
*/
public function get_is_sortable_difficulty_level_column() {
return [
'difficulty' => [
'field' => 'dl.difficultylevel',
'title' => get_string('average_column_name', 'studentquiz'),
'tip' => get_string('average_column_name', 'studentquiz')
],
'mydifficulty' => [
'field' => 'mydifficulty',
'title' => get_string('mine_column_name', 'studentquiz'),
'tip' => get_string('mine_column_name', 'studentquiz')
]
];
}
/**
* Get sortable fields for rate column.
*
* @return array
*/
public function get_is_sortable_rate_column() {
return [
'rate' => [
'field' => 'vo.rate',
'title' => get_string('average_column_name', 'studentquiz'),
'tip' => get_string('average_column_name', 'studentquiz')
],
'myrate' => [
'field' => 'myrate.myrate',
'title' => get_string('mine_column_name', 'studentquiz'),
'tip' => get_string('mine_column_name', 'studentquiz')
]
];
}
/**
* Get report read more link.
*
* @param moodle_url $url Url to the report.
* @return string Html string of read more link.
*/
public function render_report_more_link($url) {
$output = html_writer::start_div('report_more_url');
$output .= html_writer::link($url, get_string('more', 'studentquiz'));
$output .= html_writer::end_div();
return $output;
}
/**
* Get all the required columns for StudentQuiz view.
*
* @param studentquiz_bank_view $view
* @return array
*/
public function get_columns_for_question_bank_view(studentquiz_bank_view $view) {
return [
new core_question\local\bank\checkbox_column($view),
new qbank_viewquestiontype\question_type_column($view),
new \mod_studentquiz\question\bank\state_column($view),
new \mod_studentquiz\question\bank\state_pin_column($view),
new \mod_studentquiz\question\bank\question_name_column($view),
new \mod_studentquiz\question\bank\sq_edit_menu_column($view),
new qbank_history\version_number_column($view),
new \mod_studentquiz\question\bank\anonym_creator_name_column($view),
new \mod_studentquiz\question\bank\tag_column($view),
new \mod_studentquiz\question\bank\attempts_column($view),
new \mod_studentquiz\question\bank\difficulty_level_column($view),
new \mod_studentquiz\question\bank\rate_column($view),
new \mod_studentquiz\question\bank\comment_column($view),
];
}
/**
* Get all the required columns for StudentQuiz view.
*
* @param studentquiz_bank_view_pre_43 $view
* @return array
*/
public function get_columns_for_question_bank_view_pre_43(studentquiz_bank_view_pre_43 $view) {
return [
new core_question\local\bank\checkbox_column($view),
new qbank_viewquestiontype\question_type_column($view),
new \mod_studentquiz\question\bank\state_column($view),
new \mod_studentquiz\question\bank\state_pin_column($view),
new \mod_studentquiz\question\bank\question_name_column($view),
new \mod_studentquiz\question\bank\sq_edit_action($view),
new \mod_studentquiz\question\bank\sq_preview_action($view),
new \mod_studentquiz\question\bank\sq_delete_action($view),
new \mod_studentquiz\question\bank\sq_hidden_action($view),
new \mod_studentquiz\question\bank\sq_pin_action($view),
new \mod_studentquiz\question\bank\sq_edit_menu_column($view),
new qbank_history\version_number_column($view),
new \mod_studentquiz\question\bank\anonym_creator_name_column($view),
new \mod_studentquiz\question\bank\tag_column($view),
new \mod_studentquiz\question\bank\attempts_column($view),
new \mod_studentquiz\question\bank\difficulty_level_column($view),
new \mod_studentquiz\question\bank\rate_column($view),
new \mod_studentquiz\question\bank\comment_column($view),
];
}
/**
* This is renderer function to support other theme to override the question form html.
*
* @param string $questionslist the questions list html content.
* @return string
*/
public function render_question_form(string $questionslist): string {
$output = \html_writer::start_tag('form', ['action' => '', 'method' => 'get', 'id' => 'questionsubmit']);
$output .= \html_writer::empty_tag('input', ['type' => 'submit', 'style' => 'display:none;']);
$output .= $questionslist;
$output .= \html_writer::end_tag('form');
return $output;
}
}
/**
* Question bank overview renderer.
*/
class mod_studentquiz_overview_renderer extends mod_studentquiz_renderer {
/**
* Builds the studentquiz_bank_view.
*
* @param studentquiz_view $view studentquiz_view class with the necessary information
* @return string formatted html
*/
public function render_overview($view) {
$contents = '';