-
Notifications
You must be signed in to change notification settings - Fork 28
/
lib.php
153 lines (127 loc) · 5.17 KB
/
lib.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
<?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/>.
/**
* Global functions for tool_trigger plugin.
*
* @package tool_trigger
* @copyright Matt Porritt <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use tool_trigger\steps\base\base_form;
use tool_trigger\import_form;
/**
* Renders the top part of the "new workflow step" modal form. (The part with
* the "Step type" and "Step" menus.
*
* @param array $args
* @param context $args['context']
* @return string
*/
function tool_trigger_output_fragment_new_base_form($args) {
require_capability('tool/trigger:manageworkflows', $args['context']);
$mform = new base_form();
ob_start();
$mform->display();
$o = ob_get_contents();
ob_end_clean();
return $o;
}
/**
* Renders the full form for a particular step class, for the "new workflow
* step" modal form.
*
* @param array $args
* @param context $args['context']
* @param string $args['steptype'] The steptype to display in the form.
* @param string $args['stepclass'] The stepclass to display in the form.
* @param string $args['defaults'] Default values to populate the form with (JSON-encoded array)
* @param string $args['ajaxformdata'] Serialized form submission data, if the form
* is being redisplayed after failed validation.
* @return string
*/
function tool_trigger_output_fragment_new_step_form($args) {
$context = $args['context'];
require_capability('tool/trigger:manageworkflows', $context);
$event = clean_param($args['event'], PARAM_RAW_TRIMMED);
$existingsteps = json_decode(clean_param($args['existingsteps'], PARAM_RAW_TRIMMED), true);
$steptype = clean_param($args['steptype'], PARAM_ALPHA);
$steporder = clean_param($args['steporder'], PARAM_INT);
$stepclass = clean_param($args['stepclass'], PARAM_RAW);
$workflowmanager = new \tool_trigger\workflow_manager();
$stepclassobj = $workflowmanager->validate_and_make_step($stepclass);
$customdata = array(
'type' => $steptype,
'stepclass' => $stepclass,
'steptext' => $stepclass::get_step_name(),
'steps' => $workflowmanager->get_steps_by_type($steptype),
'event' => $event,
'existingsteps' => $existingsteps,
'steporder' => $steporder
);
$ajaxformdata = array();
if (!empty($args['ajaxformdata'])) {
// Don't need to clean/validate these, because formslib will do that.
parse_str($args['ajaxformdata'], $ajaxformdata);
// Apply any data transforms - determined in each step's class.
$ajaxformdata = $stepclassobj->transform_form_data($ajaxformdata);
}
$mform = $stepclassobj->make_form($customdata, $ajaxformdata);
if (!empty($args['defaults'])) {
// Don't need to clean/validate these, because formslib will do that.
$data = json_decode($args['defaults'], true);
// Requirement for new editor to populate text area..
if (!empty($data['emailcontent_editor[text]'])) {
$data['emailcontent'] = "";
$data['emailcontentformat'] = $data['emailcontent_editor[format]'];
$data['emailcontent_editor']['text'] = $data['emailcontent_editor[text]'];
$data['emailcontent_editor']['format'] = $data['emailcontent_editor[format]'];
$data['emailcontent_editor']['itemid'] = $data['id'];
$data['emailcontenttrust'] = false;
}
// Also fix up duration for debouncing.
if (!empty($data['debounceduration[number]'])) {
$data['debounceduration']['number'] = $data['debounceduration[number]'];
$data['debounceduration']['timeunit'] = $data['debounceduration[timeunit]'];
}
// Apply any data transforms - determined in each step's class.
$data = $stepclassobj->transform_form_data($data);
$mform->set_data($data);
}
if (!empty($ajaxformdata)) {
// If we were passed non-empty form data we want the mform to call validation functions and show errors.
$mform->is_validated();
}
ob_start();
$mform->display();
$o = ob_get_contents();
ob_end_clean();
return $o;
}
/**
* Renders the import workflow from file form.
*
* @param array $args
* @return string
*/
function tool_trigger_output_fragment_new_import_form($args) {
require_capability('tool/trigger:manageworkflows', $args['context']);
$mform = new import_form(null, null, 'post', '', ['class' => 'ignoredirty']);
ob_start();
$mform->display();
$o = ob_get_contents();
ob_end_clean();
return $o;
}