portfolio.php 8.6 KB

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