Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   1  <?php
   2  
   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');
   7  
   8  $portfolio     = optional_param('pf', '', PARAM_ALPHANUMEXT);
   9  $action        = optional_param('action', '', PARAM_ALPHA);
  10  $sure          = optional_param('sure', '', PARAM_ALPHA);
  11  
  12  $display = true; // fall through to normal display
  13  
  14  $pagename = 'manageportfolios';
  15  
  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';
  22  }
  23  
  24  // Need to remember this for form
  25  $formaction = $action;
  26  
  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;
  34  }
  35  
  36  admin_externalpage_setup($pagename);
  37  
  38  $baseurl    = "$CFG->wwwroot/$CFG->admin/portfolio.php";
  39  $sesskeyurl = "$CFG->wwwroot/$CFG->admin/portfolio.php?sesskey=" . sesskey();
  40  $configstr  = get_string('manageportfolios', 'portfolio');
  41  
  42  $return = true; // direct back to the main page
  43  
  44  /**
  45   * Helper function that generates a moodle_url object
  46   * relevant to the portfolio
  47   */
  48  function portfolio_action_url($portfolio) {
  49      global $baseurl;
  50      return new moodle_url($baseurl, array('sesskey'=>sesskey(), 'pf'=>$portfolio));
  51  }
  52  
  53  if (($action == 'edit') || ($action == 'new')) {
  54      if (($action == 'edit')) {
  55          $instance = portfolio_instance($portfolio);
  56          $plugin = $instance->get('plugin');
  57  
  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;
  67      }
  68  
  69      $PAGE->set_pagetype('admin-portfolio-' . $plugin);
  70  
  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});
  81          }
  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);
  88          }
  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;
 100      }
 101  } else if (($action == 'hide') || ($action == 'show')) {
 102      require_sesskey();
 103  
 104      $instance = portfolio_instance($portfolio);
 105      $current = $instance->get('visible');
 106      if (empty($current) && $instance->instance_sanity_check()) {
 107          print_error('cannotsetvisible', 'portfolio', $baseurl);
 108      }
 109  
 110      if ($action == 'show') {
 111          $visible = 1;
 112      } else {
 113          $visible = 0;
 114      }
 115  
 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);
 125          }
 126          if ($instance->delete()) {
 127              $deletedstr = get_string('instancedeleted', 'portfolio');
 128              redirect($baseurl, $deletedstr, 1);
 129          } else {
 130              print_error('instancenotdeleted', 'portfolio', $baseurl);
 131          }
 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;
 137      }
 138  } else {
 139      // If page is loaded directly
 140      echo $OUTPUT->header();
 141      echo $OUTPUT->heading(get_string('manageportfolios', 'portfolio'));
 142  
 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');
 148  
 149      $actionchoicesforexisting = array(
 150          'show' => $strshow,
 151          'hide' => $strhide,
 152          'delete' => $strdelete
 153      );
 154  
 155      $actionchoicesfornew = array(
 156          'newon' => $strshow,
 157          'newoff' => $strhide,
 158          'delete' => $strdelete
 159      );
 160  
 161      $output = $OUTPUT->box_start('generalbox');
 162  
 163      $plugins = core_component::get_plugin_list('portfolio');
 164      $plugins = array_keys($plugins);
 165      $instances = portfolio_instances(false, false);
 166      $usedplugins = array();
 167  
 168      // to avoid notifications being sent out while admin is editing the page
 169      define('ADMIN_EDITING_PORTFOLIO', true);
 170  
 171      $insane = portfolio_plugin_sanity_check($plugins);
 172      $insaneinstances = portfolio_instance_sanity_check($instances);
 173  
 174      $table = new html_table();
 175      $table->head = array(get_string('plugin', 'portfolio'), '', '');
 176      $table->data = array();
 177  
 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');
 184  
 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];
 191              }
 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';
 198              }
 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);
 202          }
 203          if (!in_array($plugin, $usedplugins)) {
 204              $usedplugins[] = $plugin;
 205          }
 206      }
 207  
 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;
 215              }
 216  
 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), '');
 224              }
 225          }
 226      }
 227  
 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), '');
 232          }
 233      }
 234  
 235      $output .= html_writer::table($table);
 236  
 237      $output .= $OUTPUT->box_end();
 238  
 239      echo $output;
 240      $return = false;
 241  }
 242  
 243  if ($return) {
 244      // Redirect to base
 245      redirect($baseurl);
 246  }
 247  
 248  echo $OUTPUT->footer();
 249