commit 5447e426e5a0bb4c95e855b2533fb6750a1c1139
Author: Gordon Anderson <gordon@catalyst.net.nz>
Date:   Tue Mar 3 15:25:21 2009 +1300

    Global grades: new admin report for exporting activity data from more than one course at a time.  Added
    select/deselect all button to code previously created by Francois Marrier.
    
        This report depends on the refactoring done in b18c3700b0d34ee00c078028e1382c71e266bacf.

diff --git a/admin/report/globalgrades/globalgrades_export_form.php b/admin/report/globalgrades/globalgrades_export_form.php
new file mode 100644
index 0000000..1776f4c
--- /dev/null
+++ b/admin/report/globalgrades/globalgrades_export_form.php
@@ -0,0 +1,85 @@
+<?php
+
+require_once $CFG->libdir.'/formslib.php';
+require_once $CFG->dirroot.'/grade/lib.php';
+
+class globalgrades_export_form extends moodleform {
+
+    function definition() {
+        global $CFG;
+
+        $mform =& $this->_form;
+        if (isset($this->_customdata)) {  // hardcoding plugin names here is hacky
+            $features = $this->_customdata;
+        } else {
+            $features = array();
+        }
+
+        $mform->addElement('header', 'options', get_string('options', 'grades'));
+
+        $mform->addElement('advcheckbox', 'export_feedback', get_string('exportfeedback', 'grades'));
+        $mform->setDefault('export_feedback', 0);
+
+        $options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
+        $mform->addElement('select', 'previewrows', get_string('previewrows', 'grades'), $options);
+
+        if (!empty($features['updategradesonly'])) {
+            $mform->addElement('advcheckbox', 'updatedgradesonly', get_string('updatedgradesonly', 'grades'));
+        }
+        /// selections for decimal points and format, MDL-11667, defaults to site settings, if set
+        //$default_gradedisplaytype = $CFG->grade_export_displaytype;
+        $options = array(GRADE_DISPLAY_TYPE_REAL       => get_string('real', 'grades'),
+                         GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'),
+                         GRADE_DISPLAY_TYPE_LETTER     => get_string('letter', 'grades'));
+
+        $mform->addElement('select', 'display', get_string('gradeexportdisplaytype', 'grades'), $options);
+        $mform->setDefault('display', $CFG->grade_export_displaytype);
+
+        //$default_gradedecimals = $CFG->grade_export_decimalpoints;
+        $options = array(0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
+        $mform->addElement('select', 'decimals', get_string('gradeexportdecimalpoints', 'grades'), $options);
+        $mform->setDefault('decimals', $CFG->grade_export_decimalpoints);
+        $mform->disabledIf('decimals', 'display', 'eq', GRADE_DISPLAY_TYPE_LETTER);
+
+        if (!empty($features['includeseparator'])) {
+            $radio = array();
+            $radio[] = &MoodleQuickForm::createElement('radio', 'separator', null, get_string('septab', 'grades'), 'tab');
+            $radio[] = &MoodleQuickForm::createElement('radio', 'separator', null, get_string('sepcomma', 'grades'), 'comma');
+            $mform->addGroup($radio, 'separator', get_string('separator', 'grades'), ' ', false);
+            $mform->setDefault('separator', 'comma');
+        }
+
+        $mform->addElement('header', 'gradeitems', get_string('gradeitemsinc', 'grades'));
+
+        $needs_multiselect = false;
+        foreach ($features['courseids'] as $courseid) {
+            $switch = grade_get_setting($courseid, 'aggregationposition', $CFG->grade_aggregationposition);
+
+            // Grab the grade_seq for this course
+            $gseq = new grade_seq($courseid, $switch);
+
+            if ($grade_items = $gseq->items) {
+                foreach ($grade_items as $grade_item) {
+                    if (!empty($features['idnumberrequired']) and empty($grade_item->idnumber)) {
+                        $mform->addElement('advcheckbox', 'itemids['.$grade_item->id.']', $grade_item->get_name(), get_string('noidnumber', 'grades'));
+                        $mform->hardFreeze('itemids['.$grade_item->id.']');
+                    } else {
+                        $mform->addElement('advcheckbox', 'itemids['.$grade_item->id.']', $grade_item->get_name(), null, array('group' => 1));
+                        $mform->setDefault('itemids['.$grade_item->id.']', 1);
+                        $needs_multiselect = true;
+                    }
+                }
+            }
+        }
+
+        if ($needs_multiselect) {
+            $this->add_checkbox_controller(1, null, null, 1); // 1st argument is group name, 2nd is link text, 3rd is attributes and 4th is original value
+        }
+
+        $mform->addElement('hidden', 'id', implode(',', $features['courseids']));
+        $mform->addElement('hidden', 'outputformat', $features['outputformat']);
+        $this->add_action_buttons(false, get_string('submit'));
+    }
+}
+
+?>
\ No newline at end of file
diff --git a/admin/report/globalgrades/index.php b/admin/report/globalgrades/index.php
new file mode 100644
index 0000000..dd1380a
--- /dev/null
+++ b/admin/report/globalgrades/index.php
@@ -0,0 +1,240 @@
+<?php
+
+/**
+ * Add the ability to display and download grades for the activities
+ * of multiple courses all on one page/spreadsheet.
+ *
+ * @author Francois Marier <francois@catalyst.net.nz>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ */
+
+require_once(dirname(__FILE__).'/../../../config.php');
+require_once($CFG->libdir.'/adminlib.php');
+require_once($CFG->libdir.'/gradelib.php');
+require_once($CFG->dirroot.'/course/lib.php');
+require_once $CFG->dirroot.'/grade/lib.php';
+require_once('globalgrades_export_form.php');
+
+$outputformat = optional_param('outputformat', '', PARAM_ALPHA);
+
+// Check permissions.
+require_login();
+$systemcontext = get_context_instance(CONTEXT_SYSTEM);
+require_capability('moodle/site:viewreports', $systemcontext);
+
+add_to_log(SITEID, "admin", "report globalgrades", "report/globalgrades/index.php", '');
+
+admin_externalpage_setup('reportglobalgrades');
+admin_externalpage_print_header();
+
+// Make a list of the course IDs that were selected (if any)
+$selectedcourseids = array();
+if ($data = data_submitted()) {
+    $rawids = array();
+
+    // There are two ways to receive course IDs
+    if (isset($data->courses)) {
+        $rawids = $data->courses;
+    }
+    elseif (isset($data->id)) {
+        $rawids = explode(',', $data->id);
+    }
+
+    // Validation on the course IDs
+    foreach ($rawids as $rawcourseid) {
+        $courseid = clean_param($rawcourseid, PARAM_INT);
+        if (!empty($courseid) && ($courseid > 0)) {
+            $selectedcourseids[] = $courseid;
+        }
+    }
+}
+
+$mform = new globalgrades_export_form(null, array('courseids' => $selectedcourseids,
+                                                  'outputformat' => $outputformat));
+
+if ($data = $mform->get_data()) {
+    $whereclause = get_course_ids_string($data->id, array('moodle/grade:export', 'gradeexport/xls:view'));
+    if (!$courses = get_records_select('course', $whereclause)) {
+        print_error('nocourseid');
+    }
+
+    require_once($CFG->dirroot."/grade/export/{$outputformat}/grade_export_{$outputformat}.php");
+
+    // Export preview form
+    $classname = 'grade_export_'.$outputformat;
+    $export = new $classname($courses, 0, '', false, false, $data->display, $data->decimals);
+
+    // print the grades on screen for feedbacks
+    $export->process_form($data);
+    $export->print_continue();
+    $export->display_preview();
+}
+elseif (data_submitted()) {
+    // Export options form
+    if (count($selectedcourseids) > 0) {
+        print_box_start();
+        echo '<div class="clearer"></div>';
+        $mform->display();
+        print_box_end();
+    }
+    else {
+        error(get_string('notenoughparameters', 'report_globalgrades'), 'index.php');
+    }
+}
+else {
+    // Course selection form
+    print_box_start();
+    print '<form method="post" action="index.php">';
+
+    print '<p>'.get_string('selectcourses', 'report_globalgrades').'</p>';
+
+    print_course_list(NULL, NULL, NULL, -1);
+
+
+    $strselectall = get_string("selectall");
+    $strdeselectall = get_string("deselectall");
+    echo "<p>";
+    echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
+    echo "<input type=\"button\" onclick=\"uncheckall()\" value=\"$strdeselectall\" />\n";
+    echo "</p>";
+    
+    $exports = get_list_of_plugins('grade/export', 'CVS');
+    $exportnames = array();
+    if (!empty($exports)) {
+        foreach ($exports as $plugin) {
+            $exportnames[$plugin] = get_string('modulename', 'gradeexport_'.$plugin);
+        }
+        asort($exportnames);
+    }
+    print '<p>'.get_string('outputformat', 'report_globalgrades').' '.
+        choose_from_menu($exportnames, 'outputformat', 'ods', '', '', 0, true).'</p>';
+
+    print '<p><input type="submit" value="'.get_string('generatereport', 'report_globalgrades').'" /></p>';
+
+    print '</form>';
+    print_box_end();
+?>
+
+<script type="text/javascript">
+//<![CDATA[
+var pixpath = "<?php echo $CFG->pixpath; ?>";
+function togglecategory(spanid) {
+    var span = document.getElementById("category_" + String(spanid));
+    var arrow = document.getElementById("arrow_" + String(spanid));
+
+    if (span.style.display == "none") {
+        // Expand category
+        span.style.display = "";
+        arrow.src = pixpath + '/i/arw_on.gif';
+    } else {
+        // Collapse category
+        span.style.display = "none";
+        arrow.src = pixpath + '/i/arw_off.gif';
+    }
+}
+//]]>
+</script>
+
+<?php
+}
+
+admin_externalpage_print_footer();
+
+function print_course_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1, $categoryid=0) {
+    global $CFG;
+
+    $categoryid += 1;
+
+    if (!$displaylist) {
+        make_categories_list($displaylist, $parentslist);
+    }
+
+    if (!$category) {
+        $category->id = 0;
+    }
+
+    $categories = get_child_categories($category->id);
+    $courses = get_courses($category->id, 'c.sortorder ASC', 'c.id,c.sortorder,c.visible,c.fullname,c.shortname,c.password,c.summary,c.guest,c.cost,c.currency');
+
+    if ($category->id != 0) {
+        if ($category->visible) {
+            $onclick = '';
+            if (!empty($CFG->collapsingcategorytree) && ($categories or $courses)) {
+                $onclick = 'onclick="togglecategory('.$categoryid.');return false"';
+            }
+            print_category_info($category, $depth, false, $onclick, $categoryid);
+        } else {
+            return $categoryid; // Don't bother printing children of invisible categories
+        }
+    }
+
+    $spanid = 0;
+    if (($category->id != 0) && ($courses or $categories)) {
+        $spanid = $categoryid;
+        $style = '';
+        if (!empty($CFG->collapsingcategorytree)) {
+            $style = 'style="display: none"';
+        }
+        echo "\n\n<div id=\"category_$spanid\" $style>";
+    }
+
+    // Print all child categories recursively
+    if ($categories) {
+        $countcats = count($categories);
+        $count = 0;
+        $first = true;
+        $last = false;
+        foreach ($categories as $cat) {
+            $count++;
+            if ($count == $countcats) {
+                $last = true;
+            }
+            $up = $first ? false : true;
+            $down = $last ? false : true;
+            $first = false;
+
+            $categoryid = print_course_list($cat, $displaylist, $parentslist, $depth + 1, $categoryid);
+        }
+    }
+
+    // Print courses contained in the current category
+    if ($courses && ($category->id != 0)) {
+        echo "\n\n".'<table class="categorylist">';
+
+        $childdepth = $depth + 1;
+        foreach ($courses as $course) {
+            if (!$course->visible) {
+                continue; // Invisibile courses have no relevant grades
+            }
+
+            echo '<tr>';
+            if ($childdepth) {
+                $indent = ($childdepth)*30;
+                echo '<td>';
+                print_spacer(10, $indent);
+                echo '</td>';
+            }
+
+            echo '<td valign="top" style="width: 100%" class="course name">';
+            $elementid = 'course'.$course->id;
+            echo "<input type=\"checkbox\" name=\"courses[]\" id=\"$elementid\" value=\"$course->id\" /> ";
+            $coursename = format_string($course->fullname);
+            echo "<label for=\"$elementid\">$coursename</label> ";
+            echo '<a href="'.$CFG->wwwroot.'/grade/report/grader/index.php?id='.
+                $course->id.'"><img src="'.$CFG->pixpath.'/i/grades.gif" alt="'.
+                get_string('grades').'" title="'.get_string('grades').'"/></a>';
+            echo '</td>';
+
+            echo '</tr>';
+        }
+        echo '</table>';
+    }
+
+    if ($spanid != 0) {
+        echo "\n\n".'</div>';
+    }
+
+    return $categoryid; // returns the highest category id of the children
+}
+
+?>
diff --git a/admin/report/globalgrades/lang/en_utf8/report_globalgrades.php b/admin/report/globalgrades/lang/en_utf8/report_globalgrades.php
new file mode 100644
index 0000000..0177cc7
--- /dev/null
+++ b/admin/report/globalgrades/lang/en_utf8/report_globalgrades.php
@@ -0,0 +1,10 @@
+<?php
+
+$string['generatereport'] = 'Generate report';
+$string['globalgrades'] = 'Global grades';
+$string['newreport'] = 'New report';
+$string['notenoughparameters'] = 'Not enough parameters, you must select at least one course.';
+$string['outputformat'] = 'Output format:';
+$string['selectcourses'] = 'Select the courses to include in the report:';
+
+?>
\ No newline at end of file

