Files
moodle/admin/tool/recyclebin/index.php
T
Mark Nelson 93af6a610e MDL-48012 tool_recyclebin: multiple changes before integration
- Added new icon for the recycle bin.
- Fixed issue where the course expiry setting was being used to display
  the expiry messages in the category bin.
- Fixed failing Behat tests.
- Tidied Behat tests.
  - General tidy up.
  - Deleted three Behat feature files.
    - Deleted 'delete_confirmation.feature' as it's testing JS functionality
      that was removed (see comment further down). The 'basic_functionality.feature'
      now tests deleting an activity and a course.
    - Deleted 'description.feature' and added the tested functionality to
      'basic_functionality.feature'.
    - Deleted 'logs_test.feature' and moved testing to PHPUnit tests.
  - Added another scenario for restoring a course.
- Removed protected mod settings - it was discussed that this was not a necessary
  feature. We should not be treating the recycle bin as a backup.
- Renamed events to conform to guidelines.
- Wrote new PHPUnit tests for events.
- Tidied up existing PHPUnit tests and extended them.
- Alphabetised lang file.
- Made changes to strings and string identifiers in the lang file and changed usages in code.
- Changed setting names to better identify their purpose.
- Renamed classes and file names so it's easier to identify their purpose.
- Renamed the 'deleted' column to 'timecreated' for consistency with other
  Moodle tables, and to easily identify what the value represents.
- Changed the columns 'category' and 'course' to 'categoryid' and
  'courseid' respectively.
- Changed the name of the table indexes for consistency with core.
- Removed module.js and any use of it and replaced with Moodle core libraries
  for JS confirmation.
- Removed 'noevent' argument in delete_item().
- Removed unnecessary capabilities - we can use the same capabilties for both
  recycle bins based on the context the capability was given.
- Removed '_' in the capability definitions to keep it consistent with core.
- Set page layout and headings for index.php - varies between the course and
  category recycle bins.
- Deleted styles.css and centred columns using the flexible_table API.
- Fixed IDE and codechecker complaints.
- Made use of the files API, rather than writing directly to
  "$CFG->dataroot . '/recyclebin'".
- Used make_temp_directory rather than expecting it to exist.
- Replaced debugging function calls with calls to the backup API to
  display errors.
- Deleted the temporary backup file after a successful restore, or if it fails.
- If the restore fails for a course, delete the course we created.
- Removed unnecessary '\' characters when not in namespace.
- Used configduration class for expiry times in the settings.
- Cleanup course bin when a course is deleted.
- Cleanup a category bin when a category is deleted.
- Removed unnecessary throw tags.
- Changed default settings.
2016-03-18 14:12:08 +08:00

