Best way to store data

Hi all,

I'm in the process of further developing the plugin I mentioned before with Purus. I'm working on the statistical data analysis for it and have hit a wall so to speak.

At the moment, we have a relationship between a user and an entity to define which entity they are currently interacting with. Once they have finished that interaction that relationship gets removed and another one replaces it to say that the user has "completed" their interaction with that entity.

What I'm trying to do is store some data to show as statistics. Basically I want to get a 3 month spread (last 3 months) of the number of users who have "started" interaction with the entity and who have "completed" the entity, on a month by month basis from the point of view of the entity.

This in itself isn't hard to do except of course the issue being that once a user has "completed" their interaction the relationship for the "starting" it is removed. 

This plugin has a lot of annotations and metadata attached to it already, so I'm deeply conscious of the need to reduce that number as much as possible. 

I'm trying to think of a clean solution that would solve this problem. This is what comes to mind first, can anyone shed any light on a better solution:


I would save 3 annotations on the entity. Then each time a user "starts" interacting or "completes" their interaction, I would update the current month's annotation. Then at the end of each month, run a cron-job to move the current month's annotation value to the previous month and so on, bumping the oldest month's value off.

Does this seem like the best solution or would there be a better way?

Any help, thoughts, much appreciated


  • " Save 3 annotations on the entity.." - you only listed 2 ?
    All this preoccupation with elgg's 'relationship' mechanism ?

                   |  mnth-2  |  mnth-1  | mnth-curr |
    Dec            |   AAA    |   ...    |   ...     |
    Nov            |   BBB    |   AAA    |   ...     |
    Oct            |   CCC    |   BBB    |   AAA     |

    Only needs *one metadata field -
    serialized JSON
             "MonthCurr0": "A"
            ,"MonthLast1": "B"
            ,"MonthLast2": ""

    array - unserialize/serialized :=
    -> shuffle
    -> store as one metadata

    CRON ?
    Why waste resources ?
    When the updates scan be done 'on-demand'


  • I see. So make a JSON array and save it as metadata. Then each time a number needs to be increased by one (new interaction) simply find the correct "month" element in the array increase its number then resave as JSON array again?

    That makes sense. Thanks for the thought D it's given me a new level of dev to think about. :)

  • MetaData Naming:=
    String ? :=  "#MyMonthLast1" . CurrCtrs" := "#MonthCurr" .'|'.  "#MonthLast1" .'|'.   "#MonthLast2"   (hanging via not even JSON;)
            0                    1                    -2
    "#MyMonthCurr0" & "#MyMonthLast1" & "#MyMonthLast2"

    Kinda depends on what kind of search you might later want to do via_metadata_by_name
    still option #2 can be searched using
    metadata string like "%MyMonth%"
    which will fetch all the 3 months' counters;
    .. can then do whatever you like.

  • Ok, I've made some progress in that I've saved metadata as a JSON encoded array. It's setup along the following lines:  monthyear => value (122011 => 1).

    I'm a bit stuck on incrementing them though. My code plus var_dump is below:

    $starter_array = json_decode($course->starters, true);

    $month = date("no");


    foreach($starter_array as $date){




    $starter_array[$month] = 1;







    var_dump is as follows:

    array(1) { [122011]=> int(2) } string(12) "{"122011":1}"  

    So it is decoding the json correctly, updating the relevant variable, but can't seem to get it to then resave the json array into the metadata. Any hints?

  • dont u mean $course->StarterJsonStringOrWhatever =  json_encode($starter_array) ? or just use serialize on $starter_array..thats all ! u better get that working - i've gtg backup/ rst/ upgrade a whole site ;-oO


  • Solved it. The update wasn't working because the session user didn't have permission to update the metadata.

    Seems to be working now. 

    I used json_encode/json_decode for this.

  • oki.. i'm on herbal-tea-brk ;-O but u = do have a look at php (un)serialize cmnds sometime..;-o