summaryrefslogtreecommitdiff
path: root/includes/model
diff options
context:
space:
mode:
Diffstat (limited to 'includes/model')
-rw-r--r--includes/model/AngelType_model.php3
-rw-r--r--includes/model/ShiftEntry_model.php20
-rw-r--r--includes/model/Shifts_model.php137
-rw-r--r--includes/model/UserAngelTypes_model.php9
-rw-r--r--includes/model/UserGroups_model.php17
-rw-r--r--includes/model/User_model.php28
6 files changed, 148 insertions, 66 deletions
diff --git a/includes/model/AngelType_model.php b/includes/model/AngelType_model.php
index df5e6ae1..d0119e6f 100644
--- a/includes/model/AngelType_model.php
+++ b/includes/model/AngelType_model.php
@@ -2,7 +2,8 @@
/**
* Delete an Angeltype.
- * @param Angeltype $angeltype
+ *
+ * @param Angeltype $angeltype
*/
function AngelType_delete($angeltype) {
return sql_query("
diff --git a/includes/model/ShiftEntry_model.php b/includes/model/ShiftEntry_model.php
index 97fdf784..332620f6 100644
--- a/includes/model/ShiftEntry_model.php
+++ b/includes/model/ShiftEntry_model.php
@@ -1,9 +1,25 @@
<?php
/**
+ * Returns next (or current) shifts of given user.
+ * @param User $user
+ */
+function ShiftEntries_upcoming_for_user($user) {
+ return sql_select("
+ SELECT *
+ FROM `ShiftEntry`
+ JOIN `Shifts` ON `Shifts`.`SID`=`ShiftEntry`.`SID`
+ WHERE `ShiftEntry`.`UID`=" . sql_escape($user['UID']) . "
+ AND `Shifts`.`end` > " . sql_escape(time()) . "
+ ORDER BY `Shifts`.`end`
+ ");
+}
+
+/**
* Returns all shift entries in given shift for given angeltype.
- * @param int $shift_id
- * @param int $angeltype_id
+ *
+ * @param int $shift_id
+ * @param int $angeltype_id
*/
function ShiftEntries_by_shift_and_angeltype($shift_id, $angeltype_id) {
return sql_select("
diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php
index 8cd4b3c2..5d0ec4a2 100644
--- a/includes/model/Shifts_model.php
+++ b/includes/model/Shifts_model.php
@@ -1,79 +1,90 @@
<?php
+function Shifts_by_user($user) {
+ return sql_select("
+ SELECT *
+ FROM `ShiftEntry`
+ JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`)
+ JOIN `Room` ON (`Shifts`.`RID` = `Room`.`RID`)
+ WHERE `UID`=" . sql_escape($user['UID']) . "
+ ORDER BY `start`
+ ");
+}
/**
* TODO: $_REQUEST is not allowed in model!
* Returns Shift id array
*/
function Shifts_filtered() {
- global $_REQUEST;
- $filter = "";
-
- // filterRoom (Array of integer) - Array of Room IDs (optional, for list request)
- if (isset($_REQUEST['filterRoom']) && is_array($_REQUEST['filterRoom']) ) {
- foreach ( $_REQUEST['filterRoom'] as $key => $value ) {
- $filter .= ", `RID`=" . sql_escape($value) . " ";
- }
- }
-
- //filterTask (Array of integer) - Array if Task (optional, for list request)
- if (isset($_REQUEST['filterTask']) && is_array($_REQUEST['filterTask']) ) {
- foreach ( $_REQUEST['filterTask'] as $key => $value ) {
-// TODO $filter .= ", `RID`=" . sql_escape($value) . " ";
- }
- }
-
- // filterOccupancy (integer) - Occupancy state: (optional, for list request)
- // 1 occupied, 2 free, 3 occupied and free
- if (isset($_REQUEST['filterOccupancy']) && is_array($_REQUEST['filterOccupancy']) ) {
- foreach ( $_REQUEST['filterOccupancy'] as $key => $value ) {
-// TODO $filter .= ", `RID`=" . sql_escape($value) . " ";
- }
- }
-
- // format filter
- if( $filter != "" ) {
- $filter = ' WHERE '. substr($filter, 1);
- }
-
- // real request
- $shifts_source = sql_select("SELECT `SID` FROM `Shifts`". $filter);
- if ($shifts_source === false)
- return false;
- if (count($shifts_source) > 0) {
- return $shifts_source;
- }
- return null;
+ global $_REQUEST;
+ $filter = "";
+
+ // filterRoom (Array of integer) - Array of Room IDs (optional, for list request)
+ if (isset($_REQUEST['filterRoom']) && is_array($_REQUEST['filterRoom'])) {
+ foreach ($_REQUEST['filterRoom'] as $key => $value) {
+ $filter .= ", `RID`=" . sql_escape($value) . " ";
+ }
+ }
+
+ // filterTask (Array of integer) - Array if Task (optional, for list request)
+ if (isset($_REQUEST['filterTask']) && is_array($_REQUEST['filterTask'])) {
+ foreach ($_REQUEST['filterTask'] as $key => $value) {
+ // TODO $filter .= ", `RID`=" . sql_escape($value) . " ";
+ }
+ }
+
+ // filterOccupancy (integer) - Occupancy state: (optional, for list request)
+ // 1 occupied, 2 free, 3 occupied and free
+ if (isset($_REQUEST['filterOccupancy']) && is_array($_REQUEST['filterOccupancy'])) {
+ foreach ($_REQUEST['filterOccupancy'] as $key => $value) {
+ // TODO $filter .= ", `RID`=" . sql_escape($value) . " ";
+ }
+ }
+
+ // format filter
+ if ($filter != "") {
+ $filter = ' WHERE ' . substr($filter, 1);
+ }
+
+ // real request
+ $shifts_source = sql_select("SELECT `SID` FROM `Shifts`" . $filter);
+ if ($shifts_source === false)
+ return false;
+ if (count($shifts_source) > 0) {
+ return $shifts_source;
+ }
+ return null;
}
/**
* Returns Shift by id.
*
- * @param $id Shift ID
+ * @param $id Shift
+ * ID
*/
function Shift($id) {
- $shifts_source = sql_select("SELECT * FROM `Shifts` WHERE `SID`=" . sql_escape($id) . " LIMIT 1");
- $shiftsEntry_source = sql_select("SELECT `TID` , `UID` , `freeloaded` FROM `ShiftEntry` WHERE `SID`=" . sql_escape($id) );
-
- if ($shifts_source === false)
- return false;
- if (count($shifts_source) > 0) {
- $result = $shifts_source[0];
-
- $result['ShiftEntry'] = $shiftsEntry_source;
-
- $temp = NeededAngelTypes_by_shift($id);
- foreach( $temp as $e)
- {
- $result['NeedAngels'][] = array (
- 'TID' => $e['angel_type_id'],
- 'count' => $e['count'],
- 'restricted' => $e['restricted'],
- 'taken' => $e['taken'] );
- }
-
- return $result;
- }
- return null;
+ $shifts_source = sql_select("SELECT * FROM `Shifts` WHERE `SID`=" . sql_escape($id) . " LIMIT 1");
+ $shiftsEntry_source = sql_select("SELECT `TID` , `UID` , `freeloaded` FROM `ShiftEntry` WHERE `SID`=" . sql_escape($id));
+
+ if ($shifts_source === false)
+ return false;
+ if (count($shifts_source) > 0) {
+ $result = $shifts_source[0];
+
+ $result['ShiftEntry'] = $shiftsEntry_source;
+
+ $temp = NeededAngelTypes_by_shift($id);
+ foreach ($temp as $e) {
+ $result['NeedAngels'][] = array(
+ 'TID' => $e['angel_type_id'],
+ 'count' => $e['count'],
+ 'restricted' => $e['restricted'],
+ 'taken' => $e['taken']
+ );
+ }
+
+ return $result;
+ }
+ return null;
}
/**
@@ -92,7 +103,7 @@ function Shifts() {
$needed_angeltypes = NeededAngelTypes_by_shift($shift['SID']);
if ($needed_angeltypes === false)
return false;
-
+
$shift['angeltypes'] = $needed_angeltypes;
}
diff --git a/includes/model/UserAngelTypes_model.php b/includes/model/UserAngelTypes_model.php
index a9944ff1..4a25d25a 100644
--- a/includes/model/UserAngelTypes_model.php
+++ b/includes/model/UserAngelTypes_model.php
@@ -3,6 +3,15 @@
* User angeltypes model
*/
+function User_angeltypes($user) {
+ return sql_select("
+ SELECT `AngelTypes`.*, `UserAngelTypes`.`confirm_user_id`, `UserAngelTypes`.`coordinator`
+ FROM `UserAngelTypes`
+ JOIN `AngelTypes` ON `UserAngelTypes`.`angeltype_id` = `AngelTypes`.`id`
+ WHERE `UserAngelTypes`.`user_id`=" . sql_escape($user['UID']) . "
+ ");
+}
+
/**
* Gets unconfirmed user angeltypes for angeltypes of which the given user is a coordinator.
*
diff --git a/includes/model/UserGroups_model.php b/includes/model/UserGroups_model.php
new file mode 100644
index 00000000..1d018386
--- /dev/null
+++ b/includes/model/UserGroups_model.php
@@ -0,0 +1,17 @@
+<?php
+
+/**
+ * Returns users groups
+ * @param User $user
+ */
+function User_groups($user) {
+ return sql_select("
+ SELECT `Groups`.*
+ FROM `UserGroups`
+ JOIN `Groups` ON `Groups`.`UID`=`UserGroups`.`group_id`
+ WHERE `UserGroups`.`uid`=" . sql_escape($user['UID']) . "
+ ORDER BY `UserGroups`.`group_id`
+ ");
+}
+
+?> \ No newline at end of file
diff --git a/includes/model/User_model.php b/includes/model/User_model.php
index c8c48e6c..45600676 100644
--- a/includes/model/User_model.php
+++ b/includes/model/User_model.php
@@ -4,6 +4,34 @@
*/
/**
+ * Returns -seconds until free if user is busy or seconds until next shift.
+ * 0 if there is an error or no upcoming shift.
+ *
+ * @param User $user
+ */
+function User_shift_state($user) {
+ $shifts = ShiftEntries_upcoming_for_user($user);
+ if ($shifts === false)
+ return 0;
+ if (count($shifts) == 0)
+ return 0;
+ if ($shifts[0]['start'] < time())
+ return $shifts[0]['end'] - time();
+ return $shifts[0]['start'] - time();
+}
+
+/**
+ * Returns true if user is freeloader
+ *
+ * @param User $user
+ */
+function User_is_freeloader($user) {
+ global $max_freeloadable_shifts, $user;
+
+ return count(ShiftEntries_freeloaded_by_user($user)) >= $max_freeloadable_shifts;
+}
+
+/**
* Returns all users that are not member of given angeltype.
*
* @param Angeltype $angeltype