Skip to content

Commit

Permalink
Improvment: Only show move to for cms where there is the right capabi…
Browse files Browse the repository at this point in the history
…lity
  • Loading branch information
georgmaisser committed Jul 10, 2024
1 parent 274548f commit 11f9127
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions classes/option/fields/moveoption.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

namespace mod_booking\option\fields;

use Exception;
use mod_booking\booking_option_settings;
use mod_booking\option\fields;
use mod_booking\option\fields_info;
Expand All @@ -33,6 +34,7 @@
use moodle_exception;
use MoodleQuickForm;
use stdClass;
use context_module;

/**
* Class to handle one property of the booking_option_settings class.
Expand Down Expand Up @@ -131,7 +133,14 @@ public static function instance_form_definition(MoodleQuickForm &$mform, array &
)"
)) {
foreach ($records as $record) {
$allowedinstances[$record->cmid] = "$record->bookingname ($record->coursename, ID: $record->cmid)";
// A user should only be able to move the option to a cm where she has access.
$context = context_module::instance($record->cmid);
if (
has_capability('mod/booking:updatebooking', $context)
|| has_capability('mod/booking:addeditownoption', $context)
) {
$allowedinstances[$record->cmid] = "$record->bookingname ($record->coursename, ID: $record->cmid)";
}
}
}

Expand Down Expand Up @@ -180,17 +189,24 @@ public static function save_data(stdClass &$data, stdClass &$option): array {
$instance = new moveoption();
$changes = $instance->check_for_changes($data, $instance, '', 'moveoption', $option->bookingid);

$elements = get_course_and_cm_from_cmid((int)$data->moveoption);
$cm = $elements[1];

if ($option->bookingid != $cm->instance) {
$option->cmid = $cm->id;
$option->bookingid = $cm->instance;
$data->cmid = $cm->id;
$data->bookingid = $cm->instance;

$DB->update_record('booking_options', ['id' => $data->id, 'bookingid' => $cm->instance]);
try {
$elements = get_course_and_cm_from_cmid((int)$data->moveoption);
$cm = $elements[1];

if (!empty($cm) && ($option->bookingid != $cm->instance)) {
$option->cmid = $cm->id;
$option->bookingid = $cm->instance;
$data->cmid = $cm->id;
$data->bookingid = $cm->instance;

$DB->update_record('booking_options', ['id' => $data->id, 'bookingid' => $cm->instance]);
}
} catch (Exception $e) {
// We don't want to throw an error here but just ignore it.
// Might occur when a cm is chosen that does not exist anymore.
$changes = [];
}

}

return $changes;
Expand Down

0 comments on commit 11f9127

Please sign in to comment.