Merge branch 'MDL-36482_24' of https://github.com/mr-russ/moodle into MOODLE_24_STABLE
[moodle.git] / admin / portfolio.php
blob16aa66183edf98f17107ccd17ef4e9a02b0bd9f7
1 <?php
3 require_once(dirname(dirname(__FILE__)) . '/config.php');
4 require_once($CFG->libdir . '/portfoliolib.php');
5 require_once($CFG->libdir . '/portfolio/forms.php');
6 require_once($CFG->libdir . '/adminlib.php');
8 $portfolio = optional_param('pf', '', PARAM_ALPHANUMEXT);
9 $action = optional_param('action', '', PARAM_ALPHA);
10 $sure = optional_param('sure', '', PARAM_ALPHA);
12 $display = true; // fall through to normal display
14 $pagename = 'manageportfolios';
16 if ($action == 'edit') {
17 $pagename = 'portfoliosettings' . $portfolio;
18 } else if ($action == 'delete') {
19 $pagename = 'portfoliodelete';
20 } else if (($action == 'newon') || ($action == 'newoff')) {
21 $pagename = 'portfolionew';
24 // Need to remember this for form
25 $formaction = $action;
27 // Check what visibility to show the new repository
28 if ($action == 'newon') {
29 $action = 'new';
30 $visible = 1;
31 } else if ($action == 'newoff') {
32 $action = 'new';
33 $visible = 0;
36 admin_externalpage_setup($pagename);
38 require_capability('moodle/site:config', context_system::instance());
40 $baseurl = "$CFG->wwwroot/$CFG->admin/portfolio.php";
41 $sesskeyurl = "$CFG->wwwroot/$CFG->admin/portfolio.php?sesskey=" . sesskey();
42 $configstr = get_string('manageportfolios', 'portfolio');
44 $return = true; // direct back to the main page
46 /**
47 * Helper function that generates a moodle_url object
48 * relevant to the portfolio
50 function portfolio_action_url($portfolio) {
51 global $baseurl;
52 return new moodle_url($baseurl, array('sesskey'=>sesskey(), 'pf'=>$portfolio));
55 if (($action == 'edit') || ($action == 'new')) {
56 if (($action == 'edit')) {
57 $instance = portfolio_instance($portfolio);
58 $plugin = $instance->get('plugin');
60 // Since visible is being passed to form
61 // and used to set the value when a new
62 // instance is created - we must also
63 // place the currently visibility into the
64 // form as well
65 $visible = $instance->get('visible');
66 } else {
67 $instance = null;
68 $plugin = $portfolio;
71 $PAGE->set_pagetype('admin-portfolio-' . $plugin);
73 // Display the edit form for this instance
74 $mform = new portfolio_admin_form('', array('plugin' => $plugin, 'instance' => $instance, 'portfolio' => $portfolio, 'action' => $formaction, 'visible' => $visible));
75 // End setup, begin output
76 if ($mform->is_cancelled()){
77 redirect($baseurl);
78 exit;
79 } else if (($fromform = $mform->get_data()) && (confirm_sesskey())) {
80 // Unset whatever doesn't belong in fromform
81 foreach (array('pf', 'action', 'plugin', 'sesskey', 'submitbutton') as $key) {
82 unset($fromform->{$key});
84 // This branch is where you process validated data.
85 if ($action == 'edit') {
86 $instance->set_config((array)$fromform);
87 $instance->save();
88 } else {
89 portfolio_static_function($plugin, 'create_instance', $plugin, $fromform->name, $fromform);
91 $savedstr = get_string('instancesaved', 'portfolio');
92 redirect($baseurl, $savedstr, 1);
93 exit;
94 } else {
95 echo $OUTPUT->header();
96 echo $OUTPUT->heading(get_string('configplugin', 'portfolio'));
97 echo $OUTPUT->box_start();
98 $mform->display();
99 echo $OUTPUT->box_end();
100 $return = false;
102 } else if (($action == 'hide') || ($action == 'show')) {
103 require_sesskey();
105 $instance = portfolio_instance($portfolio);
106 $current = $instance->get('visible');
107 if (empty($current) && $instance->instance_sanity_check()) {
108 print_error('cannotsetvisible', 'portfolio', $baseurl);
111 if ($action == 'show') {
112 $visible = 1;
113 } else {
114 $visible = 0;
117 $instance->set('visible', $visible);
118 $instance->save();
119 $return = true;
120 } else if ($action == 'delete') {
121 $instance = portfolio_instance($portfolio);
122 if ($sure) {
123 if (!confirm_sesskey()) {
124 print_error('confirmsesskeybad', '', $baseurl);
126 if ($instance->delete()) {
127 $deletedstr = get_string('instancedeleted', 'portfolio');
128 redirect($baseurl, $deletedstr, 1);
129 } else {
130 print_error('instancenotdeleted', 'portfolio', $baseurl);
132 exit;
133 } else {
134 echo $OUTPUT->header();
135 echo $OUTPUT->confirm(get_string('sure', 'portfolio', $instance->get('name')), $sesskeyurl . '&pf='.$portfolio.'&action=delete&sure=yes', $baseurl);
136 $return = false;
138 } else {
139 // If page is loaded directly
140 echo $OUTPUT->header();
141 echo $OUTPUT->heading(get_string('manageportfolios', 'portfolio'));
143 // Get strings that are used
144 $strshow = get_string('on', 'portfolio');
145 $strhide = get_string('off', 'portfolio');
146 $strdelete = get_string('disabledinstance', 'portfolio');
147 $strsettings = get_string('settings');
149 $actionchoicesforexisting = array(
150 'show' => $strshow,
151 'hide' => $strhide,
152 'delete' => $strdelete
155 $actionchoicesfornew = array(
156 'newon' => $strshow,
157 'newoff' => $strhide,
158 'delete' => $strdelete
161 $output = $OUTPUT->box_start('generalbox');
163 $plugins = get_plugin_list('portfolio');
164 $plugins = array_keys($plugins);
165 $instances = portfolio_instances(false, false);
166 $usedplugins = array();
168 // to avoid notifications being sent out while admin is editing the page
169 define('ADMIN_EDITING_PORTFOLIO', true);
171 $insane = portfolio_plugin_sanity_check($plugins);
172 $insaneinstances = portfolio_instance_sanity_check($instances);
174 $table = new html_table();
175 $table->head = array(get_string('plugin', 'portfolio'), '', '');
176 $table->data = array();
178 foreach ($instances as $i) {
179 $settings = '<a href="' . $sesskeyurl . '&amp;action=edit&amp;pf=' . $i->get('id') . '">' . $strsettings .'</a>';
180 // Set some commonly used variables
181 $pluginid = $i->get('id');
182 $plugin = $i->get('plugin');
183 $pluginname = $i->get('name');
185 // Check if the instance is misconfigured
186 if (array_key_exists($plugin, $insane) || array_key_exists($pluginid, $insaneinstances)) {
187 if (!empty($insane[$plugin])) {
188 $information = $insane[$plugin];
189 } else if (!empty($insaneinstances[$pluginid])) {
190 $information = $insaneinstances[$pluginid];
192 $table->data[] = array($pluginname, $strdelete . " " . $OUTPUT->help_icon($information, 'portfolio_' . $plugin), $settings);
193 } else {
194 if ($i->get('visible')) {
195 $currentaction = 'show';
196 } else {
197 $currentaction = 'hide';
199 $select = new single_select(portfolio_action_url($pluginid, 'pf'), 'action', $actionchoicesforexisting, $currentaction, null, 'applyto' . $pluginid);
200 $select->set_label(get_string('action'), array('class' => 'accesshide'));
201 $table->data[] = array($pluginname, $OUTPUT->render($select), $settings);
203 if (!in_array($plugin, $usedplugins)) {
204 $usedplugins[] = $plugin;
208 // Create insane plugin array
209 $insaneplugins = array();
210 if (!empty($plugins)) {
211 foreach ($plugins as $p) {
212 // Check if it can not have multiple instances and has already been used
213 if (!portfolio_static_function($p, 'allows_multiple_instances') && in_array($p, $usedplugins)) {
214 continue;
217 // Check if it is misconfigured - if so store in array then display later
218 if (array_key_exists($p, $insane)) {
219 $insaneplugins[] = $p;
220 } else {
221 $select = new single_select(portfolio_action_url($p, 'pf'), 'action', $actionchoicesfornew, 'delete', null, 'applyto' . $p);
222 $select->set_label(get_string('action'), array('class' => 'accesshide'));
223 $table->data[] = array(portfolio_static_function($p, 'get_name'), $OUTPUT->render($select), '');
228 // Loop through all the insane plugins
229 if (!empty($insaneplugins)) {
230 foreach ($insaneplugins as $p) {
231 $table->data[] = array(portfolio_static_function($p, 'get_name'), $strdelete . " " . $OUTPUT->help_icon($insane[$p], 'portfolio_' . $p), '');
235 $output .= html_writer::table($table);
237 $output .= $OUTPUT->box_end();
239 echo $output;
240 $return = false;
243 if ($return) {
244 // Redirect to base
245 redirect($baseurl);
248 echo $OUTPUT->footer();