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

djdevin’s picture

Status: Active » Needs review
StatusFileSize
new1.24 KB

Thanks, 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.

djdevin’s picture

Whoops, typo

deggertsen’s picture

Beautiful! I'm excited to test this out. I will hopefully have some time to do so tomorrow.

djdevin’s picture

Cool, if you could mark RTBC I'll commit it.

deggertsen’s picture

It 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.

Darren Shelley’s picture

I agree with deggertson that a condition would be handy.

I have reviewed #5 and made some improvements:

  • Amended key of condition and added base deceleration to put the naming conventions inline with those used in djdevins actions
  • Removed trailing whitespace from line 16 of previous patch
  • Fixed condition so that a boolean value is returned

Patch attached, Ready for review.

deggertsen’s picture

Status: Needs review » Reviewed & tested by the community

@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.

cutmedia’s picture

EDIT:
works, patch not cleanly applied...

Darren Shelley’s picture

@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:

  • git apply -v course-rules-enroll-unenroll-2010002-6.patch
  • patch -p1 < course-rules-enroll-unenroll-2010002-6.patch

Are you applying against a clean checkout of 7.x-1.x-dev?

Thankyou

cutmedia’s picture

works on 7.x-1x-dev, I applied to wrong wrong version of module, my bad
+1 RTBC

djdevin’s picture

Status: Reviewed & tested by the community » Fixed

Thanks! 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.

deggertsen’s picture

Awesome! Great work everyone!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

  • Commit 083c134 on 7.x-1.x, 7.x-1.x-fieldable-entities, course-object-entity-view by djdevin:
    Issue #2010002 by deggertsen, Darren Shelley: Added rules support for...