Skip to content

Commit

Permalink
Merge pull request #103 from praxisdigital/ara_GH-102_41
Browse files Browse the repository at this point in the history
#102 - Added activity copy button, if user has capability to back up …
  • Loading branch information
neeesn authored Oct 24, 2023
2 parents cd27641 + 7f81816 commit ca437f4
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 20 deletions.
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 3 2023.09.20
* Added activity copy button, if user has capability to back up activities, but not to manage activities
* 4.1, release 2 2023.07.05
* Return to original URL when inserting items & general code cleanup
* 4.1, release 1 2023.03.23
Expand Down
2 changes: 1 addition & 1 deletion amd/build/bulkdelete.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.

39 changes: 25 additions & 14 deletions amd/src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -766,25 +766,28 @@ define(['jquery', 'core/modal_factory', 'core/modal_events'], function($, ModalF
*
* @param e
* @param activityName
* @param {int} cmId
*/
$.on_backup = function(e, activityName) {
var cmid = (function($backup) {
var $activity = $backup.closest('li.activity');
if ($activity.length) {
return $activity.attr('id').match(/(\d+)$/)[1];
}
var $commands = $backup.closest('.commands');
var dataowner = $commands.attr('data-owner');
if (dataowner.length) {
return dataowner.match(/(\d+)$/)[1];
}
return $commands.find('a.editing_delete').attr('href').match(/delete=(\d+)/)[1];
})($(e.target));
$.on_backup = function(e, activityName, cmId = 0) {
if (cmId === 0) {
cmId = (function ($backup) {
var $activity = $backup.closest('li.activity');
if ($activity.length) {
return $activity.attr('id').match(/(\d+)$/)[1];
}
var $commands = $backup.closest('.commands');
var dataowner = $commands.attr('data-owner');
if (dataowner.length) {
return dataowner.match(/(\d+)$/)[1];
}
return $commands.find('a.editing_delete').attr('href').match(/delete=(\d+)/)[1];
})($(e.target));
}

var data =
{
"action": "is_userdata_copyable",
"cmid": cmid
"cmid": cmId
};

on_backup_modal(data, activityName, str('confirm_backup'), false);
Expand Down Expand Up @@ -1362,6 +1365,14 @@ define(['jquery', 'core/modal_factory', 'core/modal_events'], function($, ModalF

$.on_section_backup(sectionId, sectionNumber, courseId, sectionName);
});

$('.copy_activity').on('click', function(e) {
var activitySelected = ($('.activity-dropdown option:selected'));
var activityId = activitySelected.data('activity-id');
var activityName = activitySelected.data('activity-name');

$.on_backup(e, activityName, activityId);
});
}
};
});
57 changes: 56 additions & 1 deletion block_sharing_cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

use block_sharing_cart\controller;
use block_sharing_cart\section;
use block_sharing_cart\module;

defined('MOODLE_INTERNAL') || die();

Expand Down Expand Up @@ -89,6 +90,9 @@ public function get_content() {
$sectionsHandler = new section();
$sections = $sectionsHandler->all($COURSE->id);

$moduleHandler = new module();
$activities = $moduleHandler->get_all_from_course($COURSE->id);

/* Place the <noscript> tag to give out an error message if JavaScript is not enabled in the browser.
* Adding bootstrap classes to show colored info in bootstrap based themes. */
$noscript = html_writer::tag('noscript',
Expand Down Expand Up @@ -152,7 +156,32 @@ public function get_content() {
}
}

$footer = $this->insert_copy_section_in_footer($section_id, $sections_dropdown);
$footer .= $this->insert_copy_section_in_footer($section_id, $sections_dropdown);

if (!has_capability('moodle/course:manageactivities', $context)) {
$activities_dropdown = '';
/** @var \cm_info $activity */
foreach ($activities as $activity) {
if ($this->is_activity_not_in_section($section_id, $activity)) {
continue;
}

if ($this->is_activity_deletion_in_progress($activity)) {
continue;
}

$activityname = $activity->get_name();
$activities_dropdown .= "
<option data-activity-id='$activity->id' data-activity-name='$activityname'>
$activityname
</option>
";
}

if ($activities_dropdown !== '') {
$footer .= $this->insert_copy_activity_in_footer($activities_dropdown);
}
}
}
$footer .= '
<div style="display:none;">
Expand All @@ -162,6 +191,14 @@ public function get_content() {
return $this->content = (object) array('text' => $html, 'footer' => $footer);
}

