MDL-29794 Initial support for re-using a shared grading form
[moodle.git] / grade / import / lib.php
CommitLineData
e060e33d 1<?php
ce40b793 2
e060e33d 3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
8ad36f4c 17
8108909a 18require_once($CFG->libdir.'/gradelib.php');
19
20/**
21 * Returns new improtcode for current user
22 * @return int importcode
23 */
24function get_new_importcode() {
5c75a0a3 25 global $USER, $DB;
8108909a 26
27 $importcode = time();
5c75a0a3 28 while ($DB->get_record('grade_import_values', array('importcode' => $importcode, 'importer' => $USER->id))) {
8108909a 29 $importcode--;
30 }
31
32 return $importcode;
33}
34
ce40b793 35/**
ba74762b 36 * given an import code, commits all entries in buffer tables
ce40b793 37 * (grade_import_value and grade_import_newitem)
ba74762b 38 * If this function is called, we assume that all data collected
ce40b793 39 * up to this point is fine and we can go ahead and commit
40 * @param int courseid - id of the course
41 * @param string importcode - import batch identifier
a700b5de 42 * @param feedback print feedback and continue button
43 * @return bool success
ce40b793 44 */
34058207 45function grade_import_commit($courseid, $importcode, $importfeedback=true, $verbose=true) {
c3b834b4 46 global $CFG, $USER, $DB, $OUTPUT;
ba74762b 47
ce40b793 48 $commitstart = time(); // start time in case we need to roll back
49 $newitemids = array(); // array to hold new grade_item ids from grade_import_newitem table, mapping array
ba74762b 50
ce40b793 51 /// first select distinct new grade_items with this batch
5c75a0a3 52 $params = array($importcode, $USER->id);
53 if ($newitems = $DB->get_records_sql("SELECT *
54 FROM {grade_import_newitem}
55 WHERE importcode = ? AND importer=?", $params)) {
ba74762b 56
57 // instances of the new grade_items created, cached
ce40b793 58 // in case grade_update fails, so that we can remove them
59 $instances = array();
e9dea7af 60 $failed = false;
ce40b793 61 foreach ($newitems as $newitem) {
62 // get all grades with this item
ba74762b 63
5c75a0a3 64 if ($grades = $DB->get_records('grade_import_values', array('newgradeitem' => $newitem->id))) {
e9dea7af 65 /// create a new grade item for this - must use false as second param!
66 /// TODO: we need some bounds here too
67 $gradeitem = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual', 'itemname'=>$newitem->itemname), false);
68 $gradeitem->insert('import');
69 $instances[] = $gradeitem;
b4355273 70
71 // insert each individual grade to this new grade item
ba74762b 72 foreach ($grades as $grade) {
0f392ff4 73 if (!$gradeitem->update_final_grade($grade->userid, $grade->finalgrade, 'import', $grade->feedback, FORMAT_MOODLE)) {
e9dea7af 74 $failed = true;
75 break 2;
ce40b793 76 }
b4355273 77 }
ce40b793 78 }
b263e539 79 }
e9dea7af 80
81 if ($failed) {
82 foreach ($instances as $instance) {
83 $gradeitem->delete('import');
84 }
85 import_cleanup($importcode);
86 return false;
87 }
ce40b793 88 }
b263e539 89
ce40b793 90 /// then find all existing items
91
5c75a0a3 92 if ($gradeitems = $DB->get_records_sql("SELECT DISTINCT (itemid)
93 FROM {grade_import_values}
94 WHERE importcode = ? AND importer=? AND itemid > 0",
95 array($importcode, $USER->id))) {
b4355273 96
ce40b793 97 $modifieditems = array();
ba74762b 98
8108909a 99 foreach ($gradeitems as $itemid=>$notused) {
ba74762b 100
b4355273 101 if (!$gradeitem = new grade_item(array('id'=>$itemid))) {
102 // not supposed to happen, but just in case
103 import_cleanup($importcode);
104 return false;
ce40b793 105 }
106 // get all grades with this item
5c75a0a3 107 if ($grades = $DB->get_records('grade_import_values', array('itemid' => $itemid))) {
b263e539 108
0eeada79 109 // make the grades array for update_grade
ce40b793 110 foreach ($grades as $grade) {
34058207 111 if (!$importfeedback) {
112 $grade->feedback = false; // ignore it
113 }
0f392ff4 114 if (!$gradeitem->update_final_grade($grade->userid, $grade->finalgrade, 'import', $grade->feedback)) {
b4355273 115 $failed = 1;
b263e539 116 break 2;
b4355273 117 }
ce40b793 118 }
b263e539 119 //$itemdetails -> idnumber = $gradeitem->idnumber;
120 $modifieditems[] = $itemid;
b4355273 121
b263e539 122 }
ba74762b 123
b4355273 124 if (!empty($failed)) {
125 import_cleanup($importcode);
b263e539 126 return false;
127 }
ce40b793 128 }
129 }
b263e539 130
34058207 131 if ($verbose) {
c3b834b4 132 echo $OUTPUT->notification(get_string('importsuccess', 'grades'), 'notifysuccess');
7d69de18 133 $unenrolledusers = get_unenrolled_users_in_import($importcode, $courseid);
134 if ($unenrolledusers) {
5be262b6 135 $list = array();
7d69de18 136 foreach ($unenrolledusers as $u) {
137 $u->fullname = fullname($u);
5be262b6 138 $list[] = get_string('usergrade', 'grades', $u);
7d69de18 139 }
5be262b6 140 echo $OUTPUT->notification(get_string('unenrolledusersinimport', 'grades', html_writer::alist($list)), 'notifysuccess');
7d69de18 141 }
c3b834b4 142 echo $OUTPUT->continue_button($CFG->wwwroot.'/grade/index.php?id='.$courseid);
a700b5de 143 }
ce40b793 144 // clean up
145 import_cleanup($importcode);
a700b5de 146
147 return true;
ce40b793 148}
149
7d69de18 150/**
151 * This function returns an array of grades that were included in the import,
6ef4878b 152 * but where the user does not currently have a graded role on the course. These grades
7d69de18 153 * are still stored in the database, but will not be visible in the gradebook unless
154 * this user subsequently enrols on the course in a graded roles.
155 *
156 * The returned objects have fields user firstname, lastname and useridnumber, and gradeidnumber.
157 *
158 * @param integer $importcode import batch identifier
159 * @param integer $courseid the course we are importing to.
160 * @return mixed and array of user objects, or false if none.
161 */
162function get_unenrolled_users_in_import($importcode, $courseid) {
5c75a0a3 163 global $CFG, $DB;
7d69de18 164 $relatedctxcondition = get_related_contexts_string(get_context_instance(CONTEXT_COURSE, $courseid));
d24832f9 165
b61e4f46 166 //users with a gradeable role
cf717dc2 167 list($gradebookrolessql, $gradebookrolesparams) = $DB->get_in_or_equal(explode(',', $CFG->gradebookroles), SQL_PARAMS_NAMED, 'grbr');
b61e4f46 168
169 //enrolled users
2dc5e584 170 $context = get_context_instance(CONTEXT_COURSE, $courseid);
171 list($enrolledsql, $enrolledparams) = get_enrolled_sql($context);
5c75a0a3 172
d24832f9 173 $sql = "SELECT giv.id, u.firstname, u.lastname, u.idnumber AS useridnumber,
25081b95
SH
174 COALESCE(gi.idnumber, gin.itemname) AS gradeidnumber
175 FROM {grade_import_values} giv
176 JOIN {user} u
177 ON giv.userid = u.id
178 LEFT JOIN {grade_items} gi
179 ON gi.id = giv.itemid
180 LEFT JOIN {grade_import_newitem} gin
181 ON gin.id = giv.newgradeitem
182 LEFT JOIN ($enrolledsql) je
183 ON je.id = u.id
184 LEFT JOIN {role_assignments} ra
185 ON (giv.userid = ra.userid AND ra.roleid $gradebookrolessql AND ra.contextid $relatedctxcondition)
186 WHERE giv.importcode = :importcode
187 AND (ra.id IS NULL OR je.id IS NULL)
188 ORDER BY gradeidnumber, u.lastname, u.firstname";
2dc5e584 189 $params = array_merge($gradebookrolesparams, $enrolledparams);
f2491a11 190 $params['importcode'] = $importcode;
7d69de18 191
2dc5e584 192 return $DB->get_records_sql($sql, $params);
7d69de18 193}
194
ce40b793 195/**
196 * removes entries from grade import buffer tables grade_import_value and grade_import_newitem
197 * after a successful import, or during an import abort
198 * @param string importcode - import batch identifier
199 */
200function import_cleanup($importcode) {
5c75a0a3 201 global $USER, $DB;
8108909a 202
b263e539 203 // remove entries from buffer table
5c75a0a3 204 $DB->delete_records('grade_import_values', array('importcode' => $importcode, 'importer' => $USER->id));
205 $DB->delete_records('grade_import_newitem', array('importcode' => $importcode, 'importer' => $USER->id));
ce40b793 206}
741bde47 207
6c3ef410 208