You are here

function course_generate in Course 8.3

Same name and namespace in other branches
  1. 8.2 course.devel.inc \course_generate()
  2. 7.2 course.devel.inc \course_generate()
  3. 7 course.devel.inc \course_generate()
  4. 3.x course.devel.inc \course_generate()

Generate random course data.

@todo associate results with random users

Parameters

type $course_type:

type $question_types:

type $question_limit:

type $course_results:

1 call to course_generate()
course_generate_form_submit in ./course.devel.inc
Submit callback for devel_generate support.

File

./course.devel.inc, line 94

Code

function course_generate($course_type, $course_object_types, $course_object_limit, $course_enrollments) {
  $users = array_filter(devel_get_users());
  $node = (object) array(
    'type' => $course_type,
    'title' => devel_create_greeking(rand(5, 10), TRUE),
    'uid' => $users[array_rand($users)],
    'created' => REQUEST_TIME,
    'changed' => REQUEST_TIME,
  );

  // Populate all core fields on behalf of field.module
  module_load_include('inc', 'devel_generate', 'devel_generate.fields');
  devel_generate_fields($node, 'node', $node->type);
  unset($node->og_group_ref);
  node_save($node);
  for ($i = 1; $i <= $course_object_limit; $i++) {
    course_generate_objects($node, array_rand($course_object_types));
  }
  for ($i = 1; $i <= $course_enrollments; $i++) {
    if ($uid = array_pop($users)) {
      $course_enrollment = entity_create('course_enrollment', array(
        'type' => 'course_enrollment',
        'nid' => $node
          ->id(),
        'uid' => $uid,
      ));
      $course_enrollment
        ->save();
    }
    else {
      drupal_set_message("Did not have enough users. Could only create {$i} enrollments.");
      break;
    }
  }
  drupal_set_message('Generated course: ' . l($node->title, 'node/' . $node
    ->id()));
}