$listmembers = array(); $nonmembers = array(); $grade_items = array(); $grade_items = get_records('grade_item', 'courseid', $course->id); $grade_itemcount = count($grade_items); // get current group of students and assign that to be the working list of students if (groupmode($course) != 0) { if ($currentgroup = get_current_group($course->id)) { //groupmode is either separate or visible and there is a group $students = get_group_users($group); } else { //groupmode is either separate or visible and the current group is all participants $students = grade_get_course_students($course->id); } } else { $students = grade_get_course_students($course->id); //groupmode is not set (all participants) } // we need to create a multidimensional array keyed by grade_itemid with all_students at each level if (isset($grade_items)) { foreach ($grade_items as $grade_item) { $nonmembers[$grade_item->id] = array(); if ($students) { foreach ($students as $student) { $nonmembers[$grade_item->id][$student->id] = fullname($student, true); } } } } if ($grade_items) { foreach ($grade_items as $grade_item) {
function grade_download($download, $id) { global $CFG; require_login(); if (!($course = get_record("course", "id", $id))) { error("Course ID was incorrect"); } require_capability('moodle/course:viewcoursegrades', get_context_instance(CONTEXT_COURSE, $id)); $strgrades = get_string("grades"); $strgrade = get_string("grade"); $strmax = get_string("maximumshort"); $stractivityreport = get_string("activityreport"); /// Check to see if groups are being used in this course $currentgroup = get_current_group($course->id); if (($currentgroup = get_current_group($course->id)) && groupmode($course) != 0) { $students = get_group_students($currentgroup, "u.lastname ASC"); } else { $students = grade_get_course_students($course->id); } if (!empty($students)) { foreach ($students as $student) { $grades[$student->id] = array(); // Collect all grades in this array $gradeshtml[$student->id] = array(); // Collect all grades html formatted in this array $totals[$student->id] = array(); // Collect all totals in this array } } $columns = array(); // Accumulate column names in this array. $columnhtml = array(); // Accumulate column html in this array. /// Collect modules data get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused); /// Search through all the modules, pulling out grade data $sections = get_all_sections($course->id); // Sort everything the same as the course for ($i = 0; $i <= $course->numsections; $i++) { if (isset($sections[$i])) { // should always be true $section = $sections[$i]; if ($section->sequence) { $sectionmods = explode(",", $section->sequence); foreach ($sectionmods as $sectionmod) { $mod = $mods[$sectionmod]; $instance = get_record("{$mod->modname}", "id", "{$mod->instance}"); $libfile = "{$CFG->dirroot}/mod/{$mod->modname}/lib.php"; if (file_exists($libfile)) { require_once $libfile; $gradefunction = $mod->modname . "_grades"; if (function_exists($gradefunction)) { // Skip modules without grade function if ($modgrades = $gradefunction($mod->instance)) { if (!empty($modgrades->maxgrade)) { if ($mod->visible) { $maxgrade = "{$strmax}: {$modgrades->maxgrade}"; } else { $maxgrade = "{$strmax}: {$modgrades->maxgrade}"; } } else { $maxgrade = ""; } $columns[] = "{$mod->modfullname}: " . format_string($instance->name, true) . " - {$maxgrade}"; if (!empty($students)) { foreach ($students as $student) { if (!empty($modgrades->grades[$student->id])) { $grades[$student->id][] = $currentstudentgrade = $modgrades->grades[$student->id]; } else { $grades[$student->id][] = $currentstudentgrade = ""; $gradeshtml[$student->id][] = ""; } if (!empty($modgrades->maxgrade)) { $totals[$student->id] = (double) $totals[$student->id] + (double) $currentstudentgrade; } else { $totals[$student->id] = (double) $totals[$student->id] + 0; } } } } } } } } } } // a new Moodle nesting record? ;-) /// OK, we have all the data, now present it to the user /// OK, we have all the data, now present it to the user if ($download == "ods" and confirm_sesskey()) { require_once "../lib/odslib.class.php"; /// Calculate file name $downloadfilename = clean_filename("{$course->shortname} {$strgrades}.ods"); /// Creating a workbook $workbook = new MoodleODSWorkbook("-"); /// Sending HTTP headers $workbook->send($downloadfilename); /// Adding the worksheet $myxls =& $workbook->add_worksheet($strgrades); /// Print names of all the fields $myxls->write_string(0, 0, get_string("firstname")); $myxls->write_string(0, 1, get_string("lastname")); $myxls->write_string(0, 2, get_string("idnumber")); $myxls->write_string(0, 3, get_string("institution")); $myxls->write_string(0, 4, get_string("department")); $myxls->write_string(0, 5, get_string("email")); $pos = 6; foreach ($columns as $column) { $myxls->write_string(0, $pos++, strip_tags($column)); } $myxls->write_string(0, $pos, get_string("total")); /// Print all the lines of data. $i = 0; if (!empty($grades)) { foreach ($grades as $studentid => $studentgrades) { $i++; $student = $students[$studentid]; if (empty($totals[$student->id])) { $totals[$student->id] = ''; } $myxls->write_string($i, 0, $student->firstname); $myxls->write_string($i, 1, $student->lastname); $myxls->write_string($i, 2, $student->idnumber); $myxls->write_string($i, 3, $student->institution); $myxls->write_string($i, 4, $student->department); $myxls->write_string($i, 5, $student->email); $j = 6; foreach ($studentgrades as $grade) { if (is_numeric($grade)) { $myxls->write_number($i, $j++, strip_tags($grade)); } else { $myxls->write_string($i, $j++, strip_tags($grade)); } } $myxls->write_number($i, $j, $totals[$student->id]); } } /// Close the workbook $workbook->close(); exit; } else { if ($download == "xls" and confirm_sesskey()) { require_once "../lib/excellib.class.php"; /// Calculate file name $downloadfilename = clean_filename("{$course->shortname} {$strgrades}.xls"); /// Creating a workbook $workbook = new MoodleExcelWorkbook("-"); /// Sending HTTP headers $workbook->send($downloadfilename); /// Adding the worksheet $myxls =& $workbook->add_worksheet($strgrades); /// Print names of all the fields $myxls->write_string(0, 0, get_string("firstname")); $myxls->write_string(0, 1, get_string("lastname")); $myxls->write_string(0, 2, get_string("idnumber")); $myxls->write_string(0, 3, get_string("institution")); $myxls->write_string(0, 4, get_string("department")); $myxls->write_string(0, 5, get_string("email")); $pos = 6; foreach ($columns as $column) { $myxls->write_string(0, $pos++, strip_tags($column)); } $myxls->write_string(0, $pos, get_string("total")); /// Print all the lines of data. $i = 0; if (!empty($grades)) { foreach ($grades as $studentid => $studentgrades) { $i++; $student = $students[$studentid]; if (empty($totals[$student->id])) { $totals[$student->id] = ''; } $myxls->write_string($i, 0, $student->firstname); $myxls->write_string($i, 1, $student->lastname); $myxls->write_string($i, 2, $student->idnumber); $myxls->write_string($i, 3, $student->institution); $myxls->write_string($i, 4, $student->department); $myxls->write_string($i, 5, $student->email); $j = 6; foreach ($studentgrades as $grade) { if (is_numeric($grade)) { $myxls->write_number($i, $j++, strip_tags($grade)); } else { $myxls->write_string($i, $j++, strip_tags($grade)); } } $myxls->write_number($i, $j, $totals[$student->id]); } } /// Close the workbook $workbook->close(); exit; } else { if ($download == "txt" and confirm_sesskey()) { /// Print header to force download header("Content-Type: application/download\n"); $downloadfilename = clean_filename("{$course->shortname} {$strgrades}"); header("Content-Disposition: attachment; filename=\"{$downloadfilename}.txt\""); /// Print names of all the fields echo get_string("firstname") . "\t" . get_string("lastname") . "\t" . get_string("idnumber") . "\t" . get_string("institution") . "\t" . get_string("department") . "\t" . get_string("email"); foreach ($columns as $column) { $column = strip_tags($column); echo "\t{$column}"; } echo "\t" . get_string("total") . "\n"; /// Print all the lines of data. foreach ($grades as $studentid => $studentgrades) { $student = $students[$studentid]; if (empty($totals[$student->id])) { $totals[$student->id] = ''; } echo "{$student->firstname}\t{$student->lastname}\t{$student->idnumber}\t{$student->institution}\t{$student->department}\t{$student->email}"; foreach ($studentgrades as $grade) { $grade = strip_tags($grade); echo "\t{$grade}"; } echo "\t" . $totals[$student->id]; echo "\n"; } exit; } else { if ($download == '' and confirm_sesskey()) { error("No file type specified"); exit; } } } } }