3af29899 |
1 | <?php //$Id$ |
cbff94ba |
2 | |
0610812a |
3 | /** |
4 | * Print grading plugin selection popup form. |
5 | * |
6 | * @param int $courseid id of course |
7 | * @param string $active_type type of plugin on current page - import, export, report or edit |
8 | * @param string $active_plugin active plugin type - grader, user, cvs, ... |
9 | * @param boolean $return return as string |
10 | * @return nothing or string if $return true |
11 | */ |
3af29899 |
12 | function print_grade_plugin_selector($courseid, $active_type, $active_plugin, $return=false) { |
cbff94ba |
13 | global $CFG; |
cbff94ba |
14 | |
3af29899 |
15 | $context = get_context_instance(CONTEXT_COURSE, $courseid); |
cbff94ba |
16 | |
3af29899 |
17 | $menu = array(); |
cbff94ba |
18 | |
3af29899 |
19 | $active = ''; |
cbff94ba |
20 | |
3af29899 |
21 | /// report plugins with its special structure |
22 | if ($reports = get_list_of_plugins('grade/report', 'CVS')) { // Get all installed reports |
23 | foreach ($reports as $key => $plugin) { // Remove ones we can't see |
24 | if (!has_capability('gradereport/'.$plugin.':view', $context)) { |
25 | unset($reports[$key]); |
cbff94ba |
26 | } |
27 | } |
04678d8e |
28 | } |
3af29899 |
29 | $reportnames = array(); |
30 | if (!empty($reports)) { |
31 | foreach ($reports as $plugin) { |
65dd61bd |
32 | $url = 'report/'.$plugin.'/index.php?id='.$courseid; |
3af29899 |
33 | if ($active_type == 'report' and $active_plugin == $plugin ) { |
34 | $active = $url; |
cbff94ba |
35 | } |
e2008be2 |
36 | $reportnames[$url] = get_string('modulename', 'gradereport_'.$plugin, NULL, $CFG->dirroot.'/grade/report/'.$plugin.'lang/'); |
cbff94ba |
37 | } |
3af29899 |
38 | asort($reportnames); |
cbff94ba |
39 | } |
3af29899 |
40 | if (!empty($reportnames)) { |
41 | $menu['reportgroup']='--'.get_string('reportplugins', 'grades'); |
42 | $menu = $menu+$reportnames; |
cbff94ba |
43 | } |
cbff94ba |
44 | |
3af29899 |
45 | /// standard import plugins |
e2008be2 |
46 | if ($imports = get_list_of_plugins('grade/import', 'CVS')) { // Get all installed import plugins |
3af29899 |
47 | foreach ($imports as $key => $plugin) { // Remove ones we can't see |
48 | if (!has_capability('gradeimport/'.$plugin.':view', $context)) { |
49 | unset($imports[$key]); |
cbff94ba |
50 | } |
51 | } |
52 | } |
3af29899 |
53 | $importnames = array(); |
54 | if (!empty($imports)) { |
55 | foreach ($imports as $plugin) { |
56 | $url = 'import/'.$plugin.'/index.php?id='.$courseid; |
65dd61bd |
57 | if ($active_type == 'import' and $active_plugin == $plugin ) { |
3af29899 |
58 | $active = $url; |
59 | } |
e2008be2 |
60 | $importnames[$url] = get_string('modulename', 'gradeimport_'.$plugin, NULL, $CFG->dirroot.'/grade/import/'.$plugin.'lang/'); |
281ffa4a |
61 | } |
3af29899 |
62 | asort($importnames); |
281ffa4a |
63 | } |
3af29899 |
64 | if (!empty($importnames)) { |
65 | $menu['importgroup']='--'.get_string('importplugins', 'grades'); |
66 | $menu = $menu+$importnames; |
281ffa4a |
67 | } |
281ffa4a |
68 | |
3af29899 |
69 | /// standard export plugins |
e2008be2 |
70 | if ($exports = get_list_of_plugins('grade/export', 'CVS')) { // Get all installed export plugins |
3af29899 |
71 | foreach ($exports as $key => $plugin) { // Remove ones we can't see |
72 | if (!has_capability('gradeexport/'.$plugin.':view', $context)) { |
73 | unset($exports[$key]); |
281ffa4a |
74 | } |
75 | } |
cbff94ba |
76 | } |
3af29899 |
77 | $exportnames = array(); |
78 | if (!empty($exports)) { |
79 | foreach ($exports as $plugin) { |
80 | $url = 'export/'.$plugin.'/index.php?id='.$courseid; |
65dd61bd |
81 | if ($active_type == 'export' and $active_plugin == $plugin ) { |
3af29899 |
82 | $active = $url; |
83 | } |
e2008be2 |
84 | $exportnames[$url] = get_string('modulename', 'gradeexport_'.$plugin, NULL, $CFG->dirroot.'/grade/export/'.$plugin.'lang/'); |
281ffa4a |
85 | } |
3af29899 |
86 | asort($exportnames); |
cbff94ba |
87 | } |
3af29899 |
88 | if (!empty($exportnames)) { |
89 | $menu['exportgroup']='--'.get_string('exportplugins', 'grades'); |
90 | $menu = $menu+$exportnames; |
281ffa4a |
91 | } |
cbff94ba |
92 | |
3af29899 |
93 | /// editing scripts - not real plugins |
94 | if (true) { //TODO: add proper capability here |
95 | $menu['edit']='--'.get_string('edit'); |
96 | $url = 'edit/tree.php?id='.$courseid; |
97 | if ($active_type == 'edit' and $active_plugin == 'tree' ) { |
98 | $active = $url; |
cbff94ba |
99 | } |
3af29899 |
100 | $menu[$url] = get_string('edittree', 'grades'); |
281ffa4a |
101 | } |
102 | |
3af29899 |
103 | /// finally print/return the popup form |
104 | return popup_form($CFG->wwwroot.'/grade/', $menu, 'choosepluginreport', $active, 'choose', '', '', $return, 'self', get_string('selectplugin', 'grades')); |
cbff94ba |
105 | } |
106 | |
0610812a |
107 | /** |
108 | * Utility class used for return tracking when using edit and other forms from grade plubins |
109 | */ |
3af29899 |
110 | class grade_plugin_return { |
111 | var $type; |
112 | var $plugin; |
113 | var $courseid; |
114 | var $userid; |
115 | var $page; |
281ffa4a |
116 | |
0610812a |
117 | /** |
118 | * Constructor |
119 | * @param array $params - associative array with return parameters, if null parameter are taken from _GET or _POST |
120 | */ |
3af29899 |
121 | function grade_plugin_return ($params=null) { |
122 | if (empty($params)) { |
123 | $this->type = optional_param('gpr_type', null, PARAM_SAFEDIR); |
124 | $this->plugin = optional_param('gpr_plugin', null, PARAM_SAFEDIR); |
125 | $this->courseid = optional_param('gpr_courseid', null, PARAM_INT); |
126 | $this->userid = optional_param('gpr_userid', null, PARAM_INT); |
127 | $this->page = optional_param('gpr_page', null, PARAM_INT); |
a983b6ec |
128 | |
a983b6ec |
129 | } else { |
3af29899 |
130 | foreach ($params as $key=>$value) { |
131 | if (array_key_exists($key, $this)) { |
132 | $this->$key = $value; |
133 | } |
cbff94ba |
134 | } |
135 | } |
6cd8c592 |
136 | } |
137 | |
0610812a |
138 | /** |
139 | * Returns return parameters as options array suitable for buttons. |
140 | * @return array options |
141 | */ |
3af29899 |
142 | function get_options() { |
143 | if (empty($this->type) or empty($this->plugin)) { |
144 | return array(); |
865e9a82 |
145 | } |
6cd8c592 |
146 | |
3af29899 |
147 | $params = array(); |
6cd8c592 |
148 | |
3af29899 |
149 | $params['plugin'] = $this->plugin; |
6cd8c592 |
150 | |
3af29899 |
151 | if (!empty($this->courseid)) { |
152 | $params['id'] = $this->courseid; |
6cd8c592 |
153 | } |
9c61ba4d |
154 | |
3af29899 |
155 | if (!empty($this->userid)) { |
156 | $params['userid'] = $this->userid; |
9c61ba4d |
157 | } |
9c61ba4d |
158 | |
3af29899 |
159 | if (!empty($this->page)) { |
160 | $params['page'] = $this->page; |
cbff94ba |
161 | } |
865e9a82 |
162 | |
3af29899 |
163 | return $params; |
cbff94ba |
164 | } |
cbff94ba |
165 | |
0610812a |
166 | /** |
167 | * Returns return url |
168 | * @param string $default default url when params not set |
169 | * @return string url |
170 | */ |
65dd61bd |
171 | function get_return_url($default, $extras=null) { |
3af29899 |
172 | global $CFG; |
cbff94ba |
173 | |
3af29899 |
174 | if (empty($this->type) or empty($this->plugin)) { |
175 | return $default; |
cbff94ba |
176 | } |
177 | |
65dd61bd |
178 | $url = $CFG->wwwroot.'/grade/'.$this->type.'/'.$this->plugin.'/index.php'; |
179 | $glue = '?'; |
cbff94ba |
180 | |
3af29899 |
181 | if (!empty($this->courseid)) { |
182 | $url .= $glue.'id='.$this->courseid; |
183 | $glue = '&'; |
cbff94ba |
184 | } |
cbff94ba |
185 | |
3af29899 |
186 | if (!empty($this->userid)) { |
187 | $url .= $glue.'userid='.$this->userid; |
188 | $glue = '&'; |
cbff94ba |
189 | } |
7e2d7c92 |
190 | |
3af29899 |
191 | if (!empty($this->page)) { |
192 | $url .= $glue.'page='.$this->page; |
65dd61bd |
193 | $glue = '&'; |
194 | } |
195 | |
196 | if (!empty($extras)) { |
197 | foreach($extras as $key=>$value) { |
198 | $url .= $glue.$key.'='.$value; |
199 | $glue = '&'; |
200 | } |
cbff94ba |
201 | } |
cbff94ba |
202 | |
3af29899 |
203 | return $url; |
cbff94ba |
204 | } |
cbff94ba |
205 | |
0610812a |
206 | /** |
207 | * Returns string with hidden return tracking form elements. |
208 | * @return string |
209 | */ |
3af29899 |
210 | function get_form_fields() { |
211 | if (empty($this->type) or empty($this->plugin)) { |
212 | return ''; |
cbff94ba |
213 | } |
cbff94ba |
214 | |
3af29899 |
215 | $result = '<input type="hidden" name="gpr_type" value="'.$this->type.'" />'; |
216 | $result .= '<input type="hidden" name="gpr_plugin" value="'.$this->plugin.'" />'; |
0ca5abd6 |
217 | |
3af29899 |
218 | if (!empty($this->courseid)) { |
219 | $result .= '<input type="hidden" name="gpr_courseid" value="'.$this->courseid.'" />'; |
cbff94ba |
220 | } |
cbff94ba |
221 | |
3af29899 |
222 | if (!empty($this->userid)) { |
223 | $result .= '<input type="hidden" name="gpr_userid" value="'.$this->userid.'" />'; |
cbff94ba |
224 | } |
cbff94ba |
225 | |
3af29899 |
226 | if (!empty($this->page)) { |
227 | $result .= '<input type="hidden" name="gpr_page" value="'.$this->page.'" />'; |
cbff94ba |
228 | } |
229 | } |
cbff94ba |
230 | |
0610812a |
231 | /** |
232 | * Add hidden elements into mform |
233 | * @param object $mform moodle form object |
234 | * @return void |
235 | */ |
3af29899 |
236 | function add_mform_elements(&$mform) { |
237 | if (empty($this->type) or empty($this->plugin)) { |
238 | return; |
cbff94ba |
239 | } |
cbff94ba |
240 | |
3af29899 |
241 | $mform->addElement('hidden', 'gpr_type', $this->type); |
242 | $mform->setType('gpr_type', PARAM_SAFEDIR); |
cbff94ba |
243 | |
3af29899 |
244 | $mform->addElement('hidden', 'gpr_plugin', $this->plugin); |
245 | $mform->setType('gpr_plugin', PARAM_SAFEDIR); |
97033c86 |
246 | |
3af29899 |
247 | if (!empty($this->courseid)) { |
248 | $mform->addElement('hidden', 'gpr_courseid', $this->courseid); |
249 | $mform->setType('gpr_courseid', PARAM_INT); |
cbff94ba |
250 | } |
cbff94ba |
251 | |
3af29899 |
252 | if (!empty($this->userid)) { |
253 | $mform->addElement('hidden', 'gpr_userid', $this->userid); |
254 | $mform->setType('gpr_userid', PARAM_INT); |
cbff94ba |
255 | } |
cbff94ba |
256 | |
3af29899 |
257 | if (!empty($this->page)) { |
258 | $mform->addElement('hidden', 'gpr_page', $this->page); |
259 | $mform->setType('gpr_page', PARAM_INT); |
cbff94ba |
260 | } |
261 | } |
281ffa4a |
262 | |
0610812a |
263 | /** |
264 | * Add return tracking params into url |
265 | * @param string $url |
266 | * @return string $url with erturn tracking params |
267 | */ |
3af29899 |
268 | function add_url_params($url) { |
269 | if (empty($this->type) or empty($this->plugin)) { |
270 | return $url; |
cbff94ba |
271 | } |
5609f9e6 |
272 | |
3af29899 |
273 | if (strpos($url, '?') === false) { |
274 | $url .= '?gpr_type='.$this->type; |
275 | } else { |
276 | $url .= '&gpr_type='.$this->type; |
cbff94ba |
277 | } |
cbff94ba |
278 | |
3af29899 |
279 | $url .= '&gpr_plugin='.$this->plugin; |
cbff94ba |
280 | |
3af29899 |
281 | if (!empty($this->courseid)) { |
282 | $url .= '&gpr_courseid='.$this->courseid; |
cbff94ba |
283 | } |
cbff94ba |
284 | |
3af29899 |
285 | if (!empty($this->userid)) { |
286 | $url .= '&gpr_userid='.$this->userid; |
cbff94ba |
287 | } |
0a8a95c9 |
288 | |
3af29899 |
289 | if (!empty($this->page)) { |
290 | $url .= '&gpr_page='.$this->page; |
0a8a95c9 |
291 | } |
5a412dbf |
292 | |
3af29899 |
293 | return $url; |
5a412dbf |
294 | } |
5a412dbf |
295 | } |
e2008be2 |
296 | ?> |