Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / grade / import / lib.php
blobdc55db553f755e3ca729c06d8f5ea7d9d16dd811
1 <?php // $Id$
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->libdir.'/gradelib.php');
28 /**
29 * Returns new improtcode for current user
30 * @return int importcode
32 function get_new_importcode() {
33 global $USER;
35 $importcode = time();
36 while (get_record('grade_import_values', 'importcode', $importcode, 'importer', $USER->id)) {
37 $importcode--;
40 return $importcode;
43 /**
44 * given an import code, commits all entries in buffer tables
45 * (grade_import_value and grade_import_newitem)
46 * If this function is called, we assume that all data collected
47 * up to this point is fine and we can go ahead and commit
48 * @param int courseid - id of the course
49 * @param string importcode - import batch identifier
50 * @param feedback print feedback and continue button
51 * @return bool success
53 function grade_import_commit($courseid, $importcode, $importfeedback=true, $verbose=true) {
54 global $CFG, $USER;
56 $commitstart = time(); // start time in case we need to roll back
57 $newitemids = array(); // array to hold new grade_item ids from grade_import_newitem table, mapping array
59 /// first select distinct new grade_items with this batch
61 if ($newitems = get_records_sql("SELECT *
62 FROM {$CFG->prefix}grade_import_newitem
63 WHERE importcode = $importcode AND importer={$USER->id}")) {
65 // instances of the new grade_items created, cached
66 // in case grade_update fails, so that we can remove them
67 $instances = array();
68 $failed = false;
69 foreach ($newitems as $newitem) {
70 // get all grades with this item
72 if ($grades = get_records('grade_import_values', 'newgradeitem', $newitem->id)) {
73 /// create a new grade item for this - must use false as second param!
74 /// TODO: we need some bounds here too
75 $gradeitem = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual', 'itemname'=>$newitem->itemname), false);
76 $gradeitem->insert('import');
77 $instances[] = $gradeitem;
79 // insert each individual grade to this new grade item
80 foreach ($grades as $grade) {
81 if (!$gradeitem->update_final_grade($grade->userid, $grade->finalgrade, 'import', $grade->feedback, FORMAT_MOODLE)) {
82 $failed = true;
83 break 2;
89 if ($failed) {
90 foreach ($instances as $instance) {
91 $gradeitem->delete('import');
93 import_cleanup($importcode);
94 return false;
98 /// then find all existing items
100 if ($gradeitems = get_records_sql("SELECT DISTINCT (itemid)
101 FROM {$CFG->prefix}grade_import_values
102 WHERE importcode = $importcode AND importer={$USER->id} AND itemid > 0")) {
104 $modifieditems = array();
106 foreach ($gradeitems as $itemid=>$notused) {
108 if (!$gradeitem = new grade_item(array('id'=>$itemid))) {
109 // not supposed to happen, but just in case
110 import_cleanup($importcode);
111 return false;
113 // get all grades with this item
114 if ($grades = get_records('grade_import_values', 'itemid', $itemid)) {
116 // make the grades array for update_grade
117 foreach ($grades as $grade) {
118 if (!$importfeedback) {
119 $grade->feedback = false; // ignore it
121 if (!$gradeitem->update_final_grade($grade->userid, $grade->finalgrade, 'import', $grade->feedback)) {
122 $failed = 1;
123 break 2;
126 //$itemdetails -> idnumber = $gradeitem->idnumber;
127 $modifieditems[] = $itemid;
131 if (!empty($failed)) {
132 import_cleanup($importcode);
133 return false;
138 if ($verbose) {
139 notify(get_string('importsuccess', 'grades'), 'notifysuccess');
140 $unenrolledusers = get_unenrolled_users_in_import($importcode, $courseid);
141 if ($unenrolledusers) {
142 $list = "<ul>\n";
143 foreach ($unenrolledusers as $u) {
144 $u->fullname = fullname($u);
145 $list .= '<li>' . get_string('usergrade', 'grades', $u) . '</li>';
147 $list .= "</ul>\n";
148 notify(get_string('unenrolledusersinimport', 'grades', $list), 'notifysuccess');
150 print_continue($CFG->wwwroot.'/grade/index.php?id='.$courseid);
152 // clean up
153 import_cleanup($importcode);
155 return true;
159 * This function returns an array of grades that were included in the import,
160 * but wherer the user does not currenly have a graded role on the course. These gradse
161 * are still stored in the database, but will not be visible in the gradebook unless
162 * this user subsequently enrols on the course in a graded roles.
164 * The returned objects have fields user firstname, lastname and useridnumber, and gradeidnumber.
166 * @param integer $importcode import batch identifier
167 * @param integer $courseid the course we are importing to.
168 * @return mixed and array of user objects, or false if none.
170 function get_unenrolled_users_in_import($importcode, $courseid) {
171 global $CFG;
172 $relatedctxcondition = get_related_contexts_string(get_context_instance(CONTEXT_COURSE, $courseid));
174 $sql = "SELECT giv.id, u.firstname, u.lastname, u.idnumber AS useridnumber,
175 COALESCE(gi.idnumber, gin.itemname) AS gradeidnumber
176 FROM
177 {$CFG->prefix}grade_import_values giv
178 JOIN {$CFG->prefix}user u ON giv.userid = u.id
179 LEFT JOIN {$CFG->prefix}grade_items gi ON gi.id = giv.itemid
180 LEFT JOIN {$CFG->prefix}grade_import_newitem gin ON gin.id = giv.newgradeitem
181 LEFT JOIN {$CFG->prefix}role_assignments ra ON (giv.userid = ra.userid AND
182 ra.roleid IN ($CFG->gradebookroles) AND
183 ra.contextid $relatedctxcondition)
184 WHERE giv.importcode = $importcode
185 AND ra.id IS NULL
186 ORDER BY gradeidnumber, u.lastname, u.firstname";
188 return get_records_sql($sql);
192 * removes entries from grade import buffer tables grade_import_value and grade_import_newitem
193 * after a successful import, or during an import abort
194 * @param string importcode - import batch identifier
196 function import_cleanup($importcode) {
197 global $USER;
199 // remove entries from buffer table
200 delete_records('grade_import_values', 'importcode', $importcode, 'importer', $USER->id);
201 delete_records('grade_import_newitem', 'importcode', $importcode, 'importer', $USER->id);