I would like to be able to use rules in order to enroll a user in a course. This could provide for a large number of use cases including integration with Drupal Commerce. Here is some initial code for these rules actions. I haven't yet tested it and I doubt it's fully functional at this point, but it's a start. I will try to post a patch if I test it and can get it working.
<?php
function course_rules_action_info() {
$defaults = array(
'parameter' => array(
'user' => array(
'type' => 'user',
'label' => t('User'),
'save' => TRUE,
),
'node' => array(
'type' => 'node',
'label' => 'Course Node',
'save' => TRUE,
),
),
'group' => t('Course'),
);
$actions['user_enroll'] = $defaults + array(
'label' => t('Enroll a user'),
'base' => 'course_enroll',
);
$actions['user_unenroll'] = $defaults + array(
'label' => t('Unenroll a user'),
'base' => 'course_unenroll',
);
return $actions;
}
?>
See http://drupalcontrib.org/api/drupal/contributions!rules!rules.api.php/fu...
Comments
Comment #1
djdevinThanks, I took what you did, added two rules functions and made it into a patch.
With this you should be able to create a rule that enrolls/unenrolls a user as long as there is a node and user present.
Comment #2
djdevinWhoops, typo
Comment #3
deggertsen commentedBeautiful! I'm excited to test this out. I will hopefully have some time to do so tomorrow.
Comment #4
djdevinCool, if you could mark RTBC I'll commit it.
Comment #5
deggertsen commentedIt works! However, it leaves the enroll start date empty. Not sure if that needs to be fixed though.
I did add a condition as well to check if a user is enrolled in the course (can be negated to see if the user is not already enrolled). I have tested this and it is working just fine.
Leaving as needs review as I added the condition that should probably be reviewed before being committed.
Comment #6
Darren Shelley commentedI agree with deggertson that a condition would be handy.
I have reviewed #5 and made some improvements:
Patch attached, Ready for review.
Comment #7
deggertsen commented@Darren Shelley. I've added the changes you made to my site and will report back if I have any problems. If you don't hear back from me then it can be assumed that it works! =)
I'm going to mark it RTBC as I think it is now ready to commit.
Comment #8
cutmedia commentedEDIT:
works, patch not cleanly applied...
Comment #9
Darren Shelley commented@cutmedia that's odd I can't replicate the patch issue, how are you applying the patch?
The following two methods apply the patch cleanly for me:
Are you applying against a clean checkout of 7.x-1.x-dev?
Thankyou
Comment #10
cutmedia commentedworks on 7.x-1x-dev, I applied to wrong wrong version of module, my bad
+1 RTBC
Comment #11
djdevinThanks! Looks good. Committed to 7.x-1.x 083c134
I changed the label from "Course" to "Node" as it was "Node" in the original action.
Comment #12
deggertsen commentedAwesome! Great work everyone!