Change of event duration when event moved to restricted time

Hello,

I know this is a long shot but I’ve got to ask. Is there such a funcionality available now ?

If I move my event in TimeLine that lasts for example for exactly 2 days on a blocked(restricted) day lets say sunday or some kind of holiday is there some kind of functionality that will make the event automatically longer ? So it will begin on saturday and finish on monday ? Thus it will last for 2 days.

This can also be probably achieved by modifing the Save Action Update(invoked when event is drag and dropped) to change the time of event based on conditions.

But than how I can change the code for the event to be also resized when dropped(now I’d have to refresh the page for the resize to have effect) ?

Hello,
probably this can be done on the client-side (using JS API of the component), by adjusting event duration right after it was modified, but before changes were saved to db.

docs.dhtmlx.com/scheduler/api__s … event.html
docs.dhtmlx.com/scheduler/api__s … event.html

However, there is no function to retreive start/end dates of the blocked time (so you can’t calculate amount of blocked time inside the event easily). If you block only weekends, you may simply check days between events start and event end. But if there may be custom blockings, you’ll need to pass this info on the client-side somehow.

Well I’d prefer to do this on the server side. I actually know the dates of the restricted time there because they re defined on the server side.

I figured out how to do this (I pasted a code below) so that the events end time changes when I drag it on restricted dates. This is necessary for me because its vital in my project for the events to have a constant time by default when not on restricted dates.

Now this is done for holidays and Sundays.

Here is my problem now:

If I update the event using my Save method I get the correct time in my database but It doesn’t update on the client side. How can I modify my Save method to update event on the client side when i drop my event ?

Here is a Save method in my controller:

 public ContentResult Save(int? id, FormCollection actionValues)
        {
            var action = new DataAction(actionValues);

            try
            {
                var changedEvent = (CalendarEvent)DHXEventsHelper.Bind(typeof(CalendarEvent), actionValues);

                var _repositoryEvents = new EventsRepository();

                switch (action.Type)
                {
                    case DataActionTypes.Insert:
                        //......
                        break;

                    case DataActionTypes.Delete:
                        //.....

                    default: // "update"       

                        //If event is dragged on a restricted day(holiday, sunday etc change time of the event)
                        //!!!!This has to be called before data is updated to database
                        changedEvent = RestrictedDates.CheckForRestrictedTimeInDraggedEvent(changedEvent);

                        _repositoryEvents.Update(changedEvent);

                        break;
                }
            }
            catch
            {
                action.Type = DataActionTypes.Error;
            }
            return (ContentResult)new AjaxSaveResponse(action);
        }

Here is a class I created to change the time:

public static class RestrictedDates
    {
        //return number of time(in hours) that must be added to the event if it is dragged on a restricted day
        public static CalendarEvent CheckForRestrictedTimeInDraggedEvent(CalendarEvent calEvent)
        {
            //For updated event
            int getUpdatedNumberOfMinutes = GetTimeOfEvent(calEvent);

            //For Old event
            var _repositoryEvents = new EventsRepository();
            var oldEvent = _repositoryEvents.Select(calEvent.id);
            int getOldNumberOfMinutes = GetTimeOfEvent(oldEvent);

            int addMinutes = getUpdatedNumberOfMinutes - getOldNumberOfMinutes;

            calEvent.end_date = calEvent.end_date.AddMinutes(addMinutes);

            return calEvent;
        }

        private static int GetTimeOfEvent(CalendarEvent calEvent)
        {

            int numberOfMinutes = 0;
            const int minInDay = 24 * 60;
            int addTime = 0;


            for (DateTime date = calEvent.start_date; calEvent.end_date.CompareTo(date) >= 0; date = date.AddDays(1))
            {
                if (calEvent.start_date == date)
                {
                    addTime = minInDay - (calEvent.start_date.Hour * 60 + calEvent.start_date.Minute);
                }
                else if (calEvent.end_date == date)
                {
                    addTime = calEvent.start_date.Hour * 60 + calEvent.start_date.Minute;

                }
                else
                {
                    addTime = minInDay;
                }

                if (date.DayOfWeek == DayOfWeek.Sunday) numberOfMinutes = numberOfMinutes + addTime;
                if (date.Month == 5 && date.Day == 1) numberOfMinutes = numberOfMinutes + addTime; ; // 1 maja
                if (date.Month == 5 && date.Day == 3) numberOfMinutes = numberOfMinutes + addTime; // 3 maja
                if (date.Month == 8 && date.Day == 15) numberOfMinutes = numberOfMinutes + addTime; // Wniebowzięcie Najświętszej Marii Panny, Święto Wojska Polskiego
                if (date.Month == 11 && date.Day == 1) numberOfMinutes = numberOfMinutes + addTime; // Dzień Wszystkich Świętych
                if (date.Month == 11 && date.Day == 11) numberOfMinutes = numberOfMinutes + addTime; // Dzień Niepodległości 
                if (date.Month == 12 && date.Day == 25) numberOfMinutes = numberOfMinutes + addTime; // Boże Narodzenie
                if (date.Month == 12 && date.Day == 26) numberOfMinutes = numberOfMinutes + addTime; // Boże Narodzenie
                int a = date.Year % 19;
                int b = date.Year % 4;
                int c = date.Year % 7;
                int d = (a * 19 + 24) % 30;
                int e = (2 * b + 4 * c + 6 * d + 5) % 7;
                if (d == 29 && e == 6) d -= 7;
                if (d == 28 && e == 6 && a > 10) d -= 7;
                DateTime Easter = new DateTime(date.Year, 3, 22).AddDays(d + e);
                if (date.AddDays(-1) == Easter)
                    numberOfMinutes = numberOfMinutes + addTime; // Wielkanoc (poniedziałek)
                if (date.AddDays(-60) == Easter)
                    numberOfMinutes = numberOfMinutes + addTime; // Boże Ciało
            }

            

            return numberOfMinutes;
        }
      
    }

You can pass updates from the server side back to client-side
scheduler-net.com/docs/managing_ … the_server

Init: scheduler.UpdateFieldsAfterSave();
Save: var response = new AjaxSaveResponse(action); response.UpdateFields(new Dictionary<string,object>{ {"start_date", changedEvent.start_date}, {"end_date", changedEvent.end_date} }; return (ContentResult)response;

Yeap! This works perfectly.

Thanks a lot. I think you are missing ,)" in your documentation

var response = new AjaxSaveResponse(action);
response.UpdateFields(new Dictionary<string,object>{
{“start_date”, changedEvent.start_date},
{“end_date”, changedEvent.end_date}
});
return (ContentResult)response;