Skip to content

Commit

Permalink
Merge pull request #172 from praxisdigital/master
Browse files Browse the repository at this point in the history
Return to original URL when inserting items (master)
  • Loading branch information
frederikmillingpytlick authored Aug 2, 2023
2 parents f167964 + c2b3b5f commit 6d1ddfb
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 1,375 deletions.
13 changes: 0 additions & 13 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ on:
branches:
- master
- MOODLE_41_STABLE
- MOODLE_40_STABLE

env:
php: 8.2
Expand All @@ -17,18 +16,6 @@ jobs:
strategy:
matrix:
include:
## Moodle 4.0+ with PHP 7.4
- os: ubuntu-20.04
php: 7.4
db: mysqli
moodle: MOODLE_400_STABLE
experimental: false
## Moodle 4.0+ with PHP 8.0
- os: ubuntu-22.04
php: 8.0
db: mysqli
moodle: MOODLE_400_STABLE
experimental: false
## Moodle 4.1+ with PHP 8.0
- os: ubuntu-22.04
php: 8.0
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Warning: PHP versions 7.2 and older are deprecated, and will cause problems, unr

Change Log
----------
* 4.1, release 2 2023.07.05
* Return to original URL when inserting items & general code cleanup
* 4.1, release 1 2023.03.23
* Changed section copy button design
* Tested in Boost Union theme
Expand Down
2 changes: 1 addition & 1 deletion amd/build/script.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion amd/build/script.min.js.map

Large diffs are not rendered by default.

