<?php 

// deprecating
function compile_mock_actn($args) {
  $post = new post;
  $session = Session::getInstance();
  ignore_user_abort(true);
  ini_set('max_execution_time', 300);
  $students = get_class_students(['class_id' => $args['class'], 'category_id' => $args['category']]);
  
  $std_count = _count($students);
  $use_pin = get_option('use_pins_to_access_results');
  if($std_count) {
    if($args['category'] == 'all') {
      $class_courses = get_extended_course_mapping($args['class']);
      $results = $post->get_post_by_meta_data(['type' => 'mock_exam', 'exam' => $args['exam'], 'class' => $args['class'], 'session' => $args['session'], 'term' => $args['term']]);
      if(_count($results)) {
        foreach($results as $key => $result_meta) {
          if(!in_array($result_meta['course'], $class_courses)) {
            unset($results[$key]);
          }
        }
      }
    } else {
      $results = $post->get_post_by_meta_data(['type' => 'mock_exam', 'exam' => $args['exam'], 'class' => $args['class'], 'category' => $args['category'], 'session' => $args['session'], 'term' => $args['term']]);
      $course_mapping = get_option("course_mapping");
      if(isset($course_mapping[$args['class']][$args['category']]) && _count($results)) {
        foreach($results as $key => $result_meta) {
          if(!in_array($result_meta['course'], $course_mapping[$args['class']][$args['category']])) {
            unset($results[$key]);
          }
        }
      }
    }
    
    $results = prepare_mock_result($results);
    foreach ($students as $key => $value) {
      compile_mock_result($value['id'], $args['session'], $args['term'], $args['class'], $args['category'], $std_count, $results[$value['id']], $args['exam'], $use_pin);
    }
    $info = get_class_name($args['class'], $args['category'])." ".get_term_name($args['term'])." ".get_session_name($args['session'])." mock ".$args['exam']." result was compiled successfully";
    inform_admins($info, "RESULT COMPILATION", get_option("home_url"), "software_engineer", "super_admin");
    admin_log($info);
    $session->message = get_class_name($args['class'], $args['category'])." ".$args['exam']." mock results compiled successfully";
    return;
  }
  $session->message = "No student in category";
}

function extexam_compile_mock_results($args) {
  $post = new post;
  $user = new user;
  $session = Session::getInstance();
  $students = [];
  $term_meta = $post->get_post_meta($args['term']);
  $args['category'] = ($args['category'] == "") ? false : $args['category'];
  $class_courses = get_class_courses($args['class'], $args['category'], false, true, true);
  $broadsheet = extexam_get_broadsheet($args['class'], $args['category'], $args['term'], $args['ext_exam'], $class_courses);
  $std_count = _count($broadsheet); // Number of students in the class;
  $status = get_option('use_pins_to_access_results') ? "locked" : "unlocked";
  $principal = get_option('school_principal'); //principal
  $form_teacher = []; // form teacher
  $last_compiled = time();
  foreach ($broadsheet as $key => $value) {
    set_time_limit(60);
    $tm_obtainable = 100*$value['subjects_offered'];
    $student_meta = $user->get_user_meta($value['student_id'], "username");
    $category = (!$args['category']) ? $student_meta['category'] : $args['category'];
    if(!isset($form_teacher[$category])) {
      $form_teacher[$category] = get_form_teacher($args['class'], $category);
    }
    $attendance = analyse_individual_attendance($args['class'], $category, $args['term'], $student_meta['id']);
    $compilation = [
      "attendance"      => base64_encode(serialize($attendance)),
      "type"            => "compiled_mock_result",
      "term"            => $args["term"],
      "term_name"       => $term_meta['term_name'],
      "student_id"      => $student_meta['id'],
      "session"         => $term_meta['acsession'],
      "class"           => $args['class'],
      "category"        => $category,
      "ft_comment"      => $value['comment'],
      "psychometrics"   => $student_meta['psychometrics'] ?? '',
      "position"        => $value['position'],
      "picture"         => get_user_image($student_meta),
      "std_count"       => $std_count,
      "principal"       => $principal,
      "form_teacher"    => $form_teacher[$category],
      "last_compiled"   => $last_compiled,
      "course_count"    => $value['subjects_offered'],
      "tm_obtainable"   => $tm_obtainable,
      "marks_obtained"  => $value['total'],
      "courses"         => base64_encode(serialize($value['courses'] ?? array())),
      'exam'            => $args['ext_exam'],
      "status"          => $status
    ];
    $existing = db_post_unique(['type' => 'compiled_mock_result', 'exam' => $args['ext_exam'], 'term' => $args['term'], 'class' => $args['class'], 'student_id' => $student_meta['id']]);
    if($existing) {
      unset($compilation["status"]);
      $post->update_post_meta($existing['id'], $compilation);
    } else {
      $students[] = $student_meta;
      $id = $post->add_post($compilation);
    }
    continue;
  }
  // notify students of their results
  $info = "Your ".get_class_name($args['class'], $args['category'])." ".$term_meta['term_name']." ".$term_meta['acsession']." result is ready and available for printing from the portal.";
  inform_users($students, $info, "Your Result Is Ready", false, EMAIL, NOTIFICATION);

  // notify admins of result compilation
  $info = get_class_name($args['class'], $args['category'])." ".$term_meta['term_name']." ".$term_meta['acsession']." result is ready, compiled and available for printing from the portal. Compilation was done by ".get_fullname($session->id);
  inform_admins($info, "Result Compilation", false, "software_engineer");
  return ['status' => true, 'msg' => 'Done'];
}

function eedu_portal_refresh_mock_broadsheet($args) {
  ext_exam_reset_broadsheet($args['class'], $args['category'], $args['term'], $args['ext_exam']);
  return ["msg" => "Done", "status" => true];
}