Re-applying one fix that was fixed some time ago (rev 1.52) and
[moodle.git] / grade / lib.php
CommitLineData
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 12function 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) {
32 $url = 'report.php?id='.$courseid.'&amp;report='.$plugin;
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;
57 if ($active_type == 'impot' and $active_plugin == $plugin ) {
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;
81 if ($active_type == 'impot' and $active_plugin == $plugin ) {
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 110class 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 */
3af29899 171 function get_return_url($default) {
172 global $CFG;
cbff94ba 173
3af29899 174 if (empty($this->type) or empty($this->plugin)) {
175 return $default;
cbff94ba 176 }
177
3af29899 178 if ($this->type == 'report') {
179 $url = $CFG->wwwroot.'/grade/report.php?report='.$this->plugin;
180 $glue = '&amp;';
cbff94ba 181
3af29899 182 } else {
183 $url = $CFG->wwwroot.'/grade/'.$this->type.'/'.$this->plugin.'/index.php';
184 $glue = '?';
cbff94ba 185 }
cbff94ba 186
3af29899 187 if (!empty($this->courseid)) {
188 $url .= $glue.'id='.$this->courseid;
189 $glue = '&amp;';
cbff94ba 190 }
cbff94ba 191
3af29899 192 if (!empty($this->userid)) {
193 $url .= $glue.'userid='.$this->userid;
194 $glue = '&amp;';
cbff94ba 195 }
7e2d7c92 196
3af29899 197 if (!empty($this->page)) {
198 $url .= $glue.'page='.$this->page;
cbff94ba 199 }
cbff94ba 200
3af29899 201 return $url;
cbff94ba 202 }
cbff94ba 203
0610812a 204 /**
205 * Returns string with hidden return tracking form elements.
206 * @return string
207 */
3af29899 208 function get_form_fields() {
209 if (empty($this->type) or empty($this->plugin)) {
210 return '';
cbff94ba 211 }
cbff94ba 212
3af29899 213 $result = '<input type="hidden" name="gpr_type" value="'.$this->type.'" />';
214 $result .= '<input type="hidden" name="gpr_plugin" value="'.$this->plugin.'" />';
0ca5abd6 215
3af29899 216 if (!empty($this->courseid)) {
217 $result .= '<input type="hidden" name="gpr_courseid" value="'.$this->courseid.'" />';
cbff94ba 218 }
cbff94ba 219
3af29899 220 if (!empty($this->userid)) {
221 $result .= '<input type="hidden" name="gpr_userid" value="'.$this->userid.'" />';
cbff94ba 222 }
cbff94ba 223
3af29899 224 if (!empty($this->page)) {
225 $result .= '<input type="hidden" name="gpr_page" value="'.$this->page.'" />';
cbff94ba 226 }
227 }
cbff94ba 228
0610812a 229 /**
230 * Add hidden elements into mform
231 * @param object $mform moodle form object
232 * @return void
233 */
3af29899 234 function add_mform_elements(&$mform) {
235 if (empty($this->type) or empty($this->plugin)) {
236 return;
cbff94ba 237 }
cbff94ba 238
3af29899 239 $mform->addElement('hidden', 'gpr_type', $this->type);
240 $mform->setType('gpr_type', PARAM_SAFEDIR);
cbff94ba 241
3af29899 242 $mform->addElement('hidden', 'gpr_plugin', $this->plugin);
243 $mform->setType('gpr_plugin', PARAM_SAFEDIR);
97033c86 244
3af29899 245 if (!empty($this->courseid)) {
246 $mform->addElement('hidden', 'gpr_courseid', $this->courseid);
247 $mform->setType('gpr_courseid', PARAM_INT);
cbff94ba 248 }
cbff94ba 249
3af29899 250 if (!empty($this->userid)) {
251 $mform->addElement('hidden', 'gpr_userid', $this->userid);
252 $mform->setType('gpr_userid', PARAM_INT);
cbff94ba 253 }
cbff94ba 254
3af29899 255 if (!empty($this->page)) {
256 $mform->addElement('hidden', 'gpr_page', $this->page);
257 $mform->setType('gpr_page', PARAM_INT);
cbff94ba 258 }
259 }
281ffa4a 260
0610812a 261 /**
262 * Add return tracking params into url
263 * @param string $url
264 * @return string $url with erturn tracking params
265 */
3af29899 266 function add_url_params($url) {
267 if (empty($this->type) or empty($this->plugin)) {
268 return $url;
cbff94ba 269 }
5609f9e6 270
3af29899 271 if (strpos($url, '?') === false) {
272 $url .= '?gpr_type='.$this->type;
273 } else {
274 $url .= '&amp;gpr_type='.$this->type;
cbff94ba 275 }
cbff94ba 276
3af29899 277 $url .= '&amp;gpr_plugin='.$this->plugin;
cbff94ba 278
3af29899 279 if (!empty($this->courseid)) {
280 $url .= '&amp;gpr_courseid='.$this->courseid;
cbff94ba 281 }
cbff94ba 282
3af29899 283 if (!empty($this->userid)) {
284 $url .= '&amp;gpr_userid='.$this->userid;
cbff94ba 285 }
0a8a95c9 286
3af29899 287 if (!empty($this->page)) {
288 $url .= '&amp;gpr_page='.$this->page;
0a8a95c9 289 }
5a412dbf 290
3af29899 291 return $url;
5a412dbf 292 }
5a412dbf 293}
e2008be2 294?>