Skip to content

Commit

Permalink
Merge pull request #178 from praxisdigital/master
Browse files Browse the repository at this point in the history
Added new setting to "Copy activity"
  • Loading branch information
donhinkelman authored Nov 1, 2023
2 parents cdf964e + f304547 commit 91015e7
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 22 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Sharing Cart
**Sharing cart basket icon now visible by default** To drag item directly into your sharing cart, change the settings.
The old way of clicking the basket icon is now default. - Users who have already upgraded to 4.0, release 1 will need to manually change it back to the old way in settings.

Version 4.1, release 1 - 2023.03.23
Version 4.1, release 3 - 2023.09.20

The "master" branch requires Moodle 3.11.4 or newer.

Expand All @@ -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();
}
}
Loading

0 comments on commit 91015e7

Please sign in to comment.