Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   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      $plugin = $instance->get('plugin');
 106      if ($action == 'show') {
 107          $visible = 1;
 108      } else {
 109          $visible = 0;
 110      }
 111  
 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);
 120          }
 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);
 126          }
 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;
 132      }
 133  } else {
 134      // If page is loaded directly
 135      echo $OUTPUT->header();
 136      echo $OUTPUT->heading(get_string('manageportfolios', 'portfolio'));
 137  
 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');
 143  
 144      $actionchoicesforexisting = array(
 145          'show' => $strshow,
 146          'hide' => $strhide,
 147          'delete' => $strdelete
 148      );
 149  
 150      $actionchoicesfornew = array(
 151          'newon' => $strshow,
 152          'newoff' => $strhide,
 153          'delete' => $strdelete
 154      );
 155  
 156      $output = $OUTPUT->box_start('generalbox');
 157  
 158      $plugins = core_component::get_plugin_list('portfolio');
 159      $plugins = array_keys($plugins);
 160      $instances = portfolio_instances(false, false);
 161      $usedplugins = array();
 162  
 163      // to avoid notifications being sent out while admin is editing the page
 164      define('ADMIN_EDITING_PORTFOLIO', true);
 165  
 166      $insane = portfolio_plugin_sanity_check($plugins);
 167      $insaneinstances = portfolio_instance_sanity_check($instances);
 168  
 169      $table = new html_table();
 170      $table->head = array(get_string('plugin', 'portfolio'), '', '');
 171      $table->data = array();
 172  
 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');
 179  
 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];
 186              }
 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';
 193              }
 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);
 197          }
 198          if (!in_array($plugin, $usedplugins)) {
 199              $usedplugins[] = $plugin;
 200          }
 201      }
 202  
 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;
 210              }
 211  
 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), '');
 219              }
 220          }
 221      }
 222  
 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), '');
 227          }
 228      }
 229  
 230      $output .= html_writer::table($table);
 231  
 232      $output .= $OUTPUT->box_end();
 233  
 234      echo $output;
 235      $return = false;
 236  }
 237  
 238  if ($return) {
 239      // Redirect to base
 240      redirect($baseurl);
 241  }
 242  
 243  echo $OUTPUT->footer();