Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This page shows the contents of a recyclebin for a given course.
  19   *
  20   * @package    tool_recyclebin
  21   * @copyright  2015 University of Kent
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once(__DIR__ . '/../../../config.php');
  26  require_once($CFG->libdir . '/tablelib.php');
  27  
  28  $contextid = required_param('contextid', PARAM_INT);
  29  $action = optional_param('action', null, PARAM_ALPHA);
  30  
  31  $context = context::instance_by_id($contextid, MUST_EXIST);
  32  $PAGE->set_context($context);
  33  
  34  // We could be a course or a category.
  35  switch ($context->contextlevel) {
  36      case CONTEXT_COURSE:
  37          require_login($context->instanceid);
  38  
  39          $recyclebin = new \tool_recyclebin\course_bin($context->instanceid);
  40          if (!$recyclebin->can_view()) {
  41              throw new required_capability_exception($context, 'tool/recyclebin:viewitems', 'nopermissions', '');
  42          }
  43  
  44          $PAGE->set_pagelayout('incourse');
  45          // Set the $PAGE heading - this is also the same as the h2 heading.
  46          $heading = format_string($COURSE->fullname, true, array('context' => $context)) . ': ' .
  47              get_string('pluginname', 'tool_recyclebin');
  48          $PAGE->set_heading($heading);
  49  
  50          // Get the expiry to use later.
  51          $expiry = get_config('tool_recyclebin', 'coursebinexpiry');
  52      break;
  53  
  54      case CONTEXT_COURSECAT:
  55          require_login(null, false);
  56  
  57          $recyclebin = new \tool_recyclebin\category_bin($context->instanceid);
  58          if (!$recyclebin->can_view()) {
  59              throw new required_capability_exception($context, 'tool/recyclebin:viewitems', 'nopermissions', '');
  60          }
  61  
  62          $PAGE->set_pagelayout('admin');
  63          // Set the $PAGE heading.
  64          $PAGE->set_heading($COURSE->fullname);
  65          // The h2 heading on the page is going to be different than the $PAGE heading.
  66          $heading = $context->get_context_name() . ': ' . get_string('pluginname', 'tool_recyclebin');
  67  
  68          // Get the expiry to use later.
  69          $expiry = get_config('tool_recyclebin', 'categorybinexpiry');
  70      break;
  71  
  72      default:
  73          print_error('invalidcontext', 'tool_recyclebin');
  74      break;
  75  }
  76  
  77  if (!$recyclebin::is_enabled()) {
  78      print_error('notenabled', 'tool_recyclebin');
  79  }
  80  
  81  $PAGE->set_url('/admin/tool/recyclebin/index.php', array(
  82      'contextid' => $contextid
  83  ));
  84  $PAGE->set_title(get_string('pluginname', 'tool_recyclebin'));
  85  
  86  // If we are doing anything, we need a sesskey!
  87  if (!empty($action)) {
  88      raise_memory_limit(MEMORY_EXTRA);
  89      require_sesskey();
  90  
  91      $item = null;
  92      if ($action == 'restore' || $action == 'delete') {
  93          $itemid = required_param('itemid', PARAM_INT);
  94          $item = $recyclebin->get_item($itemid);
  95      }
  96  
  97      switch ($action) {
  98          // Restore it.
  99          case 'restore':
 100              if ($recyclebin->can_restore()) {
 101                  $recyclebin->restore_item($item);
 102                  redirect($PAGE->url, get_string('alertrestored', 'tool_recyclebin', $item), 2);
 103              } else {
 104                  print_error('nopermissions', 'error');
 105              }
 106          break;
 107  
 108          // Delete it.
 109          case 'delete':
 110              if ($recyclebin->can_delete()) {
 111                  $recyclebin->delete_item($item);
 112                  redirect($PAGE->url, get_string('alertdeleted', 'tool_recyclebin', $item), 2);
 113              } else {
 114                  print_error('nopermissions', 'error');
 115              }
 116          break;
 117  
 118          // Empty it.
 119          case 'empty':
 120              $recyclebin->delete_all_items();
 121              redirect($PAGE->url, get_string('alertemptied', 'tool_recyclebin'), 2);
 122          break;
 123      }
 124  }
 125  
 126  // Add a "Go Back" button.
 127  $goback = html_writer::start_tag('div', array('class' => 'backlink'));
 128  $goback .= html_writer::link($context->get_url(), get_string('backto', '', $context->get_context_name()));
 129  $goback .= html_writer::end_tag('div');
 130  
 131  // Output header.
 132  echo $OUTPUT->header();
 133  echo $OUTPUT->heading($heading);
 134  
 135  // Grab our items, check there is actually something to display.
 136  $items = $recyclebin->get_items();
 137  
 138  // Nothing to show? Bail out early.
 139  if (empty($items)) {
 140      echo $OUTPUT->box(get_string('noitemsinbin', 'tool_recyclebin'));
 141      echo $goback;
 142      echo $OUTPUT->footer();
 143      die;
 144  }
 145  
 146  // Start with a description.
 147  if ($expiry > 0) {
 148      $expirydisplay = format_time($expiry);
 149      echo '<div class=\'alert\'>' . get_string('deleteexpirywarning', 'tool_recyclebin', $expirydisplay) . '</div>';
 150  }
 151  
 152  // Define columns and headers.
 153  $firstcolstr = $context->contextlevel == CONTEXT_COURSE ? 'activity' : 'course';
 154  $columns = array($firstcolstr, 'date', 'restore', 'delete');
 155  $headers = array(
 156      get_string($firstcolstr),
 157      get_string('datedeleted', 'tool_recyclebin'),
 158      get_string('restore'),
 159      get_string('delete')
 160  );
 161  
 162  // Define a table.
 163  $table = new flexible_table('recyclebin');
 164  $table->define_columns($columns);
 165  $table->column_style('restore', 'text-align', 'center');
 166  $table->column_style('delete', 'text-align', 'center');
 167  $table->define_headers($headers);
 168  $table->define_baseurl($PAGE->url);
 169  $table->set_attribute('id', 'recycle-bin-table');
 170  $table->setup();
 171  
 172  // Cache a list of modules.
 173  $modules = null;
 174  if ($context->contextlevel == CONTEXT_COURSE) {
 175      $modules = $DB->get_records('modules');
 176  }
 177  
 178  // Add all the items to the table.
 179  $showempty = false;
 180  $canrestore = $recyclebin->can_restore();
 181  foreach ($items as $item) {
 182      $row = array();
 183  
 184      // Build item name.
 185      $name = $item->name;
 186      if ($context->contextlevel == CONTEXT_COURSE) {
 187          if (isset($modules[$item->module])) {
 188              $mod = $modules[$item->module];
 189              $modname = get_string('modulename', $mod->name);
 190              $name = $OUTPUT->image_icon('icon', $modname, $mod->name) . $name;
 191          }
 192      }
 193  
 194      $row[] = $name;
 195      $row[] = userdate($item->timecreated);
 196  
 197      // Build restore link.
 198      if ($canrestore && ($context->contextlevel == CONTEXT_COURSECAT || isset($modules[$item->module]))) {
 199          $restoreurl = new moodle_url($PAGE->url, array(
 200              'contextid' => $contextid,
 201              'itemid' => $item->id,
 202              'action' => 'restore',
 203              'sesskey' => sesskey()
 204          ));
 205          $row[] = $OUTPUT->action_icon($restoreurl, new pix_icon('t/restore', get_string('restore'), '', array(
 206              'class' => 'iconsmall'
 207          )));
 208      } else {
 209          // Show padlock.
 210          $row[] = $OUTPUT->pix_icon('t/locked', get_string('locked', 'admin'), '', array('class' => 'iconsmall'));
 211      }
 212  
 213      // Build delete link.
 214      if ($recyclebin->can_delete()) {
 215          $showempty = true;
 216          $delete = new moodle_url($PAGE->url, array(
 217              'contextid' => $contextid,
 218              'itemid' => $item->id,
 219              'action' => 'delete',
 220              'sesskey' => sesskey()
 221          ));
 222          $deleteaction = new confirm_action(get_string('deleteconfirm', 'tool_recyclebin'));
 223          $delete = $OUTPUT->action_icon($delete, new pix_icon('t/delete', get_string('delete')), $deleteaction);
 224  
 225          $row[] = $delete;
 226      } else {
 227          // Show padlock.
 228          $row[] = $OUTPUT->pix_icon('t/locked', get_string('locked', 'admin'), '', array('class' => 'iconsmall'));
 229      }
 230  
 231      $table->add_data($row);
 232  }
 233  
 234  // Display the table now.
 235  $table->finish_output();
 236  
 237  // Empty recyclebin link.
 238  if ($showempty) {
 239      $emptylink = new moodle_url($PAGE->url, array(
 240          'contextid' => $contextid,
 241          'action' => 'empty',
 242          'sesskey' => sesskey()
 243      ));
 244      $emptyaction = new confirm_action(get_string('deleteallconfirm', 'tool_recyclebin'));
 245      echo $OUTPUT->action_link($emptylink, get_string('deleteall', 'tool_recyclebin'), $emptyaction);
 246  }
 247  
 248  echo $goback;
 249  
 250  // Confirmation JS.
 251  $PAGE->requires->strings_for_js(array('deleteallconfirm', 'deleteconfirm'), 'tool_recyclebin');
 252  
 253  // Output footer.
 254  echo $OUTPUT->footer();