Merge branch 'MDL-38957_master' of git://github.com/dmonllao/moodle
[moodle.git] / admin / portfolio.php
blob1811f5f774fdeafb96421af598df06f18c1eb6b2
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 // Purge all caches related to portfolio administration.
47 cache::make('core', 'plugininfo_portfolio')->purge();
49 /**
50 * Helper function that generates a moodle_url object
51 * relevant to the portfolio
53 function portfolio_action_url($portfolio) {
54 global $baseurl;
55 return new moodle_url($baseurl, array('sesskey'=>sesskey(), 'pf'=>$portfolio));
58 if (($action == 'edit') || ($action == 'new')) {
59 if (($action == 'edit')) {
60 $instance = portfolio_instance($portfolio);
61 $plugin = $instance->get('plugin');
63 // Since visible is being passed to form
64 // and used to set the value when a new
65 // instance is created - we must also
66 // place the currently visibility into the
67 // form as well
68 $visible = $instance->get('visible');
69 } else {
70 $instance = null;
71 $plugin = $portfolio;
74 $PAGE->set_pagetype('admin-portfolio-' . $plugin);
76 // Display the edit form for this instance
77 $mform = new portfolio_admin_form('', array('plugin' => $plugin, 'instance' => $instance, 'portfolio' => $portfolio, 'action' => $formaction, 'visible' => $visible));
78 // End setup, begin output
79 if ($mform->is_cancelled()){
80 redirect($baseurl);
81 exit;
82 } else if (($fromform = $mform->get_data()) && (confirm_sesskey())) {
83 // Unset whatever doesn't belong in fromform
84 foreach (array('pf', 'action', 'plugin', 'sesskey', 'submitbutton') as $key) {
85 unset($fromform->{$key});
87 // This branch is where you process validated data.
88 if ($action == 'edit') {
89 $instance->set_config((array)$fromform);
90 $instance->save();
91 } else {
92 portfolio_static_function($plugin, 'create_instance', $plugin, $fromform->name, $fromform);
94 $savedstr = get_string('instancesaved', 'portfolio');
95 redirect($baseurl, $savedstr, 1);
96 exit;
97 } else {
98 echo $OUTPUT->header();
99 echo $OUTPUT->heading(get_string('configplugin', 'portfolio'));
100 echo $OUTPUT->box_start();
101 $mform->display();
102 echo $OUTPUT->box_end();
103 $return = false;
105 } else if (($action == 'hide') || ($action == 'show')) {
106 require_sesskey();
108 $instance = portfolio_instance($portfolio);
109 $current = $instance->get('visible');
110 if (empty($current) && $instance->instance_sanity_check()) {
111 print_error('cannotsetvisible', 'portfolio', $baseurl);
114 if ($action == 'show') {
115 $visible = 1;
116 } else {
117 $visible = 0;
120 $instance->set('visible', $visible);
121 $instance->save();
122 $return = true;
123 } else if ($action == 'delete') {
124 $instance = portfolio_instance($portfolio);
125 if ($sure) {
126 if (!confirm_sesskey()) {
127 print_error('confirmsesskeybad', '', $baseurl);
129 if ($instance->delete()) {
130 $deletedstr = get_string('instancedeleted', 'portfolio');
131 redirect($baseurl, $deletedstr, 1);
132 } else {
133 print_error('instancenotdeleted', 'portfolio', $baseurl);
135 exit;
136 } else {
137 echo $OUTPUT->header();
138 echo $OUTPUT->confirm(get_string('sure', 'portfolio', $instance->get('name')), $sesskeyurl . '&pf='.$portfolio.'&action=delete&sure=yes', $baseurl);
139 $return = false;
141 } else {
142 // If page is loaded directly
143 echo $OUTPUT->header();
144 echo $OUTPUT->heading(get_string('manageportfolios', 'portfolio'));
146 // Get strings that are used
147 $strshow = get_string('on', 'portfolio');
148 $strhide = get_string('off', 'portfolio');
149 $strdelete = get_string('disabledinstance', 'portfolio');
150 $strsettings = get_string('settings');
152 $actionchoicesforexisting = array(
153 'show' => $strshow,
154 'hide' => $strhide,
155 'delete' => $strdelete
158 $actionchoicesfornew = array(
159 'newon' => $strshow,
160 'newoff' => $strhide,
161 'delete' => $strdelete
164 $output = $OUTPUT->box_start('generalbox');
166 $plugins = get_plugin_list('portfolio');
167 $plugins = array_keys($plugins);
168 $instances = portfolio_instances(false, false);
169 $usedplugins = array();
171 // to avoid notifications being sent out while admin is editing the page
172 define('ADMIN_EDITING_PORTFOLIO', true);
174 $insane = portfolio_plugin_sanity_check($plugins);
175 $insaneinstances = portfolio_instance_sanity_check($instances);
177 $table = new html_table();
178 $table->head = array(get_string('plugin', 'portfolio'), '', '');
179 $table->data = array();
181 foreach ($instances as $i) {
182 $settings = '<a href="' . $sesskeyurl . '&amp;action=edit&amp;pf=' . $i->get('id') . '">' . $strsettings .'</a>';
183 // Set some commonly used variables
184 $pluginid = $i->get('id');
185 $plugin = $i->get('plugin');
186 $pluginname = $i->get('name');
188 // Check if the instance is misconfigured
189 if (array_key_exists($plugin, $insane) || array_key_exists($pluginid, $insaneinstances)) {
190 if (!empty($insane[$plugin])) {
191 $information = $insane[$plugin];
192 } else if (!empty($insaneinstances[$pluginid])) {
193 $information = $insaneinstances[$pluginid];
195 $table->data[] = array($pluginname, $strdelete . " " . $OUTPUT->help_icon($information, 'portfolio_' . $plugin), $settings);
196 } else {
197 if ($i->get('visible')) {
198 $currentaction = 'show';
199 } else {
200 $currentaction = 'hide';
202 $select = new single_select(portfolio_action_url($pluginid, 'pf'), 'action', $actionchoicesforexisting, $currentaction, null, 'applyto' . $pluginid);
203 $select->set_label(get_string('action'), array('class' => 'accesshide'));
204 $table->data[] = array($pluginname, $OUTPUT->render($select), $settings);
206 if (!in_array($plugin, $usedplugins)) {
207 $usedplugins[] = $plugin;
211 // Create insane plugin array
212 $insaneplugins = array();
213 if (!empty($plugins)) {
214 foreach ($plugins as $p) {
215 // Check if it can not have multiple instances and has already been used
216 if (!portfolio_static_function($p, 'allows_multiple_instances') && in_array($p, $usedplugins)) {
217 continue;
220 // Check if it is misconfigured - if so store in array then display later
221 if (array_key_exists($p, $insane)) {
222 $insaneplugins[] = $p;
223 } else {
224 $select = new single_select(portfolio_action_url($p, 'pf'), 'action', $actionchoicesfornew, 'delete', null, 'applyto' . $p);
225 $select->set_label(get_string('action'), array('class' => 'accesshide'));
226 $table->data[] = array(portfolio_static_function($p, 'get_name'), $OUTPUT->render($select), '');
231 // Loop through all the insane plugins
232 if (!empty($insaneplugins)) {
233 foreach ($insaneplugins as $p) {
234 $table->data[] = array(portfolio_static_function($p, 'get_name'), $strdelete . " " . $OUTPUT->help_icon($insane[$p], 'portfolio_' . $p), '');
238 $output .= html_writer::table($table);
240 $output .= $OUTPUT->box_end();
242 echo $output;
243 $return = false;
246 if ($return) {
247 // Redirect to base
248 redirect($baseurl);
251 echo $OUTPUT->footer();