Merge branch 'MDL-62660' of https://github.com/stronk7/moodle
[moodle.git] / admin / portfolio.php
blob6a33fc0eea03f19a6b2e3499ba5d5afc12667406
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 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 core_plugin_manager::reset_caches();
92 $savedstr = get_string('instancesaved', 'portfolio');
93 redirect($baseurl, $savedstr, 1);
94 exit;
95 } else {
96 echo $OUTPUT->header();
97 echo $OUTPUT->heading(get_string('configplugin', 'portfolio'));
98 echo $OUTPUT->box_start();
99 $mform->display();
100 echo $OUTPUT->box_end();
101 $return = false;
103 } else if (($action == 'hide') || ($action == 'show')) {
104 require_sesskey();
106 $instance = portfolio_instance($portfolio);
107 $current = $instance->get('visible');
108 if (empty($current) && $instance->instance_sanity_check()) {
109 print_error('cannotsetvisible', 'portfolio', $baseurl);
112 if ($action == 'show') {
113 $visible = 1;
114 } else {
115 $visible = 0;
118 $instance->set('visible', $visible);
119 $instance->save();
120 core_plugin_manager::reset_caches();
121 $return = true;
122 } else if ($action == 'delete') {
123 $instance = portfolio_instance($portfolio);
124 if ($sure) {
125 if (!confirm_sesskey()) {
126 print_error('confirmsesskeybad', '', $baseurl);
128 if ($instance->delete()) {
129 $deletedstr = get_string('instancedeleted', 'portfolio');
130 redirect($baseurl, $deletedstr, 1);
131 } else {
132 print_error('instancenotdeleted', 'portfolio', $baseurl);
134 exit;
135 } else {
136 echo $OUTPUT->header();
137 echo $OUTPUT->confirm(get_string('sure', 'portfolio', $instance->get('name')), $sesskeyurl . '&pf='.$portfolio.'&action=delete&sure=yes', $baseurl);
138 $return = false;
140 } else {
141 // If page is loaded directly
142 echo $OUTPUT->header();
143 echo $OUTPUT->heading(get_string('manageportfolios', 'portfolio'));
145 // Get strings that are used
146 $strshow = get_string('on', 'portfolio');
147 $strhide = get_string('off', 'portfolio');
148 $strdelete = get_string('disabledinstance', 'portfolio');
149 $strsettings = get_string('settings');
151 $actionchoicesforexisting = array(
152 'show' => $strshow,
153 'hide' => $strhide,
154 'delete' => $strdelete
157 $actionchoicesfornew = array(
158 'newon' => $strshow,
159 'newoff' => $strhide,
160 'delete' => $strdelete
163 $output = $OUTPUT->box_start('generalbox');
165 $plugins = core_component::get_plugin_list('portfolio');
166 $plugins = array_keys($plugins);
167 $instances = portfolio_instances(false, false);
168 $usedplugins = array();
170 // to avoid notifications being sent out while admin is editing the page
171 define('ADMIN_EDITING_PORTFOLIO', true);
173 $insane = portfolio_plugin_sanity_check($plugins);
174 $insaneinstances = portfolio_instance_sanity_check($instances);
176 $table = new html_table();
177 $table->head = array(get_string('plugin', 'portfolio'), '', '');
178 $table->data = array();
180 foreach ($instances as $i) {
181 $settings = '<a href="' . $sesskeyurl . '&amp;action=edit&amp;pf=' . $i->get('id') . '">' . $strsettings .'</a>';
182 // Set some commonly used variables
183 $pluginid = $i->get('id');
184 $plugin = $i->get('plugin');
185 $pluginname = $i->get('name');
187 // Check if the instance is misconfigured
188 if (array_key_exists($plugin, $insane) || array_key_exists($pluginid, $insaneinstances)) {
189 if (!empty($insane[$plugin])) {
190 $information = $insane[$plugin];
191 } else if (!empty($insaneinstances[$pluginid])) {
192 $information = $insaneinstances[$pluginid];
194 $table->data[] = array($pluginname, $strdelete . " " . $OUTPUT->help_icon($information, 'portfolio_' . $plugin), $settings);
195 } else {
196 if ($i->get('visible')) {
197 $currentaction = 'show';
198 } else {
199 $currentaction = 'hide';
201 $select = new single_select(portfolio_action_url($pluginid, 'pf'), 'action', $actionchoicesforexisting, $currentaction, null, 'applyto' . $pluginid);
202 $select->set_label(get_string('action'), array('class' => 'accesshide'));
203 $table->data[] = array($pluginname, $OUTPUT->render($select), $settings);
205 if (!in_array($plugin, $usedplugins)) {
206 $usedplugins[] = $plugin;
210 // Create insane plugin array
211 $insaneplugins = array();
212 if (!empty($plugins)) {
213 foreach ($plugins as $p) {
214 // Check if it can not have multiple instances and has already been used
215 if (!portfolio_static_function($p, 'allows_multiple_instances') && in_array($p, $usedplugins)) {
216 continue;
219 // Check if it is misconfigured - if so store in array then display later
220 if (array_key_exists($p, $insane)) {
221 $insaneplugins[] = $p;
222 } else {
223 $select = new single_select(portfolio_action_url($p, 'pf'), 'action', $actionchoicesfornew, 'delete', null, 'applyto' . $p);
224 $select->set_label(get_string('action'), array('class' => 'accesshide'));
225 $table->data[] = array(portfolio_static_function($p, 'get_name'), $OUTPUT->render($select), '');
230 // Loop through all the insane plugins
231 if (!empty($insaneplugins)) {
232 foreach ($insaneplugins as $p) {
233 $table->data[] = array(portfolio_static_function($p, 'get_name'), $strdelete . " " . $OUTPUT->help_icon($insane[$p], 'portfolio_' . $p), '');
237 $output .= html_writer::table($table);
239 $output .= $OUTPUT->box_end();
241 echo $output;
242 $return = false;
245 if ($return) {
246 // Redirect to base
247 redirect($baseurl);
250 echo $OUTPUT->footer();