You are here

static function CourseObjectAttendance::generateWord in Course 3.x

Generate an attendance code that isn't used.

1 call to CourseObjectAttendance::generateWord()
CourseObjectAttendance::preSave in modules/course_attendance/src/Plugin/course/CourseObject/CourseObjectAttendance.php
Generate an attendance code if not provided.

File

modules/course_attendance/src/Plugin/course/CourseObject/CourseObjectAttendance.php, line 157

Class

CourseObjectAttendance
Plugin annotation @CourseObject( id = "attendance", label = "Attendance", )

Namespace

Drupal\course_attendance\Plugin\course\CourseObject

Code

static function generateWord() {

  // Distinguishable characters.
  $accepted_cons_start = "BCDFGHJKLMNPQRSTVWYZ";
  $accepted_cons_end = "BCDFGHKLMNPQRSTVWXYZ";
  $accepted_vowels = "AEUO";

  // Gen word, then check
  do {
    $word = '';
    for ($i = 1; $i <= 6; $i++) {
      switch ($i) {
        case 1:
        case 4:
          $word .= $accepted_cons_start[rand(0, strlen($accepted_cons_start) - 1)];
          break;
        case 3:
        case 6:
          $word .= $accepted_cons_end[rand(0, strlen($accepted_cons_end) - 1)];
          break;
        case 2:
        case 5:
          $word .= $accepted_vowels[rand(0, strlen($accepted_vowels) - 1)];
          break;
      }
    }
    \Drupal::moduleHandler()
      ->alter('course_attendance_word', $word);
  } while (self::findObjectByCode($word));
  return $word;
}