255 lines
8.3 KiB
PHP

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This page shows the contents of a recyclebin for a given course.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__) . '/../../../config.php');
require_once($CFG->libdir . '/tablelib.php');
$contextid = required_param('contextid', PARAM_INT);
$action = optional_param('action', null, PARAM_ALPHA);
$context = context::instance_by_id($contextid, MUST_EXIST);
$PAGE->set_context($context);
// We could be a course or a category.
switch ($context->contextlevel) {
case CONTEXT_COURSE:
require_login($context->instanceid);
$recyclebin = new \tool_recyclebin\course_bin($context->instanceid);
if (!$recyclebin->can_view()) {
throw new required_capability_exception($context, 'tool/recyclebin:viewitems', 'nopermissions', '');
}
$PAGE->set_pagelayout('incourse');
// Set the $PAGE heading - this is also the same as the h2 heading.
$heading = format_string($COURSE->fullname, true, array('context' => $context)) . ': ' .
get_string('pluginname', 'tool_recyclebin');
$PAGE->set_heading($heading);
// Get the expiry to use later.
$expiry = get_config('tool_recyclebin', 'coursebinexpiry');
break;
case CONTEXT_COURSECAT:
require_login();
$recyclebin = new \tool_recyclebin\category_bin($context->instanceid);
if (!$recyclebin->can_view()) {
throw new required_capability_exception($context, 'tool/recyclebin:viewitems', 'nopermissions', '');
}
$PAGE->set_pagelayout('admin');
// Set the $PAGE heading.
$PAGE->set_heading($COURSE->fullname);
// The h2 heading on the page is going to be different than the $PAGE heading.
$heading = $context->get_context_name() . ': ' . get_string('pluginname', 'tool_recyclebin');
// Get the expiry to use later.
$expiry = get_config('tool_recyclebin', 'categorybinexpiry');
break;
default:
print_error('invalidcontext', 'tool_recyclebin');
break;
}
if (!$recyclebin::is_enabled()) {
print_error('notenabled', 'tool_recyclebin');
}
$PAGE->set_url('/admin/tool/recyclebin/index.php', array(
'contextid' => $contextid
));
$PAGE->set_title(get_string('pluginname', 'tool_recyclebin'));
// If we are doing anything, we need a sesskey!
if (!empty($action)) {
raise_memory_limit(MEMORY_EXTRA);
require_sesskey();
$item = null;
if ($action == 'restore' || $action == 'delete') {
$itemid = required_param('itemid', PARAM_INT);
$item = $recyclebin->get_item($itemid);
}
switch ($action) {
// Restore it.
case 'restore':
if ($recyclebin->can_restore()) {
$recyclebin->restore_item($item);
redirect($PAGE->url, get_string('alertrestored', 'tool_recyclebin', $item), 2);
} else {
print_error('nopermissions', 'error');
}
break;
// Delete it.
case 'delete':
if ($recyclebin->can_delete()) {
$recyclebin->delete_item($item);
redirect($PAGE->url, get_string('alertdeleted', 'tool_recyclebin', $item), 2);
} else {
print_error('nopermissions', 'error');
}
break;
// Empty it.
case 'empty':
$recyclebin->delete_all_items();
redirect($PAGE->url, get_string('alertemptied', 'tool_recyclebin'), 2);
break;
}
}
// Add a "Go Back" button.
$goback = html_writer::start_tag('div', array('class' => 'backlink'));
$goback .= html_writer::link($context->get_url(), get_string('backto', '', $context->get_context_name()));
$goback .= html_writer::end_tag('div');
// Output header.
echo $OUTPUT->header();
echo $OUTPUT->heading($heading);
// Grab our items, check there is actually something to display.
$items = $recyclebin->get_items();
// Nothing to show? Bail out early.
if (empty($items)) {
echo $OUTPUT->box(get_string('noitemsinbin', 'tool_recyclebin'));
echo $goback;
echo $OUTPUT->footer();
die;
}
// Start with a description.
if ($expiry > 0) {
$expirydisplay = format_time($expiry);
echo '<div class=\'alert\'>' . get_string('deleteexpirywarning', 'tool_recyclebin', $expirydisplay) . '</div>';
}
// Define columns and headers.
$firstcolstr = $context->contextlevel == CONTEXT_COURSE ? 'activity' : 'course';
$columns = array($firstcolstr, 'date', 'restore', 'delete');
$headers = array(
get_string($firstcolstr),
get_string('datedeleted', 'tool_recyclebin'),
get_string('restore'),
get_string('delete')
);
// Define a table.
$table = new flexible_table('recyclebin');
$table->define_columns($columns);
$table->column_style('restore', 'text-align', 'center');
$table->column_style('delete', 'text-align', 'center');
$table->define_headers($headers);
$table->define_baseurl($PAGE->url);
$table->set_attribute('id', 'recycle-bin-table');
$table->setup();
// Cache a list of modules.
$modules = null;
if ($context->contextlevel == CONTEXT_COURSE) {
$modules = $DB->get_records('modules');
}
// Add all the items to the table.
$showempty = false;
$canrestore = $recyclebin->can_restore();
foreach ($items as $item) {
$row = array();
// Build item name.
$name = $item->name;
if ($context->contextlevel == CONTEXT_COURSE) {
if (isset($modules[$item->module])) {
$mod = $modules[$item->module];
$modname = get_string('modulename', $mod->name);
$name = '<img src="' . $OUTPUT->pix_url('icon', $mod->name) . '" class="icon" alt="' . $modname . '" /> ' . $name;
}
}
$row[] = $name;
$row[] = userdate($item->timecreated);
// Build restore link.
if ($canrestore && ($context->contextlevel == CONTEXT_COURSECAT || isset($modules[$item->module]))) {
$restoreurl = new moodle_url($PAGE->url, array(
'contextid' => $contextid,
'itemid' => $item->id,
'action' => 'restore',
'sesskey' => sesskey()
));
$row[] = $OUTPUT->action_icon($restoreurl, new pix_icon('t/restore', get_string('restore'), '', array(
'class' => 'iconsmall'
)));
} else {
// Show padlock.
$row[] = $OUTPUT->pix_icon('t/locked', get_string('locked', 'admin'), '', array('class' => 'iconsmall'));
}
// Build delete link.
if ($recyclebin->can_delete()) {
$showempty = true;
$delete = new moodle_url($PAGE->url, array(
'contextid' => $contextid,
'itemid' => $item->id,
'action' => 'delete',
'sesskey' => sesskey()
));
$deleteaction = new confirm_action(get_string('deleteconfirm', 'tool_recyclebin'));
$delete = $OUTPUT->action_icon($delete, new pix_icon('t/delete', get_string('delete')), $deleteaction);
$row[] = $delete;
} else {
// Show padlock.
$row[] = $OUTPUT->pix_icon('t/locked', get_string('locked', 'admin'), '', array('class' => 'iconsmall'));
}
$table->add_data($row);
}
// Display the table now.
$table->finish_output();
// Empty recyclebin link.
if ($showempty) {
$emptylink = new moodle_url($PAGE->url, array(
'contextid' => $contextid,
'action' => 'empty',
'sesskey' => sesskey()
));
$emptyaction = new confirm_action(get_string('deleteallconfirm', 'tool_recyclebin'));
echo $OUTPUT->action_link($emptylink, get_string('deleteall', 'tool_recyclebin'), $emptyaction);
}
echo $goback;
// Confirmation JS.
$PAGE->requires->strings_for_js(array('deleteallconfirm', 'deleteconfirm'), 'tool_recyclebin');
// Output footer.
echo $OUTPUT->footer();