Merge branch 'MDL-81073' of https://github.com/paulholden/moodle
[moodle.git] / admin / portfolio.php
blob08939c173d578cad19df9fbb020571abde412c7b
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 $plugin = $instance->get('plugin');
106 if ($action == 'show') {
107 $visible = 1;
108 } else {
109 $visible = 0;
112 $class = \core_plugin_manager::resolve_plugininfo_class('portfolio');
113 $class::enable_plugin($plugin, $visible);
114 $return = true;
115 } else if ($action == 'delete') {
116 $instance = portfolio_instance($portfolio);
117 if ($sure) {
118 if (!confirm_sesskey()) {
119 throw new \moodle_exception('confirmsesskeybad', '', $baseurl);
121 if ($instance->delete()) {
122 $deletedstr = get_string('instancedeleted', 'portfolio');
123 redirect($baseurl, $deletedstr, 1);
124 } else {
125 throw new \moodle_exception('instancenotdeleted', 'portfolio', $baseurl);
127 exit;
128 } else {
129 echo $OUTPUT->header();
130 echo $OUTPUT->confirm(get_string('sure', 'portfolio', $instance->get('name')), $sesskeyurl . '&pf='.$portfolio.'&action=delete&sure=yes', $baseurl);
131 $return = false;
133 } else {
134 // If page is loaded directly
135 echo $OUTPUT->header();
136 echo $OUTPUT->heading(get_string('manageportfolios', 'portfolio'));
138 // Get strings that are used
139 $strshow = get_string('on', 'portfolio');
140 $strhide = get_string('off', 'portfolio');
141 $strdelete = get_string('disabledinstance', 'portfolio');
142 $strsettings = get_string('settings');
144 $actionchoicesforexisting = array(
145 'show' => $strshow,
146 'hide' => $strhide,
147 'delete' => $strdelete
150 $actionchoicesfornew = array(
151 'newon' => $strshow,
152 'newoff' => $strhide,
153 'delete' => $strdelete
156 $output = $OUTPUT->box_start('generalbox');
158 $plugins = core_component::get_plugin_list('portfolio');
159 $plugins = array_keys($plugins);
160 $instances = portfolio_instances(false, false);
161 $usedplugins = array();
163 // to avoid notifications being sent out while admin is editing the page
164 define('ADMIN_EDITING_PORTFOLIO', true);
166 $insane = portfolio_plugin_sanity_check($plugins);
167 $insaneinstances = portfolio_instance_sanity_check($instances);
169 $table = new html_table();
170 $table->head = array(get_string('plugin', 'portfolio'), '', '');
171 $table->data = array();
173 foreach ($instances as $i) {
174 $settings = '<a href="' . $sesskeyurl . '&amp;action=edit&amp;pf=' . $i->get('id') . '">' . $strsettings .'</a>';
175 // Set some commonly used variables
176 $pluginid = $i->get('id');
177 $plugin = $i->get('plugin');
178 $pluginname = $i->get('name');
180 // Check if the instance is misconfigured
181 if (array_key_exists($plugin, $insane) || array_key_exists($pluginid, $insaneinstances)) {
182 if (!empty($insane[$plugin])) {
183 $information = $insane[$plugin];
184 } else if (!empty($insaneinstances[$pluginid])) {
185 $information = $insaneinstances[$pluginid];
187 $table->data[] = array($pluginname, $strdelete . " " . $OUTPUT->help_icon($information, 'portfolio_' . $plugin), $settings);
188 } else {
189 if ($i->get('visible')) {
190 $currentaction = 'show';
191 } else {
192 $currentaction = 'hide';
194 $select = new single_select(portfolio_action_url($pluginid, 'pf'), 'action', $actionchoicesforexisting, $currentaction, null, 'applyto' . $pluginid);
195 $select->set_label(get_string('action'), array('class' => 'accesshide'));
196 $table->data[] = array($pluginname, $OUTPUT->render($select), $settings);
198 if (!in_array($plugin, $usedplugins)) {
199 $usedplugins[] = $plugin;
203 // Create insane plugin array
204 $insaneplugins = array();
205 if (!empty($plugins)) {
206 foreach ($plugins as $p) {
207 // Check if it can not have multiple instances and has already been used
208 if (!portfolio_static_function($p, 'allows_multiple_instances') && in_array($p, $usedplugins)) {
209 continue;
212 // Check if it is misconfigured - if so store in array then display later
213 if (array_key_exists($p, $insane)) {
214 $insaneplugins[] = $p;
215 } else {
216 $select = new single_select(portfolio_action_url($p, 'pf'), 'action', $actionchoicesfornew, 'delete', null, 'applyto' . $p);
217 $select->set_label(get_string('action'), array('class' => 'accesshide'));
218 $table->data[] = array(portfolio_static_function($p, 'get_name'), $OUTPUT->render($select), '');
223 // Loop through all the insane plugins
224 if (!empty($insaneplugins)) {
225 foreach ($insaneplugins as $p) {
226 $table->data[] = array(portfolio_static_function($p, 'get_name'), $strdelete . " " . $OUTPUT->help_icon($insane[$p], 'portfolio_' . $p), '');
230 $output .= html_writer::table($table);
232 $output .= $OUTPUT->box_end();
234 echo $output;
235 $return = false;
238 if ($return) {
239 // Redirect to base
240 redirect($baseurl);
243 echo $OUTPUT->footer();