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 = $m = "";
  54. if (time() < $last_timestamp + (60 * 60)) {
  55. $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>";
  56. $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>";
  57. $form["mark" . $m++] = array(
  58. "value" => $html,
  59. );
  60. }
  61. // If the Batch module isn't enabled, stop right here!!!
  62. if (!function_exists("batch_set")) {
  63. $form = array();
  64. $form["markup"] = array(
  65. "value" => "<p><b>" . t("To use this report, the Batch module must be enabled. Please ask your administrator
  66. to enable the Batch module.") . "</b></p>",
  67. );
  68. }
  69. return $form;
  70. }
  71. /**
  72. * Our submit handler. We will be setting up a new batch process.
  73. *
  74. * @param unknown_type $form
  75. * @param unknown_type $form_state
  76. */
  77. function stats_report_major_students_progress_form_submit($form, $form_state) {
  78. $major = $form_state["values"]["major"];
  79. $school_id = intval($form_state['values']['school_id']);
  80. // Okay, set up the batch....
  81. $batch = array(
  82. "operation" => array("stats_report_major_students_progress_perform_batch_operation", array($major, $school_id)),
  83. "title" => t("Major Students Progress - CSV Export - Major: ") . $major,
  84. "file" => menu_get_module_path("stats") . "/reports.major-students-progress.inc",
  85. "progress_message" => "Processing student @current of @total",
  86. "display_percent" => TRUE,
  87. );
  88. watchdog('stats', "report_major_students_progress major:$major, school_id:$school_id", array());
  89. // Set the batch...
  90. batch_set($batch);
  91. }
  92. /**
  93. * This is the actual batch process function which gets called per run of the batch.
  94. *
  95. * @param unknown_type $batch
  96. * @param unknown_type $major
  97. */
  98. function stats_report_major_students_progress_perform_batch_operation(&$batch, $major, $school_id = 0) {
  99. // For later, let's get our requirement types from the pie chart config...
  100. $requirement_types = array();
  101. // Get the requested piecharts from our config...
  102. $temp = variable_get_for_school("pie_chart_config", "c ~ Core Requirements\nm ~ Major Requirements\ndegree ~ Degree Progress", $school_id);
  103. $lines = explode("\n", $temp);
  104. foreach ($lines as $line) {
  105. if (trim($line) == "") continue;
  106. $temp = explode("~", $line);
  107. $requirement_type = trim($temp[0]);
  108. $label = trim($temp[1]);
  109. $requirement_types[$requirement_type] = $label;
  110. }
  111. //////////////////////////// FIRST TIME ///////////////////////////////
  112. // If this is our first time through, let's init our values.
  113. if (!isset($batch["results"]["total"])) {
  114. // Our first time through. Let's start up.
  115. $batch["results"]["current"] = 0;
  116. $batch["results"]["finished"] = FALSE;
  117. $batch["csv"] = "CWID, LAST, FIRST, CATALOG, MAJOR, RANK, ";
  118. // Get the requested piecharts from our config...
  119. $temp = variable_get_for_school("pie_chart_config", "c ~ Core Requirements\nm ~ Major Requirements\ndegree ~ Degree Progress", $school_id);
  120. $lines = explode("\n", $temp);
  121. foreach ($lines as $line) {
  122. if (trim($line) == "") continue;
  123. $temp = explode("~", $line);
  124. $requirement_type = trim($temp[0]);
  125. $label = trim($temp[1]);
  126. $requirement_types[$requirement_type] = $label;
  127. $batch["csv"] .= "$requirement_type FULFILLED, $requirement_type TOTAL, $requirement_type PERCENT, $requirement_type GPA, ";
  128. }
  129. $batch["csv"] .= "\n";
  130. // We need the "total" number of students in the selected major.
  131. $res = db_query("SELECT COUNT(*) as count FROM students a, users b, student_degrees c
  132. WHERE major_code = ?
  133. AND is_active = 1
  134. AND is_student = 1
  135. AND a.cwid = b.cwid
  136. AND b.school_id = ?
  137. AND a.cwid = c.student_id
  138. ORDER BY l_name, f_name
  139. ", array($major, $school_id));
  140. $cur = db_fetch_array($res);
  141. $batch["results"]["total"] = $cur["count"];
  142. }
  143. ////////////////////////////////////////////////////////////////////////
  144. // Okay, we can now begin the actual batch process.
  145. $current = $batch["results"]["current"];
  146. $total = $batch["results"]["total"];
  147. $limit = 7; // how many records to examine for THIS time through the function.
  148. $c = 0; // count of records.
  149. $db = get_global_database_handler();
  150. // Find active students with this major
  151. $res = db_query("SELECT * FROM students a, users b, student_degrees c
  152. WHERE major_code = ?
  153. AND is_active = 1
  154. AND is_student = 1
  155. AND a.cwid = b.cwid
  156. AND b.school_id = ?
  157. AND a.cwid = c.student_id
  158. ORDER BY l_name, f_name
  159. LIMIT $current, $limit", array($major, $school_id));
  160. while ($cur = db_fetch_array($res)) {
  161. if ($c >= $limit) {
  162. break;
  163. }
  164. $l_name = $cur["l_name"];
  165. $f_name = $cur["f_name"];
  166. // Remove possible trouble characters from names.
  167. $l_name = str_replace(",", "", $l_name);
  168. $f_name = str_replace(",", "", $f_name);
  169. $cwid = $cur["cwid"];
  170. $major_code = $cur["major_code"];
  171. $catalog_year = $cur["catalog_year"];
  172. $rank = $cur["rank_code"];
  173. $batch["csv"] .= "$cwid, $l_name, $f_name, $catalog_year, $major_code, $rank, ";
  174. // Load them up fully and calculate their percentages
  175. $student = new Student($cwid, $db);
  176. $degree_id = $db->get_degree_id($major_code, $catalog_year, FALSE, $school_id);
  177. if ($degree_id) {
  178. ///////////////////
  179. // Fully build up a FlightPath screen in memory, as if the student
  180. // were looking at this on a web page....
  181. $degree_plan = new DegreePlan($degree_id, $db, false, $student->array_significant_courses);
  182. $student->load_student_substitutions();
  183. $student->load_unassignments();
  184. $student->list_courses_taken->sort_alphabetical_order();
  185. $student->list_courses_taken->sort_most_recent_first();
  186. $fp = new FlightPath($student, $degree_plan);
  187. $screen = new AdvisingScreen("", $fp);
  188. $screen->view = "year";
  189. $fp->assign_courses_to_semesters(); // bare degree plan. not groups.
  190. $fp->assign_courses_to_groups();
  191. $screen->build_screen_elements();
  192. ////////////////
  193. // Okay, perform the percentage calculations...
  194. $degree_plan->calculate_progress_hours(FALSE, $requirement_types);
  195. $degree_plan->calculate_progress_quality_points(FALSE, $requirement_types);
  196. $use_degree_id = $degree_id;
  197. if (!isset($degree_plan->gpa_calculations[$use_degree_id])) {
  198. $use_degree_id = 0;
  199. }
  200. foreach ($requirement_types as $requirement_type => $label) {
  201. // Okay, let's see if this degreeplan even has any data on this requirement type.
  202. $total_hours = $degree_plan->gpa_calculations[$use_degree_id][$requirement_type]["total_hours"]*1;
  203. $fulfilled_hours = $degree_plan->gpa_calculations[$use_degree_id][$requirement_type]["fulfilled_hours"]*1;
  204. $qpts_hours = $degree_plan->gpa_calculations[$use_degree_id][$requirement_type]["qpts_hours"]*1;
  205. $qpts = $degree_plan->gpa_calculations[$use_degree_id][$requirement_type]["qpts"]*1;
  206. $gpa = $percent = 0;
  207. if ($qpts_hours > 0) {
  208. $gpa = fp_truncate_decimals($qpts / $qpts_hours, 3);
  209. }
  210. if ($total_hours > 0) {
  211. $percent = round(($fulfilled_hours / $total_hours) * 100);
  212. }
  213. $batch["csv"] .= "$fulfilled_hours, $total_hours, $percent, $gpa, ";
  214. }
  215. }
  216. $batch["csv"] .= "\n";
  217. $c++;
  218. } // while $cur = db_fetch_array
  219. // Update our $batch results variables
  220. $batch["results"]["current"] = $current + $c;
  221. if ($batch["results"]["current"] >= $total) {
  222. // We are finished!
  223. $batch["results"]["finished"] = TRUE;
  224. }
  225. // Add the batch_id to the SESSION so we can call it back later on.
  226. $_SESSION["stats_report_major_students_progress"][$school_id] = array();
  227. $_SESSION["stats_report_major_students_progress"][$school_id]["last_batch_id"] = $batch["batch_id"];
  228. $_SESSION["stats_report_major_students_progress"][$school_id]["last_timestamp"] = time();
  229. $_SESSION["stats_report_major_students_progress"][$school_id]["last_major"] = $major;
  230. }

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.