Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/grade/import/lib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 require_once($CFG->libdir.'/gradelib.php');
00019 
00024 function get_new_importcode() {
00025     global $USER, $DB;
00026 
00027     $importcode = time();
00028     while ($DB->get_record('grade_import_values', array('importcode' => $importcode, 'importer' => $USER->id))) {
00029         $importcode--;
00030     }
00031 
00032     return $importcode;
00033 }
00034 
00045 function grade_import_commit($courseid, $importcode, $importfeedback=true, $verbose=true) {
00046     global $CFG, $USER, $DB, $OUTPUT;
00047 
00048     $commitstart = time(); // start time in case we need to roll back
00049     $newitemids = array(); // array to hold new grade_item ids from grade_import_newitem table, mapping array
00050 
00052     $params = array($importcode, $USER->id);
00053     if ($newitems = $DB->get_records_sql("SELECT *
00054                                            FROM {grade_import_newitem}
00055                                           WHERE importcode = ? AND importer=?", $params)) {
00056 
00057         // instances of the new grade_items created, cached
00058         // in case grade_update fails, so that we can remove them
00059         $instances = array();
00060         $failed = false;
00061         foreach ($newitems as $newitem) {
00062             // get all grades with this item
00063 
00064             if ($grades = $DB->get_records('grade_import_values', array('newgradeitem' => $newitem->id))) {
00067                 $gradeitem = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual', 'itemname'=>$newitem->itemname), false);
00068                 $gradeitem->insert('import');
00069                 $instances[] = $gradeitem;
00070 
00071                 // insert each individual grade to this new grade item
00072                 foreach ($grades as $grade) {
00073                     if (!$gradeitem->update_final_grade($grade->userid, $grade->finalgrade, 'import', $grade->feedback, FORMAT_MOODLE)) {
00074                         $failed = true;
00075                         break 2;
00076                     }
00077                 }
00078             }
00079         }
00080 
00081         if ($failed) {
00082             foreach ($instances as $instance) {
00083                 $gradeitem->delete('import');
00084             }
00085             import_cleanup($importcode);
00086             return false;
00087         }
00088     }
00089 
00091 
00092     if ($gradeitems = $DB->get_records_sql("SELECT DISTINCT (itemid)
00093                                              FROM {grade_import_values}
00094                                             WHERE importcode = ? AND importer=? AND itemid > 0",
00095                                             array($importcode, $USER->id))) {
00096 
00097         $modifieditems = array();
00098 
00099         foreach ($gradeitems as $itemid=>$notused) {
00100 
00101             if (!$gradeitem = new grade_item(array('id'=>$itemid))) {
00102                 // not supposed to happen, but just in case
00103                 import_cleanup($importcode);
00104                 return false;
00105             }
00106             // get all grades with this item
00107             if ($grades = $DB->get_records('grade_import_values', array('itemid' => $itemid))) {
00108 
00109                 // make the grades array for update_grade
00110                 foreach ($grades as $grade) {
00111                     if (!$importfeedback) {
00112                         $grade->feedback = false; // ignore it
00113                     }
00114                     if (!$gradeitem->update_final_grade($grade->userid, $grade->finalgrade, 'import', $grade->feedback)) {
00115                         $failed = 1;
00116                         break 2;
00117                     }
00118                 }
00119                 //$itemdetails -> idnumber = $gradeitem->idnumber;
00120                 $modifieditems[] = $itemid;
00121 
00122             }
00123 
00124             if (!empty($failed)) {
00125                 import_cleanup($importcode);
00126                 return false;
00127             }
00128         }
00129     }
00130 
00131     if ($verbose) {
00132         echo $OUTPUT->notification(get_string('importsuccess', 'grades'), 'notifysuccess');
00133         $unenrolledusers = get_unenrolled_users_in_import($importcode, $courseid);
00134         if ($unenrolledusers) {
00135             $list = array();
00136             foreach ($unenrolledusers as $u) {
00137                 $u->fullname = fullname($u);
00138                 $list[] = get_string('usergrade', 'grades', $u);
00139             }
00140             echo $OUTPUT->notification(get_string('unenrolledusersinimport', 'grades', html_writer::alist($list)), 'notifysuccess');
00141         }
00142         echo $OUTPUT->continue_button($CFG->wwwroot.'/grade/index.php?id='.$courseid);
00143     }
00144     // clean up
00145     import_cleanup($importcode);
00146 
00147     return true;
00148 }
00149 
00162 function get_unenrolled_users_in_import($importcode, $courseid) {
00163     global $CFG, $DB;
00164     $relatedctxcondition = get_related_contexts_string(get_context_instance(CONTEXT_COURSE, $courseid));
00165 
00166     //users with a gradeable role
00167     list($gradebookrolessql, $gradebookrolesparams) = $DB->get_in_or_equal(explode(',', $CFG->gradebookroles), SQL_PARAMS_NAMED, 'grbr');
00168 
00169     //enrolled users
00170     $context = get_context_instance(CONTEXT_COURSE, $courseid);
00171     list($enrolledsql, $enrolledparams) = get_enrolled_sql($context);
00172 
00173     $sql = "SELECT giv.id, u.firstname, u.lastname, u.idnumber AS useridnumber,
00174                    COALESCE(gi.idnumber, gin.itemname) AS gradeidnumber
00175               FROM {grade_import_values} giv
00176               JOIN {user} u
00177                    ON giv.userid = u.id
00178               LEFT JOIN {grade_items} gi
00179                         ON gi.id = giv.itemid
00180               LEFT JOIN {grade_import_newitem} gin
00181                         ON gin.id = giv.newgradeitem
00182               LEFT JOIN ($enrolledsql) je
00183                         ON je.id = u.id
00184               LEFT JOIN {role_assignments} ra
00185                         ON (giv.userid = ra.userid AND ra.roleid $gradebookrolessql AND ra.contextid $relatedctxcondition)
00186              WHERE giv.importcode = :importcode
00187                    AND (ra.id IS NULL OR je.id IS NULL)
00188           ORDER BY gradeidnumber, u.lastname, u.firstname";
00189     $params = array_merge($gradebookrolesparams, $enrolledparams);
00190     $params['importcode'] = $importcode;
00191 
00192     return $DB->get_records_sql($sql, $params);
00193 }
00194 
00200 function import_cleanup($importcode) {
00201     global $USER, $DB;
00202 
00203     // remove entries from buffer table
00204     $DB->delete_records('grade_import_values', array('importcode' => $importcode, 'importer' => $USER->id));
00205     $DB->delete_records('grade_import_newitem', array('importcode' => $importcode, 'importer' => $USER->id));
00206 }
00207 
00208 
 All Data Structures Namespaces Files Functions Variables Enumerations