tags:

views:

2745

answers:

4

Hi,

I am working with a Drupal 6.x system to create exercise / personal training programmes, and am using the CCK with content types of Exercise and Programme, where Programme contains a few header fields and a list of node references to the exercises it consists of. This works great and I can manually create programmes which work fine. I now wish to create a module which can generate these programs automatically based on a number of algorithms I have developed, the process will look like:

  1. Load all exercises into array
  2. Load users personal info (entered previously)
  3. Establish best suited exercises
  4. Create new programme content type
  5. Save programme

An Exercise has a number of related attributes and although I could do all of the above using SQL directly into the tables it would be quite complex and doesn't feel right. I would like in step 1 to load the exercises as an array of Exercise objects (node_load?), and then create a programme object and save. Is this OO type approach possible or do I have to resort to manipulating the data directly?

Thanks

+5  A: 

The best way to tackle this problem would be to write your own module to do this.

Step 1 you can do node_load($nid) on all the excercies Step 2 you can use user_load($uid) Step 3 you'll need to iterate through the user object and match up to the appropriate excercies. Step 4/5 I'd create a new $node = stdClass(); object and populate the attributes with the correct data then perfrom a node_save($node); this will assign it a $node->id etc.

If your unsure on what attributes are in your training program node, then do a print_r($node); on one you've created already.

Phil

Phil Carter
I'm planning on writing a module for this - how does node_save work? will this just save in the node table or will it actually create the entries for the programme as well?
Macros
node_save works on a node object and saves it in the node and node_revisions tables, and then all the other module hooks get invoked on it.
Phil Carter
+1  A: 

Drupal doesn't provide any "cascading" save mechanism to save a hierarchy of nodes in one swoop. You'll need to build each node in code and call node_save() on it explicitly as described by Phil Carter.

The Node Export module can generate sample code for creating a node (complete with CCK fields) programatically.

Eaton
A: 

There are a ton of modules that try to do the import thing right. Take a look at a comparison of them http://groups.drupal.org/node/21338

If you need strategies or examples of importing stuff into nodes, those are a great resource.

greggles
A: 

I don't think "creating" these programs is even necessary.

Why not just display a list of exercises that match your requirements and share 'characteristics' with the user.

I'd accomplish this by making the 'characteristics' be taxonomy. Then attached to users either with a profile, or taxonomy flag. Then display a list (perhaps even a view at first) and those with the same characteristic tags within exercises.

This would be dynamic and user specific and note require pre-loading a bunch of programmes.

A thought.

Initially we were going down this route however there is a lot of user data taken into account, and the programmmes were not nearly as tailored as they should be. The alogorithm that establishes the best exercises in step 3 is pretty now complex
Macros
Yeah, I figured that was the answer. But with the right query you could load all the potential exercise matches, then narrow it down father by organizing the remaining results, and displaying that list.Just a vote for not needing a node factory.

related questions