adding current groupid to grade_export class - soon to be used in plugins
[moodle-pu.git] / grade / export / lib.php
blob90725f532b84041f9749cdd059c621eb5da63a06
1 <?php
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
11 // //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
16 // //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
24 ///////////////////////////////////////////////////////////////////////////
26 require_once($CFG->dirroot.'/lib/gradelib.php');
27 require_once($CFG->dirroot.'/grade/lib.php');
28 require_once($CFG->dirroot.'/grade/export/grade_export_form.php');
30 /**
31 * Base export class
33 class grade_export {
35 var $id; // course id
36 var $grade_items; // array of grade_items
37 var $groupid;
38 var $grades = array(); // Collect all grades in this array
39 var $comments = array(); // Collect all comments for each grade
40 var $columns = array(); // Accumulate column names in this array.
41 var $columnidnumbers = array(); // Collect all gradeitem id numbers
42 var $students = array();
43 var $course; // course
44 var $userkey; // Optional MD5 string used to publish this export data via a URL
45 var $export_letters;
46 var $itemidsurl; // A string of itemids to add to the URL for the export
48 // common strings
49 var $strgrades;
50 var $strgrade;
52 /**
53 * Constructor should set up all the private variables ready to be pulled
54 * @param int $courseid course id
55 * @param array $itemids array of grade item ids, empty means all
56 * @param stdClass $formdata Optional object of formdata.
57 * @note Exporting as letters will lead to data loss if that exported set it re-imported.
59 function grade_export($courseid, $itemids=null, $formdata=null) {
60 global $CFG, $USER, $COURSE;
62 $this->export_letters = false;
63 if (isset($formdata->export_letters)) {
64 $this->export_letters = $formdata->export_letters;
67 $this->userkey = false;
68 if (isset($formdata->key)) {
69 if ($formdata->key == 1 && isset($formdata->iprestriction) && isset($formdata->validuntil)) { // Create a new key
70 $formdata->key = create_user_key('grade/export', $USER->id, $COURSE->id, $formdata->iprestriction, $formdata->validuntil);
72 $this->userkey = $formdata->key;
75 $this->strgrades = get_string("grades");
76 $this->strgrade = get_string("grade");
78 if (!$course = get_record("course", "id", $courseid)) {
79 error("Course ID was incorrect");
81 $context = get_context_instance(CONTEXT_COURSE, $course->id);
82 require_capability('moodle/grade:export', $context);
84 $this->id = $course->id;
85 $this->course = $course;
87 // fetch all grade items
88 if (empty($itemids)) {
89 $this->grade_items = grade_item::fetch_all(array('courseid'=>$this->id));
90 } else {
91 $this->grade_items = array();
92 foreach ($itemids as $iid) {
93 if ($grade_item = grade_item::fetch(array('id'=>(int)$iid, 'courseid'=>$this->id))) {
94 $this->grade_items[$grade_item->id] = $grade_item;
99 // init colums
100 foreach ($this->grade_items as $grade_item) {
101 if ($grade_item->itemtype == 'mod') {
102 $this->columns[$grade_item->id] = get_string('modulename', $grade_item->itemmodule).': '.$grade_item->get_name();
103 } else {
104 $this->columns[$grade_item->id] = $grade_item->get_name();
106 $this->columnidnumbers[$grade_item->id] = $grade_item->idnumber; // this might be needed for some export plugins
109 /// Check to see if groups are being used in this course
110 if ($groupmode = groupmode($course)) { // Groups are being used
112 if (isset($_GET['group'])) {
113 $changegroup = $_GET['group']; /// 0 or higher
114 } else {
115 $changegroup = -1; /// This means no group change was specified
118 $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
120 } else {
121 $currentgroup = false;
124 $this->groupid = $currentgroup;
126 if ($currentgroup) {
127 $this->students = get_group_students($currentgroup, "u.lastname ASC");
128 } else {
129 $this->students = get_role_users(@implode(',', $CFG->gradebookroles), $context);
132 if (!empty($this->students)) {
133 foreach ($this->students as $student) {
134 $this->grades[$student->id] = array(); // Collect all grades in this array
135 $this->comments[$student->id] = array(); // Collect all comments in tihs array
139 if (isset($formdata->itemids)) {
140 // Build itemidsurl for links
141 $itemids = array();
142 if ($formdata->itemids) {
143 foreach ($formdata->itemids as $itemid=>$selected) {
144 if ($selected) {
145 $itemids[] = $itemid;
148 $this->itemidsurl = implode(",", $itemids);
149 } else {
150 //error?
151 $this->itemidsurl = '';
156 function load_grades() {
157 global $CFG;
159 // first make sure we have all final grades
160 // TODO: check that no grade_item has needsupdate set
161 grade_regrade_final_grades($this->id);
163 if ($this->export_letters) {
164 require_once($CFG->dirroot . '/grade/report/lib.php');
165 $report = new grade_report($this->id, null, null);
166 $letters = $report->get_grade_letters();
167 } else {
168 $letters = null;
171 if ($this->grade_items) {
172 foreach ($this->grade_items as $gradeitem) {
173 // load as an array of grade_final objects
174 if ($itemgrades = $gradeitem->get_final() and !empty($this->students)) {
175 foreach ($this->students as $student) {
176 $finalgrade = null;
177 $feedback = '';
178 if (array_key_exists($student->id, $itemgrades)) {
179 $finalgrade = $itemgrades[$student->id]->finalgrade;
180 $grade = new grade_grade($itemgrades[$student->id], false);
181 if ($grade_text = $grade->load_text()) {
182 $feedback = format_text($grade_text->feedback, $grade_text->feedbackformat);
186 if ($this->export_letters) {
187 $grade_item_displaytype = $report->get_pref('gradedisplaytype', $gradeitem->id);
188 // TODO Convert final grade to letter if export option is on, and grade_item is set to letter type MDL-10490
189 if ($grade_item_displaytype == GRADE_REPORT_GRADE_DISPLAY_TYPE_LETTER) {
190 $finalgrade = grade_grade::get_letter($letters, $finalgrade, $gradeitem->grademin, $gradeitem->grademax);
194 $this->grades[$student->id][$gradeitem->id] = $finalgrade;
195 $this->comments[$student->id][$gradeitem->id] = $feedback;
203 * To be implemented by child class
204 * TODO finish PHPdocs
206 function print_grades() { }
209 * Displays all the grades on screen as a feedback mechanism
210 * TODO finish PHPdoc
212 function display_grades($feedback=false, $rows=10) {
214 $this->load_grades();
216 echo '<table>';
217 echo '<tr>';
218 echo '<th>'.get_string("firstname")."</th>".
219 '<th>'.get_string("lastname")."</th>".
220 '<th>'.get_string("idnumber")."</th>".
221 '<th>'.get_string("institution")."</th>".
222 '<th>'.get_string("department")."</th>".
223 '<th>'.get_string("email")."</th>";
224 foreach ($this->columns as $column) {
225 $column = strip_tags($column);
226 echo "<th>$column</th>";
228 /// add a column_feedback column
229 if ($feedback) {
230 echo "<th>{$column}_feedback</th>";
233 echo '</tr>';
234 /// Print all the lines of data.
236 $i = 0;
237 foreach ($this->grades as $studentid => $studentgrades) {
239 // number of preview rows
240 if ($i++ == $rows) {
241 break;
243 echo '<tr>';
244 $student = $this->students[$studentid];
246 echo "<td>$student->firstname</td><td>$student->lastname</td><td>$student->idnumber</td><td>$student->institution</td><td>$student->department</td><td>$student->email</td>";
247 foreach ($studentgrades as $itemid=>$grade) {
248 $grade = strip_tags($grade);
249 echo "<td>$grade</td>";
251 if ($feedback) {
252 echo '<td>'.$this->comments[$studentid][$itemid].'</td>';
255 echo "</tr>";
257 echo '</table>';
261 * Either prints a "continue" box, which will redirect the user to the download page, or prints the URL for the published data.
262 * @note exit() at the end of the method
263 * @param string $plugin Required: name of the plugin calling this method. Used for building the URL.
264 * @return void
266 function print_continue($plugin) {
267 global $CFG;
269 // this redirect should trigger a download prompt
270 if (!$this->userkey) {
271 print_continue('export.php?id='.$this->id.'&amp;itemids='.$this->itemidsurl.'&amp;export_letters='.$this->export_letters);
273 } else {
274 $link = $CFG->wwwroot.'/grade/export/'.$plugin.'/dump.php?id='.$this->id.'&amp;itemids='
275 . $this->itemidsurl.'&amp;export_letters='.$this->export_letters.'&amp;key='.$this->userkey;
277 echo '<p>';
278 echo '<a href="'.$link.'">'.$link.'</a>';
279 echo '</p>';
280 print_footer();
282 exit();