Created
December 1, 2014 18:17
-
-
Save jonathanfranks/467d054fea3242085522 to your computer and use it in GitHub Desktop.
Adding a node as a step to a Course object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Adds a new step to a course | |
* @param $course_id NID of the Course Node to add the step to | |
* @param $step_node_id NID of the node to add as a step | |
*/ | |
function my_module_add_existing_step_to_course($course_id, $step_node_id) { | |
$node = node_load($course_id); | |
$course = course_get_course($node); | |
$new_weight = count($course->getObjects()); | |
global $user; | |
$node_to_add = node_load($step_node_id); | |
$node_type = $node_to_add->type; | |
$module_types = array( | |
'course_module' => 'course_content', | |
'quiz' => 'course_quiz', | |
); | |
$module_type = $module_types[$node_type]; | |
$newObject = course_get_course_object($module_type, $node_type); | |
$newObject->setUser($user); | |
$newObject->setCourse($course); | |
$newObject->setCourse($node->nid); | |
$newObject->setModule($module_type); | |
$newObject->setComponent($node_type); | |
$newObject->setInstanceId($node_to_add->nid); | |
$newObject->setOption('title', $node_to_add->title); | |
$newObject->setOption('required', TRUE); | |
$newObject->setOption('enabled', TRUE); | |
$newObject->setOption('hidden', FALSE); | |
$newObject->setOption('weight', $new_weight); | |
$newObject->setOption( | |
'plugins', array( | |
'access' => | |
array( | |
'conditional' => | |
array( | |
'conditional_type' => 'completed', | |
'conditional_time' => 0, | |
'conditional_object' => $step_node_id, | |
'conditional_hidden' => 0, | |
) | |
) | |
) | |
); | |
$newObject->save(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment