reports.major-students-progress.inc

  1. 7.x modules/stats/reports.major-students-progress.inc
  2. 6.x modules/stats/reports.major-students-progress.inc

This file handles the more complicated Major Students Progress csv export report, since it is part of a batch process.

File

modules/stats/reports.major-students-progress.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * This file handles the more complicated Major Students Progress csv export report, since
  5. * it is part of a batch process.
  6. */
  7. /**
  8. * This report will let the user select a major, then see a report of students
  9. * in that major, and what percentages they have let to complete, as well as other
  10. * data which may be useful.
  11. *
  12. */
  13. /**
  14. * Simply display a form letting the user select an available major.
  15. *
  16. */
  17. function stats_report_major_students_progress_form() {
  18. $form = array();
  19. $school_id = intval(@$_REQUEST['school_id']);
  20. $school_select_title = "Major Students Progress - Select School";
  21. // If schools is enabled, first ask for the school.
  22. if (module_enabled("schools") && !isset($_REQUEST['school_id'])) {
  23. fp_goto("stats/select-school", "title=$school_select_title&redirect=stats/reports/major-students-progress");
  24. return;
  25. }
  26. else if (module_enabled('schools')) {
  27. // Display what school we selected, link to change.
  28. $form['selected_school_name'] = array(
  29. 'value' => "<div class='current-school'>" . t("Current school: ") . "<strong>" . schools_get_school_name_for_id($school_id) . "</strong>
  30. - " . l(t("Change?"), "stats/select-school", "title=$school_select_title&redirect=stats/reports/major-students-progress") . "</div>",
  31. );
  32. }
  33. $form['school_id'] = array(
  34. 'type' => 'hidden',
  35. 'value' => $school_id,
  36. );
  37. $last_major = @$_SESSION["stats_report_major_students_progress"][$school_id]["last_major"];
  38. $form["major"] = array(
  39. "label" => t("Please select a major:"),
  40. "type" => "select",
  41. "hide_please_select" => TRUE,
  42. "options" => stats_draw_majors_pulldown($last_major, FALSE, FALSE, t("Please select a major"), TRUE, FALSE, TRUE, $school_id),
  43. "value" => $last_major,
  44. );
  45. $form["submit_btn"] = array(
  46. "type" => "submit",
  47. "spinner" => TRUE,
  48. "value" => t("Submit and generate CSV"),
  49. );
  50. // If we have previously generated a CSV within the last hour, display it here.
  51. $last_timestamp = @$_SESSION["stats_report_major_students_progress"][$school_id]["last_timestamp"];
  52. $last_batch_id = @$_SESSION["stats_report_major_students_progress"][$school_id]["last_batch_id"];
  53. $html = "";
  54. $m = 0;
  55. if (time() < $last_timestamp + (60 * 60)) {
  56. $html .= "<hr><p>" . t("Last CSV report was generated for @major on @time. It will remain for 1 hour.", array("@major" => $last_major, "@time" => format_date(convert_time($last_timestamp)))) . "</p>";
  57. $html .= "<p>" . l(t("Download CSV for ") . $last_major, "stats/download-csv-from-batch/$last_batch_id", "filename=" . urlencode("major_student_progress__" . $last_major)) . "</p>";
  58. $form["mark" . $m++] = array(
  59. "value" => $html,
  60. );
  61. }
  62. // If the Batch module isn't enabled, stop right here!!!
  63. if (!function_exists("batch_set")) {
  64. $form = array();
  65. $form["markup"] = array(
  66. "value" => "<p><b>" . t("To use this report, the Batch module must be enabled. Please ask your administrator
  67. to enable the Batch module.") . "</b></p>",
  68. );
  69. }
  70. return $form;
  71. }
  72. /**
  73. * Our submit handler. We will be setting up a new batch process.
  74. *
  75. * @param array $form
  76. * @param array $form_state
  77. */
  78. function stats_report_major_students_progress_form_submit($form, $form_state) {
  79. $major = $form_state["values"]["major"];
  80. $school_id = intval($form_state['values']['school_id']);
  81. // Okay, set up the batch....
  82. $batch = array(
  83. "operation" => array("stats_report_major_students_progress_perform_batch_operation", array($major, $school_id)),
  84. "title" => t("Major Students Progress - CSV Export - Major: ") . $major,
  85. "file" => menu_get_module_path("stats") . "/reports.major-students-progress.inc",
  86. "progress_message" => "Processing student @current of @total",
  87. "display_percent" => TRUE,
  88. );
  89. watchdog('stats', "report_major_students_progress major:$major, school_id:$school_id", array());
  90. // Set the batch...
  91. batch_set($batch);
  92. }
  93. /**
  94. * This is the actual batch process function which gets called per run of the batch.
  95. *
  96. * @param array $batch
  97. * @param string $major
  98. */
  99. function stats_report_major_students_progress_perform_batch_operation(&$batch, $major, $school_id = 0) {
  100. // For later, let's get our requirement types from the pie chart config...
  101. $requirement_types = array();
  102. // Get the requested piecharts from our config...
  103. $temp = variable_get_for_school("pie_chart_config", "c ~ Core Requirements\nm ~ Major Requirements\ndegree ~ Degree Progress", $school_id);
  104. $lines = explode("\n", $temp);
  105. foreach ($lines as $line) {
  106. if (trim($line) == "") continue;
  107. $temp = explode("~", $line);
  108. $requirement_type = trim($temp[0]);
  109. $label = trim($temp[1]);
  110. $requirement_types[$requirement_type] = $label;
  111. }
  112. //////////////////////////// FIRST TIME ///////////////////////////////
  113. // If this is our first time through, let's init our values.
  114. if (!isset($batch["results"]["total"])) {
  115. // Our first time through. Let's start up.
  116. $batch["results"]["current"] = 0;
  117. $batch["results"]["finished"] = FALSE;
  118. $batch["csv"] = "CWID, LAST, FIRST, CATALOG, MAJOR, RANK, ";
  119. // Get the requested piecharts from our config...
  120. $temp = variable_get_for_school("pie_chart_config", "c ~ Core Requirements\nm ~ Major Requirements\ndegree ~ Degree Progress", $school_id);
  121. $lines = explode("\n", $temp);
  122. foreach ($lines as $line) {
  123. if (trim($line) == "") continue;
  124. $temp = explode("~", $line);
  125. $requirement_type = trim($temp[0]);
  126. $label = trim($temp[1]);
  127. $requirement_types[$requirement_type] = $label;
  128. $batch["csv"] .= "$requirement_type FULFILLED, $requirement_type TOTAL, $requirement_type PERCENT, $requirement_type GPA, ";
  129. }
  130. $batch["csv"] .= "\n";
  131. // We need the "total" number of students in the selected major.
  132. $res = db_query("SELECT COUNT(*) as count FROM students a, users b, student_degrees c
  133. WHERE major_code = ?
  134. AND is_active = 1
  135. AND is_student = 1
  136. AND a.cwid = b.cwid
  137. AND b.school_id = ?
  138. AND a.cwid = c.student_id
  139. ORDER BY l_name, f_name
  140. ", array($major, $school_id));
  141. $cur = db_fetch_array($res);
  142. $batch["results"]["total"] = $cur["count"];
  143. }
  144. ////////////////////////////////////////////////////////////////////////
  145. // Okay, we can now begin the actual batch process.
  146. $current = $batch["results"]["current"];
  147. $total = $batch["results"]["total"];
  148. $limit = 1; // how many records to examine for THIS time through the function.
  149. $c = 0; // count of records.
  150. $db = get_global_database_handler();
  151. // Find active students with this major
  152. $res = db_query("SELECT * FROM students a, users b, student_degrees c
  153. WHERE major_code = ?
  154. AND is_active = 1
  155. AND is_student = 1
  156. AND a.cwid = b.cwid
  157. AND b.school_id = ?
  158. AND a.cwid = c.student_id
  159. ORDER BY l_name, f_name
  160. LIMIT $current, $limit", array($major, $school_id));
  161. while ($cur = db_fetch_array($res)) {
  162. if ($c >= $limit) {
  163. break;
  164. }
  165. $l_name = $cur["l_name"];
  166. $f_name = $cur["f_name"];
  167. // Remove possible trouble characters from names.
  168. $l_name = str_replace(",", "", $l_name);
  169. $f_name = str_replace(",", "", $f_name);
  170. $cwid = $cur["cwid"];
  171. $major_code = $cur["major_code"];
  172. $catalog_year = $cur["catalog_year"];
  173. $rank = $cur["rank_code"];
  174. $batch["csv"] .= "$cwid, $l_name, $f_name, $catalog_year, $major_code, $rank, ";
  175. // Load them up fully and calculate their percentages
  176. $student = new Student($cwid, $db);
  177. $degree_id = $db->get_degree_id($major_code, $catalog_year, FALSE, $school_id);
  178. if ($degree_id) {
  179. ///////////////////
  180. // Fully build up a FlightPath screen in memory, as if the student
  181. // were looking at this on a web page....
  182. $degree_plan = new DegreePlan($degree_id, $db, false, $student->array_significant_courses);
  183. $student->load_student_substitutions();
  184. $student->load_unassignments();
  185. $student->list_courses_taken->sort_alphabetical_order();
  186. $student->list_courses_taken->sort_most_recent_first();
  187. $fp = new FlightPath($student, $degree_plan);
  188. $screen = new AdvisingScreen("", $fp);
  189. $screen->view = "year";
  190. $fp->assign_courses_to_semesters(); // bare degree plan. not groups.
  191. $fp->assign_courses_to_groups();
  192. $screen->build_screen_elements();
  193. ////////////////
  194. // Okay, perform the percentage calculations...
  195. $degree_plan->calculate_progress_hours(FALSE, $requirement_types);
  196. $degree_plan->calculate_progress_quality_points(FALSE, $requirement_types);
  197. $use_degree_id = $degree_id;
  198. if (!isset($degree_plan->gpa_calculations[$use_degree_id])) {
  199. $use_degree_id = 0;
  200. }
  201. foreach ($requirement_types as $requirement_type => $label) {
  202. // Okay, let's see if this degreeplan even has any data on this requirement type.
  203. $total_hours = $degree_plan->gpa_calculations[$use_degree_id][$requirement_type]["total_hours"]*1;
  204. $fulfilled_hours = $degree_plan->gpa_calculations[$use_degree_id][$requirement_type]["fulfilled_hours"]*1;
  205. $qpts_hours = $degree_plan->gpa_calculations[$use_degree_id][$requirement_type]["qpts_hours"]*1;
  206. $qpts = $degree_plan->gpa_calculations[$use_degree_id][$requirement_type]["qpts"]*1;
  207. $gpa = $percent = 0;
  208. if ($qpts_hours > 0) {
  209. $gpa = fp_truncate_decimals($qpts / $qpts_hours, 3);
  210. }
  211. if ($total_hours > 0) {
  212. $percent = round(($fulfilled_hours / $total_hours) * 100);
  213. }
  214. $batch["csv"] .= "$fulfilled_hours, $total_hours, $percent, $gpa, ";
  215. }
  216. }
  217. $batch["csv"] .= "\n";
  218. $c++;
  219. } // while $cur = db_fetch_array
  220. // Update our $batch results variables
  221. $batch["results"]["current"] = $current + $c;
  222. if ($batch["results"]["current"] >= $total) {
  223. // We are finished!
  224. $batch["results"]["finished"] = TRUE;
  225. }
  226. // Add the batch_id to the SESSION so we can call it back later on.
  227. $_SESSION["stats_report_major_students_progress"][$school_id] = array();
  228. $_SESSION["stats_report_major_students_progress"][$school_id]["last_batch_id"] = $batch["batch_id"];
  229. $_SESSION["stats_report_major_students_progress"][$school_id]["last_timestamp"] = time();
  230. $_SESSION["stats_report_major_students_progress"][$school_id]["last_major"] = $major;
  231. }

Functions

Namesort descending Description
stats_report_major_students_progress_form Simply display a form letting the user select an available major.
stats_report_major_students_progress_form_submit Our submit handler. We will be setting up a new batch process.
stats_report_major_students_progress_perform_batch_operation This is the actual batch process function which gets called per run of the batch.