From f3dac109bba1b82902daf3ceb4c20b8ccca9b092 Mon Sep 17 00:00:00 2001
From: Viktoria Wiebe <vwiebe@uni-osnabrueck.de>
Date: Tue, 1 Mar 2022 17:20:06 +0000
Subject: [PATCH] fix #348 - re-add option to exclude resource from global
 locks

---
 app/controllers/resources/room.php          |  3 +++
 app/views/resources/room/_add_edit_form.php |  9 +++++--
 db/migrations/5.1.24_biest_348.php          | 30 +++++++++++++++++++++
 lib/models/resources/Resource.class.php     |  6 ++---
 4 files changed, 43 insertions(+), 5 deletions(-)
 create mode 100644 db/migrations/5.1.24_biest_348.php

diff --git a/app/controllers/resources/room.php b/app/controllers/resources/room.php
index 33b259c5d93..7b1ad87d543 100644
--- a/app/controllers/resources/room.php
+++ b/app/controllers/resources/room.php
@@ -289,6 +289,7 @@ class Resources_RoomController extends AuthenticatedController
                 $this->name                   = Request::get('name');
                 $this->description            = Request::get('description');
                 $this->requestable            = Request::int('requestable');
+                $this->lockable               = Request::int('lockable', 0);
                 $this->room_type              = Request::get('room_type');
                 $this->seats                  = Request::int('seats');
                 $this->booking_plan_is_public = Request::get('booking_plan_is_public');
@@ -363,6 +364,7 @@ class Resources_RoomController extends AuthenticatedController
                 $this->room->name        = $this->name;
                 $this->room->description = $this->description;
                 $this->room->requestable = strval($this->requestable);
+                $this->room->lockable    = $this->lockable;
                 if ($GLOBALS['perm']->have_perm('root')) {
                     $this->room->sort_position = $this->sort_position;
                 }
@@ -458,6 +460,7 @@ class Resources_RoomController extends AuthenticatedController
                 $this->name                   = $this->room->name;
                 $this->description            = $this->room->description;
                 $this->requestable            = '1';
+                $this->lockable               = 1;
                 $this->room_type              = $this->room->room_type;
                 $this->seats                  = $this->room->seats;
                 $this->booking_plan_is_public = (bool)$this->room->booking_plan_is_public;
diff --git a/app/views/resources/room/_add_edit_form.php b/app/views/resources/room/_add_edit_form.php
index 6cd3fd2bbda..f0a640bb0b2 100644
--- a/app/views/resources/room/_add_edit_form.php
+++ b/app/views/resources/room/_add_edit_form.php
@@ -29,9 +29,14 @@
             </label>
             <label>
                 <input type="checkbox" name="requestable" value="1"
-                       <?= $room->requestable ? 'checked="checked"' : '' ?>>
+                       <?= $room->requestable ? 'checked' : '' ?>>
                 <?= _('Raum ist wünschbar') ?>
             </label>
+            <label>
+                <input type="checkbox" name="lockable" value="1"
+                       <?= $room->lockable ? 'checked' : '' ?>>
+                <?= _('Globale Buchungssperren gelten für diesen Raum') ?>
+            </label>
             <label>
                 <?= _('Raumtyp') ?>
                 <input type="text" name="room_type" value="<?= htmlReady($room_type) ?>">
@@ -42,7 +47,7 @@
             </label>
             <label>
                 <input type="checkbox" name="booking_plan_is_public" value="1"
-                       <?= $booking_plan_is_public ? 'checked="checked"' : '' ?>>
+                       <?= $booking_plan_is_public ? 'checked' : '' ?>>
                 <?= _('Raumplan ist öffentlich zugänglich') ?>
             </label>
             <? if ($GLOBALS['perm']->have_perm('root')): ?>
diff --git a/db/migrations/5.1.24_biest_348.php b/db/migrations/5.1.24_biest_348.php
new file mode 100644
index 00000000000..67f854fc69b
--- /dev/null
+++ b/db/migrations/5.1.24_biest_348.php
@@ -0,0 +1,30 @@
+<?php
+class Biest348 extends Migration
+{
+    public function description ()
+    {
+        return 'Adds a column to the resources table to mark resources as lockable, default is 1.';
+    }
+
+    public function up()
+    {
+        if ($this->columnExists()) {
+            return;
+        }
+
+        $query = 'ALTER TABLE `resources`
+                  ADD `lockable` TINYINT(1) UNSIGNED NOT NULL DEFAULT 1 AFTER `requestable`';
+        DBManager::get()->exec($query);
+    }
+
+    public function down()
+    {
+        $query = 'ALTER TABLE `resources` DROP `lockable`';
+        DBManager::get()->exec($query);
+    }
+
+    private function columnExists()
+    {
+        return DBManager::get()->fetchFirst("SHOW COLUMNS FROM `resources` LIKE 'lockable'");
+    }
+}
diff --git a/lib/models/resources/Resource.class.php b/lib/models/resources/Resource.class.php
index 90690397a1a..7fbaf575fc1 100644
--- a/lib/models/resources/Resource.class.php
+++ b/lib/models/resources/Resource.class.php
@@ -2371,7 +2371,7 @@ class Resource extends SimpleORMap implements StudipItem
         }
         //Now we must check for global resource locks:
 
-        if (GlobalResourceLock::currentlyLocked()) {
+        if ($this->lockable && GlobalResourceLock::currentlyLocked()) {
             //The resource management system is currently locked.
             //permission level 'user' for all other permission
             //levels.
@@ -2431,7 +2431,7 @@ class Resource extends SimpleORMap implements StudipItem
                 return false;
             }
         } elseif ($permission === 'autor') {
-            if (GlobalResourceLock::currentlyLocked()) {
+            if ($this->lockable && GlobalResourceLock::currentlyLocked()) {
                 //A global resource lock means no writing actions are permitted.
                 return false;
             }
@@ -2441,7 +2441,7 @@ class Resource extends SimpleORMap implements StudipItem
                 return false;
             }
         } elseif ($permission === 'tutor') {
-            if (GlobalResourceLock::currentlyLocked()) {
+            if ($this->lockable && GlobalResourceLock::currentlyLocked()) {
                 //A global resource lock means no writing actions are permitted.
                 return false;
             }
-- 
GitLab