function student_priority_get_calculations_for_student

6.x student_priority.module student_priority_get_calculations_for_student($student_cwid, $bool_save_to_student = FALSE)

Run all of the calculations for a student, and return back the results in a formatted array.

If bool_save_to_student is true, then we will write the results to the student_priority.

2 calls to student_priority_get_calculations_for_student()
student_priority_display_priority_calculations_page in modules/student_priority/student_priority.module
Displays the calculations used to get the priority values. Can be displayed in either the dialog or stand-alone.
student_priority_get_academic_priority_value in modules/student_priority/student_priority.module
Queries for the academic priority value, and if its older than the set number of seconds, we will re-calculate for this student. Set to zero to force it to recalculate every time.

File

modules/student_priority/student_priority.module, line 264

Code

function student_priority_get_calculations_for_student($student_cwid, $bool_save_to_student = FALSE) {

  global $current_student_id, $user, $student;

  if (!isset($student) || $student == null || !is_object($student)) {
    $student = new Student($current_student_id);
  }

  $rtn = array();

  //load student as object for use in the calculation tests.
  $use_student = $student;
  if ($student_cwid != $current_student_id) {
    $use_student = new Student($student_cwid);
  }

  $student_user_id = db_get_user_id_from_cwid($use_student->student_id, 'student');

  $calc_test_results = array();
  $total = 0;
  $max_possible = 0;
  $count = 0;

  // Invoke a hook to run our priority calculations.  
  // First, get all our tests from all modules.
  $calc_tests = array();
  invoke_hook('define_calculation_tests', array(&$calc_tests));


  /////////////////////////////////
  // sort results by weight, title.
  /////////////////////////////////  
  $sorted_calc_tests = array();
  $lines = array();
  foreach ($calc_tests as $key => $value) {
    $lines [] = str_pad($value ['weight'], 5, '0', STR_PAD_LEFT) . " ~~~ " . $value ['title'] . " ~~~ " . $key;
  }
  sort($lines);
  foreach ($lines as $line) {
    $temp = explode("~~~", $line);
    $key = trim($temp [2]);
    $sorted_calc_tests [$key] = $calc_tests [$key];
  }
  $calc_tests = $sorted_calc_tests;
  //////////////////////////


  $rtn ['calc_tests'] = $calc_tests;

  foreach ($calc_tests as $callback => $details) {

    $file = @$details ['file']; // 0 = module, 1 = filename    
    if ($file) {
      $x = include_once(fp_get_module_path($file [0], TRUE, FALSE) . "/" . $file [1]);
      if (!$x) {
        fpm('Could not load include file:' . fp_get_module_path($file [0], TRUE, FALSE) . "/" . $file [1]);
      }


      // Now execute the callback.
      $calc_test_results [$callback] = call_user_func_array($callback, array($use_student));
    }

    // Are we instead looking for a user attribute?
    if (isset($details ['result_from_user_attribute'])) {
      $val = user_get_attribute($student_user_id, $details ['result_from_user_attribute']);
      if ($val == '') {
        $val = 'SKIP';
      }
      $calc_test_results [$callback]['result'] = $val;
      $calc_test_results [$callback]['result_label'] = $val;

      // If we have the original options we can use for the label, use that.
      //fpm($details);
      if (isset($details ['options'][$val])) {
        $calc_test_results [$callback]['result_label'] = $details ['options'][$val];
      }

    }


    $raw_score = $details ['result_scores'][$calc_test_results [$callback]['result']];

    // If the raw_score is "SKIP" then we don't want to count this at all.

    if (is_numeric($raw_score)) {
      $score = floatval($raw_score);
      $calc_test_results [$callback]['score'] = $score;

      // Figure out the max possible score from this test.
      $test_max_score = 0;
      foreach ($details ['result_scores'] as $k => $v) {
        $v = floatval($v);
        if ($v > $test_max_score) {
          $test_max_score = $v;
        }
      }

      $max_possible += $test_max_score;

      $total = $total + $score;
      $count++;
    }
    else {
      $calc_test_results [$callback]['score'] = $raw_score;
    }

  } // foreach

  $avg = 0;
  if ($count > 0) {
    $avg = round($total / $count, 2);
  }

  $percent = 0;
  if ($max_possible > 0) {
    $percent = round(($total / $max_possible * 100), 2);
  }


  $rtn ['calc_test_results'] = $calc_test_results;
  $rtn ['count'] = $count;
  $rtn ['total'] = $total;
  $rtn ['avg'] = $avg;
  $rtn ['max_possible'] = $max_possible;
  $rtn ['percent'] = $percent;


  // Should we save the value to the student_priority table?  
  if ($bool_save_to_student) {
    db_query('REPLACE INTO student_priority (student_id, priority_value, results, updated)
                VALUES (?, ?, ?, ?) ', array($student_cwid, $percent, serialize($rtn), time()));
  }

  return $rtn;


}