From dca8ee80de74568073121ed9a0edb863a23f1a87 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sat, 11 Apr 2015 14:46:37 +0200 Subject: [PATCH] Assigning positions WORKS --- Logic/Swarm/Person.cs | 31 +++++++++++ Logic/Swarm/PositionAssignments.cs | 9 ++++ Site5/App_GlobalResources/Controls.Swarm.resx | 10 ++++ Site5/App_GlobalResources/Global.resx | 3 ++ Site5/Automation/Json-PositionsTree.aspx.cs | 18 ++++--- Site5/Automation/SwarmFunctions.aspx.cs | 52 +++++++++++++++++-- Site5/Controls/v5/Swarm/ComboPeople.ascx | 7 +++ Site5/Controls/v5/Swarm/TreePositions.ascx | 41 ++++++++++++--- Site5/Controls/v5/Swarm/TreePositions.ascx.cs | 2 + 9 files changed, 153 insertions(+), 20 deletions(-) diff --git a/Logic/Swarm/Person.cs b/Logic/Swarm/Person.cs index 90392b98..ab284b79 100644 --- a/Logic/Swarm/Person.cs +++ b/Logic/Swarm/Person.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Drawing; using System.Globalization; +using System.Linq; using System.Text; using System.Text.RegularExpressions; using NBitcoin; @@ -1108,6 +1109,28 @@ namespace Swarmops.Logic.Swarm return GetAvatarLink (pixelSize).Replace ("http://www", "https://secure"); } + + public PositionAssignment PositionAssignment + { + get + { + PositionAssignments candidateSet = this.PositionAssignments; + // candidateSet = (PositionAssignments) candidateSet.Where (assignment => assignment.Active); THIS THROWS, FIX WHEN THIS LOGIC IS NEEDED + + if (candidateSet.Count != 1) + { + throw new ArgumentException ("No active position"); + } + + return candidateSet[0]; + } + } + + public PositionAssignments PositionAssignments + { + get { return PositionAssignments.ForPerson (this); } + } + public bool HasAccess (Access access) { if (access == null) @@ -1156,6 +1179,14 @@ namespace Swarmops.Logic.Swarm return true; } + // ENTERING TEMPORARY TERRITORY HERE, UNTIL REAL ACCESS COMPLETED + + if (access.Organization == null) + { + access = new Access (Organization.FromIdentity (1), access.Aspect, access.Type); + } + + if (access.Type == AccessType.Write) { ids = access.Organization.Parameters.TemporaryAccessListWrite; diff --git a/Logic/Swarm/PositionAssignments.cs b/Logic/Swarm/PositionAssignments.cs index 2b323a57..5c5d844d 100644 --- a/Logic/Swarm/PositionAssignments.cs +++ b/Logic/Swarm/PositionAssignments.cs @@ -25,5 +25,14 @@ namespace Swarmops.Logic.Swarm return FromArray (SwarmDb.GetDatabaseForReading().GetPositionAssignments (positions)); } + + public static PositionAssignments ForPerson (Person person, bool includeTerminated = false) + { + if (!includeTerminated) + { + return FromArray (SwarmDb.GetDatabaseForReading().GetPositionAssignments (person, DatabaseCondition.ActiveTrue)); + } + return FromArray(SwarmDb.GetDatabaseForReading().GetPositionAssignments(person)); + } } } diff --git a/Site5/App_GlobalResources/Controls.Swarm.resx b/Site5/App_GlobalResources/Controls.Swarm.resx index 8378319c..47faef02 100644 --- a/Site5/App_GlobalResources/Controls.Swarm.resx +++ b/Site5/App_GlobalResources/Controls.Swarm.resx @@ -134,10 +134,20 @@ Assign this person to position + + Assign + Kind of "hire" but also for volunteers + Assign person as <span id='{0}_modalPositionName'>...</span> Header for new assignment dialog + + Failed: Can't assign any more people to this position. + + + Please select a person to assign. + (vacant) For unassigned positions diff --git a/Site5/App_GlobalResources/Global.resx b/Site5/App_GlobalResources/Global.resx index bd507fe0..732efca3 100644 --- a/Site5/App_GlobalResources/Global.resx +++ b/Site5/App_GlobalResources/Global.resx @@ -680,6 +680,9 @@ Position + + It seems another user beat you to this operation; the database changed while you were looking. Please examine the changed conditions and retry what you were trying to do. + Type a name... diff --git a/Site5/Automation/Json-PositionsTree.aspx.cs b/Site5/Automation/Json-PositionsTree.aspx.cs index a4d1e879..7e20e71c 100644 --- a/Site5/Automation/Json-PositionsTree.aspx.cs +++ b/Site5/Automation/Json-PositionsTree.aspx.cs @@ -18,7 +18,7 @@ namespace Swarmops.Frontend.Automation // If level if system-wide, execute this block. Also, move the data to the real place, please JsonPositions rootPositions = new JsonPositions(); - _customCookieClass = Request["CustomCookie"]; // may be null and that's ok + _customCookieClass = "LocalPosition" + Request["Cookie"]; // may be null and that's ok Tree systemPositions = Positions.ForSystem().Tree; @@ -43,14 +43,15 @@ namespace Swarmops.Frontend.Automation PositionAssignments assignments = position.Assignments; string expires = string.Empty; - string assignedName = string.Format("{0} {2}", Resources.Controls.Swarm.Positions_Vacant, _customCookieClass, Resources.Controls.Swarm.Positions_AssignPerson, position.Identity, JavascriptEscape(position.Localized())); + string assignedName = string.Format("{2} {0}", Resources.Controls.Swarm.Positions_Vacant, _customCookieClass, Resources.Controls.Swarm.Positions_AssignPerson, position.Identity, JavascriptEscape(position.Localized())); if (assignments.Count > 0) { assignedName = assignments[0].Person.Canonical; - if (assignments[0].ExpiresDateTimeUtc.Year > 2000) // as in, "is defined" + int expireYear = assignments[0].ExpiresDateTimeUtc.Year; + if (expireYear > 2000 && expireYear < 3000) // as in, "is defined" { - expires = assignments[0].ExpiresDateTimeUtc.ToString ("yyyy-MM-dd"); + expires = assignments[0].ExpiresDateTimeUtc.ToString("yyyy-MMM-dd"); } } @@ -70,9 +71,10 @@ namespace Swarmops.Frontend.Automation elements.Add ("{" + element + "}"); expires = string.Empty; - if (assignments[assignmentCount].ExpiresDateTimeUtc.Year > 2000) // as in, "is defined" + int expireYear = assignments[assignmentCount].ExpiresDateTimeUtc.Year; + if (expireYear > 2000 && expireYear < 3000) // as in, "is defined" { - expires = assignments[assignmentCount].ExpiresDateTimeUtc.ToString ("yyyy-MM-dd"); + expires = assignments[assignmentCount].ExpiresDateTimeUtc.ToString ("yyyy-MMM-dd"); } element = String.Format ( @@ -82,14 +84,14 @@ namespace Swarmops.Frontend.Automation assignmentCount++; } - if (assignmentCount < position.MaxCount) + if (assignmentCount < position.MaxCount || (position.MaxCount == 0 && position.Assignments.Count > 0)) { // finally, if the assigned count is less than max count, add a "assign another person" link elements.Add ("{" + element + "}"); string addPerson = string.Format ( - "{0}", + "{0}", Resources.Controls.Swarm.Positions_AssignMorePerson, position.Identity, JavascriptEscape (position.Localized()), _customCookieClass); diff --git a/Site5/Automation/SwarmFunctions.aspx.cs b/Site5/Automation/SwarmFunctions.aspx.cs index 6e694b28..63ba889a 100644 --- a/Site5/Automation/SwarmFunctions.aspx.cs +++ b/Site5/Automation/SwarmFunctions.aspx.cs @@ -6,7 +6,9 @@ using System.Web.Services; using System.Web.UI; using System.Web.UI.WebControls; using Swarmops.Logic.Security; +using Swarmops.Logic.Structure; using Swarmops.Logic.Swarm; +using Swarmops.Common.Exceptions; namespace Swarmops.Frontend.Automation { @@ -18,13 +20,53 @@ namespace Swarmops.Frontend.Automation } [WebMethod] - public static AjaxCallResult AssignPosition (string cookie) + public static AjaxCallResult AssignPosition (int personId, int positionId, int durationMonths, int organizationId, int geographyId) { - string[] dataParts = cookie.Split ('-'); - int positionId = Convert.ToInt32(dataParts[0]); - int personId = Convert.ToInt32 (dataParts[2]); + AuthenticationData authData = GetAuthenticationDataAndCulture(); + Position position = Position.FromIdentity (positionId); + Person person = Person.FromIdentity (personId); + Organization organization = (organizationId == 0 ? null : Organization.FromIdentity (organizationId)); + Geography geography = (geographyId == 0 ? null : Geography.FromIdentity (geographyId)); - return new AjaxCallResult(); // TODO + if ((position.OrganizationId > 0 && authData.CurrentOrganization.Identity != position.OrganizationId) || person.Identity < 0) + { + throw new UnauthorizedAccessException(); + } + if (!authData.CurrentUser.HasAccess (new Access (AccessAspect.Administration))) + { + throw new UnauthorizedAccessException(); + } + + if (position.MaxCount > 0 && position.Assignments.Count >= position.MaxCount) + { + return new AjaxCallResult + { + Success = false, + DisplayMessage = Resources.Controls.Swarm.Positions_NoMorePeopleOnPosition + }; + } + + // Deliberate: no requirement for membership (or equivalent) in order to be assigned to position. + + Position currentUserPosition = authData.CurrentUser.PositionAssignment.Position; // excludes acting positions. May throw! + DateTime? expiresUtc = null; + + if (durationMonths > 0) + { + expiresUtc = DateTime.UtcNow.AddMonths (durationMonths); + } + + try + { + PositionAssignment.Create (organization, geography, position, person, authData.CurrentUser, currentUserPosition, + expiresUtc, string.Empty); + } + catch (DatabaseConcurrencyException) + { + return new AjaxCallResult {Success = false, DisplayMessage = Resources.Global.Error_DatabaseConcurrency}; + } + + return new AjaxCallResult() {Success = true}; } public class AvatarData : AjaxCallResult diff --git a/Site5/Controls/v5/Swarm/ComboPeople.ascx b/Site5/Controls/v5/Swarm/ComboPeople.ascx index 658c3cea..84e3bbfb 100644 --- a/Site5/Controls/v5/Swarm/ComboPeople.ascx +++ b/Site5/Controls/v5/Swarm/ComboPeople.ascx @@ -119,6 +119,13 @@ function <%=this.ClientID %>_clear() { <%=this.ClientID%>_val(''); <%=this.ClientID%>_avatarUrl(''); + $('#<%=this.ClientID%>_DropPeople').combobox('reload'); + $('#<%=this.ClientID%>_DropPeople').combobox('clear'); + <%=this.ClientID%>_pvt_selectedPersonId = 0; + } + + function <%=this.ClientID %>_focus() { + $('span#<%= this.ClientID %>_SpanPeople span input.textbox-text').focus(); } diff --git a/Site5/Controls/v5/Swarm/TreePositions.ascx b/Site5/Controls/v5/Swarm/TreePositions.ascx index bf11a987..ca2f6d4d 100644 --- a/Site5/Controls/v5/Swarm/TreePositions.ascx +++ b/Site5/Controls/v5/Swarm/TreePositions.ascx @@ -9,11 +9,13 @@ { onLoadSuccess: function () { - $('.LocalAssignPerson').click(function () { - currentPosition = $(this).attr("positionId"); + $('.LocalAssignPerson.LocalPosition<%=this.Cookie%>').click(function () { + currentPositionId = $(this).attr("positionId"); $('#<%= this.ClientID %>_modalPositionName').text(decodeURIComponent($(this).attr("positionName"))); <%= this.DropPerson.ClientID%>_clear(); + $('#<%= this.DropDuration.ClientID%>').val("12"); // reset to 12-month default <%= this.DialogAdd.ClientID %>_open(); + <%= this.DropPerson.ClientID%>_focus(); }); } @@ -21,18 +23,43 @@ }); $('#<%=this.ClientID%>_buttonAssign').click(function() { - alert("foo!"); + var personId = <%=this.DropPerson.ClientID%>_selectedPersonId(); + + if (personId == 0) { + // abort + alert("No person selected, not assigning anybody"); + return; + } + + // TODO: Check existing position for person; can't have two positions + + SwarmopsJS.ajaxCall( + "/Automation/SwarmFunctions.aspx/AssignPosition", + { personId: personId, positionId: currentPositionId, durationMonths: $('#<%= this.DropDuration.ClientID%>').val(), organizationId: 0, geographyId: 0 }, + function (result) { + // Close modal and reload grid regardless of success or not + // (the only failure here should be a concurrency error, in which case + // the user needs to examine the new state anyway) + + <%= this.DialogAdd.ClientID %>_close(); + $('#<%=this.ClientID%>_tablePositions').treegrid('reload'); + if (result.Success) { + // success + } else { + alertify.error(result.DisplayMessage); + } + }); }); }); - var currentPosition = ''; + var currentPositionId = ''; - + @@ -54,7 +81,7 @@
​
​
- +