You are here

function opigno_statistics_app_migrate_data in Opigno Statistics App 7

Migrate opigno data to opigno statistics

1 call to opigno_statistics_app_migrate_data()
opigno_statistics_app_install in ./opigno_statistics_app.install
Implements hook_install()

File

./opigno_statistics_app.install, line 27
Contains install instructions and logic

Code

function opigno_statistics_app_migrate_data() {
  $users = entity_load('user');
  $courses = entity_load('node', FALSE, array(
    'type' => 'course',
  ));
  $classes = entity_load('node', FALSE, array(
    'type' => 'class',
  ));
  $month_year_now = mktime(0, 0, 0, date('m', time()), 1);
  variable_set('opigno_statistics_module_installation_date_timestamp', $month_year_now);
  foreach ($courses as $course) {
    db_insert('opigno_statistics_group')
      ->fields(array(
      'group_nid' => $course->nid,
      'month_year' => $month_year_now,
      'category_taxonomy_term_name' => 'NONE',
      'group_title' => $course->title,
      'group_type' => 'course',
    ))
      ->execute();
  }
  foreach ($users as $user) {
    if ($user->uid == 0) {
      continue;
    }
    foreach ($classes as $class) {
      $result = db_query('SELECT 1 FROM {og_membership} ogm
              INNER JOIN {node} n
              ON ogm.gid = n.nid
              WHERE etid = :uid
              AND gid = :class_nid
              AND entity_type = :user_type
              AND n.type = :node_type', array(
        ':class_nid' => $class->nid,
        ':uid' => $user->uid,
        ':user_type' => 'user',
        ':node_type' => 'class',
      ))
        ->fetchCol();
      $is_part_of_class = !empty($result);
      if ($is_part_of_class) {
        db_insert('opigno_statistics_user_group')
          ->fields(array(
          'uid' => $user->uid,
          'group_nid' => $class->nid,
          'nid' => $class->nid,
        ))
          ->execute();
      }
    }
    foreach ($courses as $course) {
      $quiz_nids = opigno_quiz_app_course_lessons($course->nid);
      $quiz_nids = array_keys($quiz_nids[$course->nid]);
      $user_quiz_scores = quiz_get_score_data($quiz_nids, $user->uid);

      //check if user is part of course
      $result = db_query('SELECT 1 FROM {og_membership} ogm
              INNER JOIN {node} n
              ON ogm.gid = n.nid
              WHERE etid = :uid
              AND gid = :course_nid
              AND entity_type = :user_type
              AND n.type = :node_type', array(
        ':course_nid' => $course->nid,
        ':uid' => $user->uid,
        ':user_type' => 'user',
        ':node_type' => 'course',
      ))
        ->fetchCol();
      $is_part_of_course = !empty($result);
      if ($is_part_of_course) {

        //Fill opigno_statistics_user_group table
        db_insert('opigno_statistics_user_group')
          ->fields(array(
          'uid' => $user->uid,
          'group_nid' => $course->nid,
          'nid' => $course->nid,
        ))
          ->execute();
        if (count($user_quiz_scores) > 0) {

          //Fill opigno_statistics_user_course table
          $passed = (int) opigno_og_prereq_user_passed_course($user->uid, $course);
          $user_course_nid = db_insert('opigno_statistics_user_course')
            ->fields(array(
            'uid' => $user->uid,
            'course_nid' => $course->nid,
            'status' => $passed,
            'course_name' => $course->title,
            'score' => 0,
            //Will be updated after course_details is filled
            'username' => $user->name,
          ))
            ->execute();
          if ($passed) {
            db_merge('opigno_statistics_group')
              ->key(array(
              'group_nid' => $course->nid,
              'month_year' => $month_year_now,
              'category_taxonomy_term_name' => 'NONE',
              'group_title' => $course->title,
              'group_type' => 'course',
            ))
              ->fields(array())
              ->expression('number_passed', 'number_passed + 1')
              ->execute();
          }
          foreach ($user_quiz_scores as $user_quiz_score) {
            if (isset($user_quiz_score->percent_score)) {
              $quiz = node_load($user_quiz_score->nid);
              if ($quiz) {

                //quiz not deleted
                db_insert('opigno_statistics_user_course_details')
                  ->fields(array(
                  'opigno_statistics_user_course_fk' => $user_course_nid,
                  'type' => $quiz->type,
                  'entity_id' => $quiz->nid,
                  'entity_name' => $quiz->title,
                  'score' => (int) $user_quiz_score->percent_score,
                  'timestamp' => time(),
                  //cannot retrieve when user finished the quiz because it wasn't not stored in the database until now
                  'status' => (int) ((int) $user_quiz_score->percent_score >= (int) $user_quiz_score->percent_pass),
                  'required' => opigno_statistics_app_is_lesson_required_in_course($quiz, $course),
                ))
                  ->execute();
              }
            }
          }
          opigno_statistics_app_query_user_course_update_score($user_course_nid);
        }
      }
    }
  }
}