private function is_activity_not_in_section(int $section_id, \cm_info $activity): bool {
return $section_id !== $activity->get_section_info()->section;
}

private function is_activity_deletion_in_progress(\cm_info $activity): bool {
return $activity->deletioninprogress == 1;
}

private function insert_copy_section_in_footer(int $section_id, string $sections_dropdown): string {
if (!get_config('block_sharing_cart', 'show_copy_section_in_block')) {
return "";
Expand All @@ -179,6 +216,24 @@ private function insert_copy_section_in_footer(int $section_id, string $sections
";
}

private function insert_copy_activity_in_footer(string $activities): string {
if (!get_config('block_sharing_cart', 'show_copy_activity_in_block')) {
return "";
}

return "
<form class='mt-3' id=\"copy-activity-form\"'>
<p>" . get_string('copy_activity_reason', __CLASS__) . "</p>
<select class='custom-select activity-dropdown'>
$activities
</select>
<a href='javascript:void(0)' class='copy_activity' title='" . get_string('copy_activity_title', __CLASS__) . "'>
<input id='copy' type='button' class='btn btn-primary' value='" . get_string('copy_activity', __CLASS__) . "'>
</a>
</form>
";
}

/**
* Get the block header
*
Expand Down
15 changes: 15 additions & 0 deletions classes/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,19 @@ public static function has_backup(int $cmid, int $course = 0): bool {
[$course, $cm] = get_course_and_cm_from_cmid($cmid, '', $course);
return (bool)plugin_supports('mod', $cm->modname, FEATURE_BACKUP_MOODLE2);
}

/**
* @param int $course_id
* @return \section_info[]
* @throws \moodle_exception
*/
public function get_all_from_course(int $course_id): array {
if (empty($course_id)) {
return [];
}

/** @var \course_modinfo $modules */
$modules = get_fast_modinfo($course_id);
return $modules->get_cms();
}
}
5 changes: 5 additions & 0 deletions lang/en/block_sharing_cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
$string['inprogess_pleasewait'] = 'Please wait…';
$string['copy_section'] = 'Copy section';
$string['copy_section_title'] = 'Copy selected section';
$string['copy_activity'] = 'Copy activity';
$string['copy_activity_title'] = 'Copy selected activity';
$string['copy_activity_reason'] = 'This view is available because you have access to backup activities, but no access to manage/move activities.';
$string['label_image_replaced_text'] = ' (Label: Image)';
$string['uninstalled_plugin_warning_title'] = 'This plugin is uninstalled. Trying to recover this without reinstalling the plugin: {$a} will cause errors. Restoration is disabled';
$string['drop_here'] = 'Drop here..';
Expand All @@ -84,6 +87,8 @@
$string['settings:click_to_add'] = 'Click to add';
$string['settings:show_copy_section_in_block'] = 'Show the "Copy section" in block';
$string['settings:show_copy_section_in_block_desc'] = 'Show the "Copy section" in the sharing cart block, underneath all modules/activities';
$string['settings:show_copy_activity_in_block'] = 'Show the "Copy activity" in block';
$string['settings:show_copy_activity_in_block_desc'] = 'Show the "Copy activity" in the sharing cart block, underneath all modules/activities - This is only available if the user has the capability to backup activities, but not the capability to manage/move activities';

$string['invalidoperation'] = 'An invalid operation detected';
$string['unexpectederror'] = 'An unexpected error occurred';
Expand Down
Loading

0 comments on commit ca437f4

Please sign in to comment.