forked from moodleou/moodle-block_workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
1451 lines (1257 loc) · 60.2 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/>.
/**
* Workflow block libraries
*
* @package block_workflow
* @copyright 2011 Lancaster University Network Services Limited
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* block_workflow Renderer
*
* Class for rendering various block_workflow objects
*
* @package block
* @subpackage workflow
* @copyright 2011 Lancaster University Network Services Limited
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_workflow_renderer extends plugin_renderer_base {
/**
* Render the block for the specified state
*
* @param object $state The block_workflow_step_state to render for
* @return string The rendered content
*/
public function block_display(block_workflow_step_state $state, $ajax = false) {
global $USER;
$canmakechanges = block_workflow_can_make_changes($state);
$output = '';
// Create the title.
$output .= html_writer::tag('h3', get_string('activetasktitle', 'block_workflow'));
$output .= html_writer::tag('p', format_string($state->step()->name));
// Roles overview.
if ($roles = $state->step()->roles()) {
$context = $state->context();
$output .= html_writer::tag('h3', get_string('tobecompletedby', 'block_workflow'));
$who = '';
$whoelse = array();
// Got through the list.
foreach ($roles as $role) {
if (user_has_role_assignment($USER->id, $role->id, $context->id)) {
$who = get_string('youandanyother', 'block_workflow');
}
$whoelse[] = $role->localname;
}
if (empty($who)) {
// If the current user isn't in the list, make it 'Any ...'.
$who = get_string('any', 'block_workflow');
}
if (count($whoelse)) {
// If any roles are assigned, grab the last one and leave it to one side.
$lastrole = array_pop($whoelse);
if (count($whoelse) > 0) {
// If there are still other roles assigned, turn them into a list.
$who .= implode(', ', $whoelse);
$who .= get_string('youor', 'block_workflow') . $lastrole;
} else {
// Just add the last role.
$who .= $lastrole;
}
}
$output .= html_writer::tag('span', $who);
$output .= $this->get_popup_button($roles, $context);
$this->page->requires->yui_module('moodle-block_workflow-userinfo', 'M.block_workflow.userinfo.init');
}
// Instructions.
$output .= html_writer::tag('h3', get_string('instructions', 'block_workflow'));
$output .= html_writer::tag('div', $state->step()->format_instructions($state->context()));
// Comments.
$output .= html_writer::tag('h3', get_string('comments', 'block_workflow'));
$commentsblock = html_writer::start_tag('div', array('class' => 'block_workflow_comments'));
$commenttext = shorten_text(format_text($state->comment, $state->commentformat,
array('context' => $state->context())), BLOCK_WORKFLOW_MAX_COMMENT_LENGTH);
if ($commenttext) {
$commentsblock .= $commenttext;
} else {
$commentsblock .= get_string('nocomments', 'block_workflow');
}
$commentsblock .= html_writer::end_tag('div');
$output .= $commentsblock;
// To-do list overview.
if ($todos = $state->todos()) {
$output .= html_writer::tag('h3', get_string('todolisttitle', 'block_workflow'));
$list = html_writer::start_tag('ul', array('class' => 'block_workflow_todolist'));
foreach ($state->todos() as $todo) {
$list .= $this->block_display_todo_item($todo, $state->id, $canmakechanges);
}
$list .= html_writer::end_tag('ul');
$output .= $list;
}
if ($canmakechanges) {
// Edit comments.
$url = new moodle_url('/blocks/workflow/editcomment.php',
array('stateid' => $state->id));
$editbutton = new single_button($url, get_string('editcomments', 'block_workflow'), 'get');
$editbutton->class = 'singlebutton block_workflow_editcommentbutton';
$output .= html_writer::tag('div', $this->output->render($editbutton));
if (!$ajax) {
// Output the contents of the edit comment dialogue, hidden.
// Prepare editor.
$editor = new MoodleQuickForm_editor('comment_editor', get_string('commentlabel', 'block_workflow'),
array('id' => 'wkf-comment-editor'), block_workflow_editor_options());
$editor->setValue(array('text' => $state->comment));
$output .= '<div class="block-workflow-panel">
<form class="wkf-comments" action=".">
<div class="wfk-textarea">' .
html_writer::label(get_string('commentlabel', 'block_workflow'),
'wkf-comment-editor', false, array('class' => 'accesshide')) .
$editor->toHtml() . '
</div>
<div class="wfk-submit">
<input type="button" class="submitbutton" value="' . get_string('submit') . '" />
</div>
</form>
<div class="loading-lightbox hidden">' .
$this->pix_icon('i/loading', get_string('loading', 'admin'), 'moodle',
array('class' => 'loading-icon')) . '
</div>
</div>';
}
// Finish step.
$url = new moodle_url('/blocks/workflow/finishstep.php',
array('stateid' => $state->id));
$finishbutton = new single_button($url, get_string('finishstep', 'block_workflow'), 'get');
$finishbutton->class = 'singlebutton block_workflow_finishstepbutton';
$output .= html_writer::tag('div', $this->output->render($finishbutton));
}
$output .= $this->workflow_overview_button($state->contextid, $state->step()->workflowid);
return $output;
}
/**
* Display a button to go to the workflow overview.
* @param int $contextid the context to display the overiew for.
* @param int $workflowid the workflow to display the overiew for.
* @return string HTML of the button.
*/
public function workflow_overview_button($contextid, $workflowid) {
$url = new moodle_url('/blocks/workflow/overview.php', array(
'contextid' => $contextid, 'workflowid' => $workflowid));
$overviewbutton = new single_button($url,
get_string('workflowoverview', 'block_workflow'), 'get');
return html_writer::tag('div', $this->output->render($overviewbutton));
}
/**
* Render the given todo list item as a <li> element with appropriate links
*
* @param object $todo The todo stdClass to render
* @param integer $stateid The ID of the state to render for (used for links)
* @param boolean $editable Whether this user has permission to make changes to todolist items
* @return string The rendered list item
*/
public function block_display_todo_item($todo, $stateid, $editable) {
global $CFG;
$todoattribs = array();
// The contents of the list item.
$text = format_string($todo->task);
// Determine whether the task has been completed.
if ($todo->userid) {
$todoattribs['class'] = ' completed';
}
if ($editable) {
// Generate the URL and Link.
$returnurl = str_replace($CFG->wwwroot, '', $this->page->url->out(false));
$url = new moodle_url('/blocks/workflow/toggletaskdone.php',
array('sesskey' => sesskey(), 'stateid' => $stateid, 'todoid' => $todo->id, 'returnurl' => $returnurl));
$li = html_writer::tag('li', html_writer::link($url, $text,
array('class' => 'block-workflow-todotask', 'id' => 'block-workflow-todoid-' . $todo->id)),
$todoattribs);
} else {
$li = html_writer::tag('li', $text, $todoattribs);
}
// Return the generate list item.
return $li;
}
/**
* Render the content when there is no active workflow.
* @param $context database record containing the context data
* @param $addableworkflows array The list of available workflows
* @param $previous array A list of the previous workflows on this
* context
* @return string the HTML to output.
*/
public function block_display_no_more_steps($parentcontextid,
$canadd, array $addableworkflows, array $previous = null) {
$output = '';
if ($previous) {
$p = reset($previous);
$output .= html_writer::tag('p', get_string('nomorestepsleft', 'block_workflow'));
$output .= $this->workflow_overview_button($parentcontextid, $p->id);
}
if (!$canadd) {
return $output;
}
if (!$previous) {
// No workflow was previously assigned.
$output .= html_writer::tag('p', get_string('noworkflow', 'block_workflow'));
}
if ($addableworkflows) {
$url = new moodle_url('/blocks/workflow/addworkflow.php',
array('sesskey' => sesskey(), 'contextid' => $parentcontextid));
$addoptions = array();
foreach ($addableworkflows as $wf) {
$addoptions[$wf->id] = $wf->name;
}
$list = new single_select($url, 'workflow', $addoptions);
if ($previous) {
$list->set_label(get_string('addanotherworkflow', 'block_workflow'));
} else {
$list->set_label(get_string('addaworkflow', 'block_workflow'));
}
// And generate the output.
$output .= html_writer::tag('div', $this->output->render($list));
}
return $output;
}
/**
* Render the content to display when no more steps remain
*
* This is used by the ajax library so that users get feedback when finishing the final step
*
* @return string The text to render
*/
public function block_display_step_complete_confirmation() {
return html_writer::tag('p', get_string('stepfinishconfirmation', 'block_workflow'));
}
/**
* Display the interface to manage workflows
*
* @param array $workflows The list of workflows to display
* @param array $emails The list of email templates to display
* @return string The text to render
*/
public function manage_workflows(array $workflows, array $emails) {
$output = '';
// The manage workflows section.
$output .= $this->output->heading(get_string('manageworkflows', 'block_workflow'));
$output .= html_writer::tag('p', get_string('managedescription', 'block_workflow'));
$output .= $this->list_workflows($workflows);
// The manage workflows section.
$output .= $this->output->heading(get_string('manageemails', 'block_workflow'));
$output .= html_writer::tag('p', get_string('emaildescription', 'block_workflow'));
$output .= $this->list_emails($emails);
return $output;
}
/**
* The workflow list table
*
* Called by manage_workflows
* @param array $workflows The list of workflows to display
* @return string The text to render
*/
protected function list_workflows($workflows) {
$output = '';
// Display the current workflows.
$table = new html_table();
$table->attributes['class'] = '';
$table->head = array();
$table->colclasses = array();
$table->data = array();
$table->head[] = get_string('shortname', 'block_workflow');
$table->head[] = get_string('name', 'block_workflow');
$table->head[] = get_string('appliesto', 'block_workflow');
$table->head[] = '';
// Check whether each workflow is deletable.
foreach ($workflows as $workflow) {
$workflow->is_deletable = block_workflow_workflow::is_workflow_deletable($workflow->id);
$table->data[] = $this->workflow_row($workflow);
}
// Create a new workflow.
$emptycell = new html_table_cell();
$emptycell->colspan = 3;
$actions = array();
$add = html_writer::empty_tag('img', array('src' => $this->output->image_url('t/add'),
'class' => 'iconsmall',
'title' => get_string('createworkflow', 'block_workflow'),
'alt' => get_string('createworkflow', 'block_workflow')
));
$url = new moodle_url('/blocks/workflow/editsettings.php');
$actions[] = html_writer::link($url, $add);
$add = html_writer::empty_tag('img', array('src' => $this->output->image_url('t/restore'),
'class' => 'iconsmall',
'title' => get_string('importworkflow', 'block_workflow'),
'alt' => get_string('importworkflow', 'block_workflow')
));
$url = new moodle_url('/blocks/workflow/import.php');
$actions[] = html_writer::link($url, $add);
$addimportcell = new html_table_cell(implode(' ', $actions));
$addimportcell->attributes['class'] = 'mdl-align';
$row = new html_table_row(array($emptycell, $addimportcell));
$table->data[] = $row;
$output .= html_writer::table($table);
return $output;
}
/**
* The workflow list row
*
* Called by list_workflows
* @param object $workflow The workflow to display
* @return string The text to render
*/
protected function workflow_row(stdClass $workflow) {
$row = new html_table_row();
$row->attributes['class'] = 'workflow';
if ($workflow->obsolete != BLOCK_WORKFLOW_ENABLED) {
$row->attributes['class'] .= ' dimmed_text';
}
// Shortname.
$cell = new html_table_cell(s($workflow->shortname));
$row->cells[] = $cell;
// Workflow name.
$cell = new html_table_cell(format_string($workflow->name));
$row->cells[] = $cell;
// Applies to.
$cell = new html_table_cell(block_workflow_appliesto($workflow->appliesto));
$row->cells[] = $cell;
// View/Edit steps.
$url = new moodle_url('/blocks/workflow/editsteps.php', array('workflowid' => $workflow->id));
$actions[] = html_writer::link($url, html_writer::empty_tag('img', array(
'src' => $this->output->image_url('t/edit'),
'class' => 'iconsmall',
'title' => get_string('vieweditworkflow', 'block_workflow'),
'alt' => get_string('vieweditworkflow', 'block_workflow')
)));
// Export workflow.
$url = new moodle_url('/blocks/workflow/export.php', array('sesskey' => sesskey(), 'workflowid' => $workflow->id));
$actions[] = html_writer::link($url, html_writer::empty_tag('img', array(
'src' => $this->output->image_url('t/backup'),
'class' => 'iconsmall',
'title' => get_string('exportworkflow', 'block_workflow'),
'alt' => get_string('exportworkflow', 'block_workflow')
)));
// Clone workflow.
$url = new moodle_url('/blocks/workflow/clone.php', array('workflowid' => $workflow->id));
$actions[] = html_writer::link($url, html_writer::empty_tag('img', array(
'src' => $this->output->image_url('t/copy'),
'class' => 'iconsmall',
'title' => get_string('cloneworkflow', 'block_workflow'),
'alt' => get_string('cloneworkflow', 'block_workflow')
)));
// Disable/Enable workflow.
$cell = new html_table_cell();
if ($workflow->obsolete == BLOCK_WORKFLOW_ENABLED) {
$url = new moodle_url('/blocks/workflow/toggleworkflowobsolete.php',
array('sesskey' => sesskey(), 'workflowid' => $workflow->id));
$actions[] = html_writer::link($url, html_writer::empty_tag('img', array(
'src' => $this->output->image_url('t/hide'),
'class' => 'iconsmall',
'title' => get_string('disableworkflow', 'block_workflow'),
'alt' => get_string('disableworkflow', 'block_workflow')
)));
} else {
$url = new moodle_url('/blocks/workflow/toggleworkflowobsolete.php',
array('sesskey' => sesskey(), 'workflowid' => $workflow->id));
$actions[] = html_writer::link($url, html_writer::empty_tag('img', array(
'src' => $this->output->image_url('t/show'),
'class' => 'iconsmall',
'title' => get_string('enableworkflow', 'block_workflow'),
'alt' => get_string('enableworkflow', 'block_workflow')
)));
}
// Remove workflow.
if ($workflow->is_deletable) {
$url = new moodle_url('/blocks/workflow/delete.php', array('workflowid' => $workflow->id));
$actions[] = html_writer::link($url, html_writer::empty_tag('img', array(
'src' => $this->output->image_url('t/delete'),
'class' => 'iconsmall',
'title' => get_string('removeworkflow', 'block_workflow'),
'alt' => get_string('removeworkflow', 'block_workflow')
)));
} else {
$a = block_workflow_workflow::in_use_by($workflow->id);
$actions[] = html_writer::empty_tag('img', array(
'src' => $this->output->image_url('t/delete'),
'class' => 'iconsmall',
'title' => get_string('cannotdeleteworkflowinuseby', 'block_workflow', $a),
'alt' => get_string('removeworkflow', 'block_workflow')
));
}
$cell = new html_table_cell(implode(' ', $actions));
$row->cells[] = $cell;
return $row;
}
/**
* The email list table
*
* Called by manage_workflows
* @param array $emails The list of email templates to display
* @return string The text to render
*/
protected function list_emails(array $emails) {
$output = '';
// Table setup.
$table = $this->setup_table();
$table->attributes['class'] = '';
$table->head[] = get_string('shortname', 'block_workflow');
$table->head[] = get_string('emailsubject', 'block_workflow');
$table->head[] = '';
// Add the individual emails.
foreach ($emails as $email) {
$table->data[] = $this->email_row($email);
}
// Create a new email.
$emptycell = new html_table_cell();
$emptycell->colspan = 2;
$add = html_writer::empty_tag('img', array('src' => $this->output->image_url('t/add'),
'class' => 'iconsmall',
'title' => get_string('addemail', 'block_workflow'),
'alt' => get_string('addemail', 'block_workflow')
));
$url = new moodle_url('/blocks/workflow/editemail.php');
$addnewcell = new html_table_cell(html_writer::link($url, $add));
$addnewcell->attributes['class'] = 'mdl-align';
$row = new html_table_row(array($emptycell, $addnewcell));
$table->data[] = $row;
$output .= html_writer::table($table);
return $output;
}
/**
* The e-mail template list row
*
* Called by list_emails
* @param object $email The e-mail template to display
* @return string The text to render
*/
protected function email_row(stdClass $email) {
$row = new html_table_row();
$row->attributes['class'] = 'email';
// Shortname.
$cell = new html_table_cell(s($email->shortname));
$row->cells[] = $cell;
// Subject.
$cell = new html_table_cell(format_string($email->subject));
$row->cells[] = $cell;
// View/Edit steps.
$url = new moodle_url('/blocks/workflow/editemail.php', array('emailid' => $email->id));
$actions[] = html_writer::link($url, html_writer::empty_tag('img', array(
'src' => $this->output->image_url('t/edit'),
'class' => 'iconsmall',
'title' => get_string('vieweditemail', 'block_workflow'),
'alt' => get_string('vieweditemail', 'block_workflow'),
)));
// Remove email.
if ($email->activecount || $email->completecount) {
$actions[] = html_writer::empty_tag('img', array(
'src' => $this->output->image_url('t/delete'),
'class' => 'iconsmall',
'title' => get_string('cannotremoveemailinuse', 'block_workflow'),
'alt' => get_string('deleteemail', 'block_workflow'),
));
} else {
$url = new moodle_url('/blocks/workflow/deleteemail.php', array('emailid' => $email->id));
$actions[] = html_writer::link($url, html_writer::empty_tag('img', array(
'src' => $this->output->image_url('t/delete'),
'class' => 'iconsmall',
'title' => get_string('deleteemail', 'block_workflow'),
'alt' => get_string('deleteemail', 'block_workflow'),
)));
}
// Add the steps.
$cell = new html_table_cell(implode(' ', $actions));
$row->cells[] = $cell;
return $row;
}
/**
* Render a list of steps
*
* Used in editsteps.php
*
* @param object $workflow The workflow to display
* @return string The text to render
*/
public function list_steps($workflow) {
$output = '';
// List of steps.
$output .= $this->output->heading(get_string('workflowsteps', 'block_workflow'));
// Set up the table and it's headers.
$table = $this->setup_table();
$table->attributes['class'] = '';
$table->head[] = get_string('stepno', 'block_workflow');
$table->head[] = get_string('stepname', 'block_workflow');
$table->head[] = get_string('doerstitle', 'block_workflow');
$table->head[] = get_string('stepinstructions', 'block_workflow');
$table->head[] = get_string('finish', 'block_workflow');
$table->head[] = '';
// Retrieve a list of steps etc.
$steps = $workflow->steps();
$info = new stdClass();
$info->stepcount = count($steps);
$info->workflowid = $workflow->id;
$info->appliesto = $workflow->appliesto;
// The image to add a new step.
$add = html_writer::empty_tag('img', array('src' => $this->output->image_url('t/add'),
'class' => 'iconsmall',
'title' => get_string('addstep', 'block_workflow'),
'alt' => get_string('addstep', 'block_workflow')
));
// Add a step to the beginning.
$addempty = new html_table_cell();
$addempty->colspan = 5;
$addcell = new html_table_cell(html_writer::link(new moodle_url('/blocks/workflow/editstep.php',
array('workflowid' => $workflow->id, 'beforeafter' => -1)), $add));
$addcell->attributes['class'] = 'mdl-align';
$addrow = new html_table_row(array($addempty, $addcell));
$table->data[] = $addrow;
// Process the other steps.
while ($step = array_shift($steps)) {
if (count($steps) == 0) {
$step->finalstep = true;
}
$table->data[] = $this->workflow_step($step, $info);
}
// Add option to add a new step.
$infocell = new html_table_cell($this->atendgobackto($workflow));
$infocell->colspan = 5;
$infocell->attributes['class'] = 'mdl-align';
$url = new moodle_url('/blocks/workflow/editstep.php', array('workflowid' => $workflow->id));
$addnewcell = new html_table_cell(html_writer::link($url, $add));
$addnewcell->attributes['class'] = 'mdl-align';
$row = new html_table_row(array($infocell, $addnewcell));
$table->data[] = $row;
// Display the table.
$output .= html_writer::table($table);
return $output;
}
protected function workflow_step($step, $info) {
$row = new html_table_row();
// Step number.
$cell = new html_table_cell($step->stepno);
$cell->attributes['class'] = 'mdl-align';
$row->cells[] = $cell;
// Name.
$cell = new html_table_cell(format_string($step->name));
$row->cells[] = $cell;
// Roles reponsible for this step.
$cell = new html_table_cell($this->workflow_step_doers($step));
$row->cells[] = $cell;
// Instructions.
$cell = new html_table_cell(format_text($step->instructions, $step->instructionsformat));
$row->cells[] = $cell;
// Automatically finish.
$cell = new html_table_cell($this->workflow_step_auto_finish($step, $info->appliesto));
$row->cells[] = $cell;
// Modification.
$actions = array();
$url = new moodle_url('/blocks/workflow/editstep.php', array('stepid' => $step->id));
$actions[] = html_writer::link($url, html_writer::empty_tag('img', array('src' => $this->output->image_url('t/edit'),
'class' => 'iconsmall',
'title' => get_string('editstep', 'block_workflow'),
'alt' => get_string('editstep', 'block_workflow')
)));
// Add step after this one.
$url = new moodle_url('/blocks/workflow/editstep.php',
array('workflowid' => $info->workflowid, 'beforeafter' => $step->stepno));
$actions[] = html_writer::link($url, html_writer::empty_tag('img', array(
'src' => $this->output->image_url('t/add'),
'class' => 'iconsmall',
'title' => get_string('addstepafter', 'block_workflow'),
'alt' => get_string('addstepafter', 'block_workflow')
)));
// Can't be removed if this is the only step or in use.
if ($info->stepcount != 1 && !block_workflow_step::is_step_in_use($step->id)) {
$url = new moodle_url('/blocks/workflow/deletestep.php', array('stepid' => $step->id));
$actions[] = html_writer::link($url, html_writer::empty_tag('img', array(
'src' => $this->output->image_url('t/delete'),
'class' => 'iconsmall',
'title' => get_string('removestep', 'block_workflow'),
'alt' => get_string('removestep', 'block_workflow')
)));
}
// Move up if this is not the first step.
if ($step->stepno != 1) {
$url = new moodle_url('/blocks/workflow/movestep.php',
array('sesskey' => sesskey(), 'id' => $step->id, 'direction' => 'up'));
$actions[] = html_writer::link($url, html_writer::empty_tag('img', array(
'src' => $this->output->image_url('t/up'),
'class' => 'iconsmall',
'title' => get_string('moveup', 'block_workflow'),
'alt' => get_string('moveup', 'block_workflow')
)));
}
// Move down if this is not the final step.
if (!isset($step->finalstep)) {
$url = new moodle_url('/blocks/workflow/movestep.php',
array('sesskey' => sesskey(), 'id' => $step->id, 'direction' => 'down'));
$actions[] = html_writer::link($url, html_writer::empty_tag('img', array(
'src' => $this->output->image_url('t/down'),
'class' => 'iconsmall',
'title' => get_string('movedown', 'block_workflow'),
'alt' => get_string('movedown', 'block_workflow')
)));
}
$cell = new html_table_cell(implode(' ', $actions));
$row->cells[] = $cell;
return $row;
}
/**
* Return a sting that indicates whether a given step is set to be finish automatically
* @param object $stepdata raw data about the step, loaded from the DB.
* @return string textual description of the settings.
*/
protected function workflow_step_auto_finish($step, $appliesto) {
// Do not finish this step automatically.
if (!$step->autofinish || $step->autofinish == 'donotautomaticallyfinish') {
return get_string('donotautomaticallyfinish', 'block_workflow');
}
list($options, $days) = block_workflow_step::get_autofinish_options($appliesto);
if ($step->autofinishoffset > 0) {
// Days after certain condition.
$days = $step->autofinishoffset / (24 * 60 * 60);
if ($days == 1) {
$daysstring = get_string('dayafter', 'block_workflow', $days);
} else {
$daysstring = get_string('daysafter', 'block_workflow', $days);
}
} else if ($step->autofinishoffset < 0) {
// Days before certain condition.
$days = abs($step->autofinishoffset) / (24 * 60 * 60);
if ($days == 1) {
$daysstring = get_string('daybefore', 'block_workflow', $days);
} else {
$daysstring = get_string('daysbefore', 'block_workflow', $days);
}
} else {
// Same day as certain condition.
$daysstring = get_string('dayas', 'block_workflow');
}
list($table, $field) = explode(';', $step->autofinish);
$key = $table . ';' . $field;
if (array_key_exists($key, $options)) {
return $daysstring . ' ' . $options[$key];
}
return '';
}
/**
* Get all roles that are doers of a given step
* @param object $stepdata raw data about the step, loaded from the DB.
* @return string comma-separated list of role names.
*/
protected function workflow_step_doers($stepdata) {
$step = block_workflow_step::make($stepdata);
$doernames = array();
foreach ($step->roles() as $doer) {
$doernames[] = $doer->localname;
}
return implode(', ', $doernames);
}
protected function workflow_information($workflow) {
$output = '';
// Header and general information.
$output .= $this->output->heading(get_string('workflowinformation', 'block_workflow'), 3, 'title header');
$table = $this->setup_table();
// Workflow name and shortname.
$row = new html_table_row(array(get_string('name', 'block_workflow')));
$cell = new html_table_cell();
$data = array('name' => format_string($workflow->name), 'shortname' => s($workflow->shortname));
$cell->text = get_string('nameshortname', 'block_workflow', $data);
$row->cells[] = $cell;
$table->data[] = $row;
// Description.
$row = new html_table_row(array(get_string('description', 'block_workflow')));
$cell = new html_table_cell();
$cell->text = format_text($workflow->description, $workflow->descriptionformat);
$row->cells[] = $cell;
$table->data[] = $row;
// What contexts does this block apply to.
$row = new html_table_row(array(get_string('appliesto', 'block_workflow')));
$cell = new html_table_cell();
$cell->text = $workflow->appliesto;
$row->cells[] = $cell;
$table->data[] = $row;
// Status information.
$row = new html_table_row(array(get_string('status', 'block_workflow')));
$cell = new html_table_cell();
if ($workflow->obsolete == BLOCK_WORKFLOW_OBSOLETE) {
$cell->text = get_string('obsoleteworkflow', 'block_workflow');
} else {
$cell->text = get_string('enabledworkflow', 'block_workflow');
}
$row->cells[] = $cell;
$table->data[] = $row;
// Other info.
$row = new html_table_row(array(get_string('inuseby', 'block_workflow')));
$cell = new html_table_cell('This workflow is active in x contexts');
$row->cells[] = $cell;
$table->data[] = $row;
$output .= html_writer::table($table);
return $output;
}
/**
* Display the specified workflow settings and include links to edit these settings
*
* @param object $workflow The workflow to display
* @return object The renderer to display
*/
public function display_workflow($workflow) {
$output = '';
// Start the box.
$output .= $this->output->heading(get_string('workflowsettings', 'block_workflow'));
// Setup the table.
$table = $this->setup_table();
$table->attributes['class'] = '';
// Shortname.
$row = new html_table_row(array(
get_string('shortname', 'block_workflow'),
s($workflow->shortname),
));
$table->data[] = $row;
// Name.
$row = new html_table_row(array(
get_string('name', 'block_workflow'),
format_string($workflow->name),
));
$table->data[] = $row;
// Description.
$row = new html_table_row(array(
get_string('description', 'block_workflow'),
format_text($workflow->description, $workflow->descriptionformat),
));
$table->data[] = $row;
// Applies to.
$row = new html_table_row(array(
get_string('thisworkflowappliesto', 'block_workflow'),
block_workflow_appliesto($workflow->appliesto),
));
$table->data[] = $row;
// Current status.
$togglelink = new moodle_url('/blocks/workflow/toggleworkflowobsolete.php',
array('workflowid' => $workflow->id, 'returnto' => 'editsteps', 'sesskey' => sesskey()));
if ($workflow->obsolete) {
$status = get_string('workflowobsolete', 'block_workflow', $togglelink->out());
} else {
$status = get_string('workflowactive', 'block_workflow', $togglelink->out());
}
// Count the times the workflow is actively in use.
if ($count = block_workflow_workflow::in_use_by($workflow->id, true)) {
$status .= get_string('inuseby', 'block_workflow', $count);
} else {
$status .= get_string('notcurrentlyinuse', 'block_workflow');
}
$row = new html_table_row(array(
get_string('workflowstatus', 'block_workflow'),
$status
));
$table->data[] = $row;
// Workflow actions.
$row = new html_table_row();
$cell = new html_table_cell();
$cell->colspan = 2;
$cell->attributes['class'] = 'mdl-align';
$actions = array();
// Edit the workflow.
$url = new moodle_url('/blocks/workflow/editsettings.php', array('workflowid' => $workflow->id));
$actions[] = html_writer::link($url, get_string('edit', 'block_workflow'));
// Clone the workflow.
$url = new moodle_url('/blocks/workflow/clone.php', array('workflowid' => $workflow->id));
$actions[] = html_writer::link($url, get_string('clone', 'block_workflow'));
// Export the workflow.
$url = new moodle_url('/blocks/workflow/export.php', array('sesskey' => sesskey(), 'workflowid' => $workflow->id));
$actions[] = html_writer::link($url, get_string('export', 'block_workflow'));
if (block_workflow_workflow::is_workflow_deletable($workflow->id)) {
// Delete the workflow.
$url = new moodle_url('/blocks/workflow/delete.php', array('workflowid' => $workflow->id));
$actions[] = html_writer::link($url, get_string('delete', 'block_workflow'));
}
$cell->text = implode(', ', $actions);
$row->cells[] = $cell;
$table->data[] = $row;
// Display the table.
$output .= html_writer::table($table);
return $output;
}
public function step_todolist($todos, $step) {
$output = '';
// Title area.
$output .= $this->output->heading(get_string('todotitle', 'block_workflow'), 3, 'title header');
// The to-do list.
$table = $this->setup_table();
$table->head[] = get_string('todotask', 'block_workflow');
$table->head[] = '';
foreach ($todos as $todo) {
$todo->isremovable = true;
$table->data[] = $this->step_todolist_item($todo);
}
// Add option to add a new task.
$emptycell = new html_table_cell();
$add = html_writer::empty_tag('img', array('src' => $this->output->image_url('t/add'),
'class' => 'iconsmall',
'title' => get_string('addtask', 'block_workflow'),
'alt' => get_string('addtask', 'block_workflow')
));
$url = new moodle_url('/blocks/workflow/edittask.php', array('stepid' => $step->id));
$addnewcell = new html_table_cell(html_writer::link($url, $add));
$row = new html_table_row(array($emptycell, $addnewcell));
$table->data[] = $row;
// Display the table.
$output .= html_writer::table($table);
return $output;
}
protected function step_todolist_item(stdClass $task) {
$row = new html_table_row();
$name = new html_table_cell(format_string($task->task));
$actions = array();
$url = new moodle_url('/blocks/workflow/edittask.php', array('id' => $task->id));
$actions[] = html_writer::link($url, html_writer::empty_tag('img', array('src' => $this->output->image_url('t/edit'),
'class' => 'iconsmall',
'title' => get_string('edittask', 'block_workflow'),
'alt' => get_string('edittask', 'block_workflow')
)));
// Obsolete task.
$url = new moodle_url('/blocks/workflow/toggletaskobsolete.php', array('sesskey' => sesskey(), 'taskid' => $task->id));
if ($task->obsolete == BLOCK_WORKFLOW_ENABLED) {
$actions[] = html_writer::link($url, html_writer::empty_tag('img', array('src' => $this->output->image_url('t/hide'),
'class' => 'iconsmall',
'title' => get_string('hidetask', 'block_workflow'),
'alt' => get_string('hidetask', 'block_workflow')
)));
} else {
$actions[] = html_writer::link($url, html_writer::empty_tag('img', array('src' => $this->output->image_url('t/show'),
'class' => 'iconsmall',
'title' => get_string('showtask', 'block_workflow'),
'alt' => get_string('showtask', 'block_workflow')
)));
}
// Delete task.
if ($task->isremovable) {
$url = new moodle_url('/blocks/workflow/deletetask.php', array('id' => $task->id));
$actions[] = html_writer::link($url,
html_writer::empty_tag('img', array('src' => $this->output->image_url('t/delete'),
'class' => 'iconsmall',
'title' => get_string('removetask', 'block_workflow'),
'alt' => get_string('removetask', 'block_workflow')
)));
}
$actions = new html_table_cell(implode(' ', $actions));
// Put it all together into a row and return the data.
$row = new html_table_row(array($name, $actions));
return $row;
}
public function step_doers($roles, $doers, $stepid) {
$output = '';