45 changes: 13 additions & 32 deletions amd/src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,42 +647,23 @@ define(['jquery', 'core/modal_factory', 'core/modal_events'], function($, ModalF
* @returns {jQuery}
*/


function create_target(id, section) {
var href = '';

var inSection = $('#copy-section-form').data('in-section');
if (restore_targets.is_directory) {
href = get_action_url('restore', {
'directory': true,
'path': id,
'course': course.id,
'section': section,
'in_section': inSection,
'sesskey': M.cfg.sesskey
});
} else {
href = get_action_url('restore', {
'directory': false,
'id': id,
'course': course.id,
'section': section,
'in_section': inSection,
'sesskey': M.cfg.sesskey
});
}
const href = get_action_url('restore', {
'directory': (restore_targets.is_directory === true),
'target': id,
'course': course.id,
'section': section,
'in_section': $('#copy-section-form').data('in-section'),
'sesskey': M.cfg.sesskey,
'returnurl': document.URL,
});

var $target = $('<a/>')
.attr('class', 'restore')
.attr('href', href)
.attr('title', str('copyhere'))
.append(
$('<img class="move_target"/>')
.attr('alt', str('copyhere'))
.attr('src', M.util.image_url('dropzone_arrow', 'block_sharing_cart'))
);
let $target = $('<a/>').attr('href', href).attr('title', str('copyhere')).append(
$('<img class="move_target"/>').attr('alt', str('copyhere')).attr('src', M.util.image_url('dropzone_arrow', 'block_sharing_cart'))
);

targets.push($target);

return $target;
}

Expand Down
2 changes: 1 addition & 1 deletion block_sharing_cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Sharing Cart block
*
* @package block_sharing_cart
* @copyright 2021 (C) Don Hinkelman and others
* @copyright 2023 (c) Don Hinkelman, moxis and others
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

Expand Down
53 changes: 30 additions & 23 deletions classes/section_title_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ class section_title_form extends \moodleform {
private $directory;

/** @var string */
private $path;
private $target;

/** @var string */
private $returnurl;

/** @var int */
private $courseid;
Expand All @@ -29,18 +32,19 @@ class section_title_form extends \moodleform {
* section_title_form constructor.
*
* @param bool $directory
* @param string $path
* @param string $target
* @param int $courseid
* @param int $sectionnumber
* @param array $eligible_sections
* @param int $items_count
*/
public function __construct(bool $directory, string $path, int $courseid, int $sectionnumber, array $eligible_sections, int $items_count = 0) {
public function __construct(bool $directory, string $target, int $courseid, int $sectionnumber, array $eligible_sections, string $returnurl, int $items_count = 0) {
$this->directory = $directory;
$this->path = $path;
$this->target = $target;
$this->courseid = $courseid;
$this->sectionnumber = $sectionnumber;
$this->sections = $eligible_sections;
$this->returnurl = $returnurl;
$this->items_count = $items_count;
parent::__construct();
}
Expand All @@ -51,39 +55,42 @@ public function definition(): void {
$mform =& $this->_form;

if ($this->items_count > 9) {
$mform->addElement('static', 'restore_heavy_load_warning_message', '',
'<p class="alert alert-danger" role="alert">
'.
get_string('restore_heavy_load_warning_message', 'block_sharing_cart')
.'
</p>');
$mform->addElement(
'static',
'restore_heavy_load_warning_message',
'',
'<p class="alert alert-danger" role="alert">'.get_string('restore_heavy_load_warning_message', 'block_sharing_cart').'</p>'
);
}

$mform->addElement('static', 'description', '', get_string('conflict_description', 'block_sharing_cart'));

$mform->addElement('radio', 'sharing_cart_section',
get_string('conflict_no_overwrite', 'block_sharing_cart', $current_section_name), null, 0);
$mform->addElement('radio', 'overwrite', get_string('conflict_no_overwrite', 'block_sharing_cart', $current_section_name), null, 0);

foreach ($this->sections as $section) {
$option_title = get_string('conflict_overwrite_title', 'block_sharing_cart', $section->name);
if ($section->summary != null) {
$option_title .= '<br><div class="small"><strong>' . get_string('summary') . ':</strong> ' .
strip_tags($section->summary) . '</div>';
}

$mform->addElement('radio', 'sharing_cart_section', $option_title, null, $section->id);
$option_title .= ($section->summary != null) ? '<br><div class="small"><strong>'.get_string('summary').':</strong> '.strip_tags($section->summary).'</div>' : '';
$mform->addElement('radio', 'overwrite', $option_title, null, $section->id);
}
$mform->setDefault('section_title', 0);

$mform->setDefault('overwrite', 0);

$mform->addElement('hidden', 'directory', $this->directory);
$mform->setType('directory', PARAM_BOOL);
$mform->addElement('hidden', 'path', $this->path);
$mform->setType('path', PARAM_TEXT);

$mform->addElement('hidden', 'target', $this->target);
$mform->setType('target', PARAM_TEXT);

$mform->addElement('hidden', 'course', $this->courseid);
$mform->setType('course', PARAM_INT);

$mform->addElement('hidden', 'section', $this->sectionnumber);
$mform->setType('section', PARAM_INT);

$mform->addElement('hidden', 'returnurl', $this->returnurl);
$mform->setType('returnurl', PARAM_TEXT);

$mform->addElement('static', 'description_note', '',
'<div class="small">' . get_string('conflict_description_note', 'block_sharing_cart') . '</div>');
$mform->addElement('static', 'description_note', '', '<div class="small">'.get_string('conflict_description_note', 'block_sharing_cart').'</div>');

$this->add_action_buttons(true, get_string('conflict_submit', 'block_sharing_cart'));
}
Expand Down
103 changes: 40 additions & 63 deletions restore.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,105 +27,82 @@

require_once '../../config.php';

global $OUTPUT, $PAGE;
global $CFG, $OUTPUT, $PAGE, $DB, $USER;

$directory = required_param('directory', PARAM_BOOL);
$id = null;
$path = null;

if ($directory) {
$path = required_param('path', PARAM_TEXT);
} else {
$id = required_param('id', PARAM_INT);
}
$target = required_param('target', PARAM_RAW);
$courseid = required_param('course', PARAM_INT);
$sectionnumber = required_param('section', PARAM_INT);
$in_section = optional_param('in_section', 0, PARAM_INT);
$overwrite = optional_param('overwrite', 0, PARAM_INT);
$returnurl = optional_param('returnurl', '', PARAM_TEXT);

if ($courseid == SITEID) {
$returnurl = new moodle_url('/');
} else {
$returnurl = new moodle_url('/course/view.php', array('id' => $courseid));
}

if ($in_section) {
$returnurl .= '&section=' . $sectionnumber;
} else {
$returnurl .= '#section-' . $sectionnumber;
if (!$returnurl) {
$returnurl = ($courseid == SITEID) ? new moodle_url('/') : new moodle_url('/course/view.php', ['id' => $courseid]);
}

require_login($courseid);

try {

$controller = new controller();

// Trying to restore a directory of items
if ($directory) {
$form = new section_title_form($directory, $path, $courseid, $sectionnumber, array());
if ($form->is_cancelled()) {
redirect($returnurl);
exit;
}

$use_sc_section = optional_param('sharing_cart_section', -1, PARAM_INT);
$form = new section_title_form($directory, $target, $courseid, $sectionnumber, [], $returnurl, 0);

if ($path[0] == '/') {
$path = substr($path, 1);
if ($form->is_cancelled()) {
redirect($returnurl); exit;
}

GLOBAL $DB, $USER;
$items = $DB->get_records('block_sharing_cart', array('tree' => $path, 'userid' => $USER->id));
$items_count = count($items);

if ($use_sc_section < 0) {
$sections = $controller->get_path_sections($path);
if (count($sections) > 0) {
$dest_section = $DB->get_record('course_sections', array('course' => $courseid, 'section' => $sectionnumber));
$target = ltrim($target, '/');

$PAGE->set_pagelayout('standard');
$PAGE->set_url('/blocks/sharing_cart/restore.php');
$PAGE->set_title(get_string('pluginname', 'block_sharing_cart') . ' - ' .
get_string('restore', 'block_sharing_cart'));
$PAGE->set_heading(get_string('restore', 'block_sharing_cart'));
$sections = $controller->get_path_sections($target);

$urlchunk = '#section-';
if ($in_section) {
$urlchunk = '&section=';
}
// Directory contains an entire section of items. Display form to let user resolve conflicts
if (count($sections) > 0 && !$form->is_submitted()) {

$PAGE->navbar
->add(get_section_name($courseid, $sectionnumber),
new moodle_url("/course/view.php?id={$courseid}{$urlchunk}{$sectionnumber}"))
->add(get_string('pluginname', 'block_sharing_cart'))
->add(get_string('restore', 'block_sharing_cart'));
$items = $DB->get_records('block_sharing_cart', array('tree' => $target, 'userid' => $USER->id));
$items_count = count($items);

echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('section_name_conflict', 'block_sharing_cart'));
$dest_section = $DB->get_record('course_sections', array('course' => $courseid, 'section' => $sectionnumber));

$form = new section_title_form($directory, $path, $courseid, $sectionnumber, $sections, $items_count);
$form->display();
$PAGE->set_pagelayout('standard');
$PAGE->set_url($returnurl);
$PAGE->set_title(get_string('pluginname', 'block_sharing_cart').' - '.get_string('restore', 'block_sharing_cart'));
$PAGE->set_heading(get_string('restore', 'block_sharing_cart'));

echo $OUTPUT->footer();
exit;
}
$form = new section_title_form($directory, $target, $courseid, $sectionnumber, $sections, $returnurl, $items_count);

$use_sc_section = 0;
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('section_name_conflict', 'block_sharing_cart'));
$form->display();
echo $OUTPUT->footer();
exit;
}

if ($use_sc_section > -1) {
$controller->restore_directory($path, $courseid, $sectionnumber, $use_sc_section);
}
// Perform directory restore
$controller->restore_directory($target, $courseid, $sectionnumber, $overwrite);

} else {
$controller->restore($id, $courseid, $sectionnumber);

// Restore single item
$controller->restore($target, $courseid, $sectionnumber);

}

redirect($returnurl);

} catch (sharing_cart\exception $ex) {
} catch (\block_sharing_cart\exception $ex) {

print_error($ex->errorcode, $ex->module, $returnurl, $ex->a);

} catch (Exception $ex) {

if (!empty($CFG->debug) && $CFG->debug >= DEBUG_DEVELOPER) {
print_error('notlocalisederrormessage', 'error', '', $ex->__toString());
} else {
print_error('unexpectederror', 'block_sharing_cart', $returnurl);
}

}
Loading

0 comments on commit 6d1ddfb

Please sign in to comment.