Merge branch 'MDL-71187-39' of git://github.com/lucaboesch/moodle into MOODLE_39_STABLE
[moodle.git] / admin / portfolio.php
blob66ff3a71ebf9623165f9d0e4b37c95a9bc76cdc8
1 <?php
3 require_once(__DIR__ . '/../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 $baseurl = "$CFG->wwwroot/$CFG->admin/portfolio.php";
39 $sesskeyurl = "$CFG->wwwroot/$CFG->admin/portfolio.php?sesskey=" . sesskey();
40 $configstr = get_string('manageportfolios', 'portfolio');
42 $return = true; // direct back to the main page
44 /**
45 * Helper function that generates a moodle_url object
46 * relevant to the portfolio
48 function portfolio_action_url($portfolio) {
49 global $baseurl;
50 return new moodle_url($baseurl, array('sesskey'=>sesskey(), 'pf'=>$portfolio));
53 if (($action == 'edit') || ($action == 'new')) {
54 if (($action == 'edit')) {
55 $instance = portfolio_instance($portfolio);
56 $plugin = $instance->get('plugin');
58 // Since visible is being passed to form
59 // and used to set the value when a new
60 // instance is created - we must also
61 // place the currently visibility into the
62 // form as well
63 $visible = $instance->get('visible');
64 } else {
65 $instance = null;
66 $plugin = $portfolio;
69 $PAGE->set_pagetype('admin-portfolio-' . $plugin);
71 // Display the edit form for this instance
72 $mform = new portfolio_admin_form('', array('plugin' => $plugin, 'instance' => $instance, 'portfolio' => $portfolio, 'action' => $formaction, 'visible' => $visible));
73 // End setup, begin output
74 if ($mform->is_cancelled()){
75 redirect($baseurl);
76 exit;
77 } else if (($fromform = $mform->get_data()) && (confirm_sesskey())) {
78 // Unset whatever doesn't belong in fromform
79 foreach (array('pf', 'action', 'plugin', 'sesskey', 'submitbutton') as $key) {
80 unset($fromform->{$key});
82 // This branch is where you process validated data.
83 if ($action == 'edit') {
84 $instance->set_config((array)$fromform);
85 $instance->save();
86 } else {
87 portfolio_static_function($plugin, 'create_instance', $plugin, $fromform->name, $fromform);
89 core_plugin_manager::reset_caches();
90 $savedstr = get_string('instancesaved', 'portfolio');
91 redirect($baseurl, $savedstr, 1);
92 exit;
93 } else {
94 echo $OUTPUT->header();
95 echo $OUTPUT->heading(get_string('configplugin', 'portfolio'));
96 echo $OUTPUT->box_start();
97 $mform->display();
98 echo $OUTPUT->box_end();
99 $return = false;
101 } else if (($action == 'hide') || ($action == 'show')) {
102 require_sesskey();
104 $instance = portfolio_instance($portfolio);
105 $current = $instance->get('visible');
106 if (empty($current) && $instance->instance_sanity_check()) {
107 print_error('cannotsetvisible', 'portfolio', $baseurl);
110 if ($action == 'show') {
111 $visible = 1;
112 } else {
113 $visible = 0;
116 $instance->set('visible', $visible);
117 $instance->save();
118 core_plugin_manager::reset_caches();
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 = core_component::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();