Wunderbyte-GmbH / moodle-mod_booking

Moodle Booking Module
https://www.wunderbyte.at
21 stars 39 forks source link

Reset/Delete Bookings with Recompletion Plugin #607

Open VOOM108 opened 4 weeks ago

VOOM108 commented 4 weeks ago

Is your feature request related to a problem? Please describe. When a user is reset with Recompletion, their Bookings are not deleted or archived.

Describe the solution you'd like Recompletion works with plugins for different types of activities

Describe alternatives you've considered An option within the Booking plugin to delete all bookings of a user

Here is an attempt for such a plugin, derived from mod_lesson https://github.com/danmarsden/moodle-local_recompletion/tree/MOODLE_403_STABLE/classes/plugins

Could you look at it from the perspective of the Booking tables, and make a PR at the Recompletion repo if it should work?

mod_booking.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/>.

namespace local_recompletion\plugins;

use stdClass;
use lang_string;
use admin_setting_configselect;
use admin_setting_configcheckbox;
use admin_settingpage;
use MoodleQuickForm;

defined('MOODLE_INTERNAL') || die;

require_once($CFG->dirroot.'/local/recompletion/locallib.php');

/**
 * Booking handler event.
 *
 * @package    local_recompletion
 * @author     
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class mod_booking {

    /**
     * Add params to form.
     *
     * @param MoodleQuickForm $mform
     */
    public static function editingform(MoodleQuickForm $mform): void {
        $config = get_config('local_recompletion');

        $cba = [];
        $cba[] = $mform->createElement('radio', 'booking', '',
                get_string('donothing', 'local_recompletion'), LOCAL_RECOMPLETION_NOTHING);
        $cba[] = $mform->createElement('radio', 'booking', '',
                get_string('delete', 'local_recompletion'), LOCAL_RECOMPLETION_DELETE);

        $mform->addGroup($cba, 'booking', get_string('bookingattempts', 'local_recompletion'), [' '], false);
        $mform->addHelpButton('booking', 'bookingattempts', 'local_recompletion');
        $mform->setDefault('booking', $config->booking);

        $mform->addElement('checkbox', 'archivebooking', get_string('archive', 'local_recompletion'));
        $mform->setDefault('archivebooking', $config->archivebooking);

        $mform->disabledIf('archivebooking', 'enable');
        $mform->hideIf('archivebooking', 'booking');
        $mform->disabledIf('booking', 'enable');
    }

    /**
     * Add site level settings for this plugin.
     *
     * @param admin_settingpage $settings
     */
    public static function settings(admin_settingpage $settings): void {
        $choices = [
            LOCAL_RECOMPLETION_NOTHING => get_string('donothing', 'local_recompletion'),
            LOCAL_RECOMPLETION_DELETE => get_string('delete', 'local_recompletion')
        ];

        $settings->add(new admin_setting_configselect('local_recompletion/booking',
                new lang_string('bookingattempts', 'local_recompletion'),
                new lang_string('bookingattempts_help', 'local_recompletion'), LOCAL_RECOMPLETION_NOTHING, $choices));

        $settings->add(new admin_setting_configcheckbox('local_recompletion/archivebooking',
            new lang_string('archivebooking', 'local_recompletion'), '', 1));
    }

    /**
     * Reset booking records.
     *
     * @param int $userid - user id
     * @param stdClass $course - course record.
     * @param stdClass $config - recompletion config.
     */
    public static function reset(int $userid, stdClass $course, stdClass $config): void {
        global $DB;

        if (empty($config->booking)) {
            return;
        }

        if ($config->booking == LOCAL_RECOMPLETION_DELETE) {

            $tables = [
                'booking_answers' => 'local_recompletion_ba',
                'booking_options' => 'local_recompletion_bo',
                'booking_teachers' => 'local_recompletion_bt',
            ];

            $params = ['userid' => $userid, 'course' => $course->id];
            $sql = 'userid = :userid AND bookingid IN (SELECT id FROM {booking} WHERE course = :course)';

            if ($config->archivebooking) {
                foreach ($tables as $originaltable => $archivetable) {
                    $records = $DB->get_records_select($originaltable, $sql, $params);
                    if (!empty($records)) {
                        foreach ($records as $record) {
                            $record->course = $course->id;
                        }
                        $DB->insert_records($archivetable, $records);
                    }
                }
            }

            // Delete original records after archiving.
            foreach ($tables as $originaltable => $archivetable) {
                $DB->delete_records_select($originaltable, $sql, $params);
            }
        }
    }
}