Examples of Magic Data in Users and Groups

Some examples to try in the symbol tester for users and group exit times.

When learning Magic Data, it often helps to build up sets of symbols piece by piece and evaluate each part and also conduct some experiments on the way. The examples below assume we have a group called Members which is time-limited.

Return a number for the group ID, showing that you have such a group.

SET Members AS_GROUP

Return the exit time from the group (if it is in the future) or null if the user is no longer in the group. This will be a long number like 1378045849 (but above that, this is the time I am writing).

UID GROUP_EXIT_TIME Members

Return today's date.

TIME DATE "d M Y" 

A more verbose format of today's date. The DATE symbol will process a date format string or a concrete5 defined constant that supplies a date format string.

TIME DATE DATE_APP_GENERIC_MDYT_FULL 

Putting the above together, when the current user is due to exit a group.

UID GROUP_EXIT_TIME Members DATE "d M Y"

Adding an alternate message for users who ar not in the group.

UID GROUP_EXIT_TIME Members ZERO_AS_THEN_END "You are not a member" DATE "d M Y"

Making the date nicer with the symbol FRIENDLY_DATE from the Last Updated addon.

UID GROUP_EXIT_TIME Members FRIENDLY_DATE Human

Once you have the above working, bear in mind that you also need to cater for the situation of users not in the Members group. You could do that using more complex Magic Data expressions to add an alternate message for users who ar not in the group.

UID GROUP_EXIT_TIME Members ZERO_AS_THEN_END "You are not a member" DATE FRIENDLY_DATE Human

However, its probably easier and more practical to use block permissions and only show this block to Members, then another block about joining for those who are not Members.

Jumping straight in to something more complicated, then I will explain. Here is an expression that lists all groups a user is in and how long they have left in that group.

UID LIST_GROUPS 
APPLY_EACH 
  GROUP_NAME SAVE "m1" 
    ( 
      UID GROUP_EXIT_TIME ( "m1" RETRIEVE ) 
      SAVE "m2"
      INT_MAX_AS_THEN_END "Unlimited"
      FRIENDLY_DATE Human 
     )  SAVE "m3"
  "m2" RETRIEVE ZERO_AS_THEN_END ( NULL )
  "Group {{m1}} : {{m3}}" FORMAT 
END_APPLY_EACH 
REDUCE_LIST IMPLODE '; '
  1. Lists the groups a user is in
  2. Applies the following to each element in the list returned by (1)
  3. Get the group name and save it to a memory
  4. Start a parenthesized expression, so we can end it early later
  5. User again, gets the exit time to leave the previously remembered group
  6. Save the unformatted exit time to a memory
  7. Make a nice interpretation of the forever value and quit the parenthesized expression started at 4.
  8. Still in the parenthesized expression, nicely format the group exit time.
  9. At the end of the parenthesized expression, save the result to a memory.
  10. Using the previously saved unformatted exit time, if it is zero (not in group), then we end that loop of APPLY_EACH with a null value.
  11. We have good data, so format it nicely.
  12. Marks the end of APPLY_EACH
  13. Removes null values from the resulting list and joins them together with '; ' between each.

The complexity comes mainly from concrete5 being inconsistent about the groups 'Registered Users' and 'Guest'. When a user's groups are listed, those groups are returned. However, when testing for membership, those group tests fail.

Having tested and developed from any of the above using the symbol tester, to use the expression in a content block you will need to reformat to just one line within the [% ... %].

In an html block you can keep the lines to make it easier to read/maintain.

Last updated: over a year ago