function course_generate in Course 3.x
Same name and namespace in other branches
- 8.3 course.devel.inc \course_generate()
- 8.2 course.devel.inc \course_generate()
- 7.2 course.devel.inc \course_generate()
- 7 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' => \Drupal::time()
->getRequestTime(),
'changed' => \Drupal::time()
->getRequestTime(),
);
// 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 = \Drupal::service('entity_type.manager')
->getStorage('course_enrollment')
->create(array(
'type' => 'course_enrollment',
'cid' => $node
->id(),
'uid' => $uid,
));
$course_enrollment
->save();
}
else {
\Drupal::messenger()
->addStatus("Did not have enough users. Could only create {$i} enrollments.");
break;
}
}
\Drupal::messenger()
->addStatus('Generated course: ' . l($node->title, 'node/' . $node
->id()));
}