]> git.mjollnir.org Git - moodle.git/commitdiff
added 4 new functions for Moodle 1.9 gradebook: "hotpot_get_user_grades", "hotpot_upd...
authorgbateson <gbateson>
Tue, 2 Oct 2007 07:46:49 +0000 (07:46 +0000)
committergbateson <gbateson>
Tue, 2 Oct 2007 07:46:49 +0000 (07:46 +0000)
mod/hotpot/attempt.php
mod/hotpot/lib.php
mod/hotpot/report.php

index 50cc54044ae2009229d8085c2937b27b670153a3..790d92b9b32f20290baa3fe093a989752eaafa03 100644 (file)
         error("Could not update attempt record: ".$db->ErrorMsg(), $next_url);
     }
 
+    // update grades for this user
+    hotpot_update_grades($hotpot, $attempt->userid);
+
     // get previous attempt details record, if any
     $details_exist = record_exists("hotpot_details", "attempt", $attempt->id);
 
index a5d4ce199923943bc47de662fafcbf69952f492d..2b02b0da94117f8fd67e983a32473954af24336c 100644 (file)
@@ -199,6 +199,7 @@ function hotpot_add_instance(&$hotpot) {
         if ($result = insert_record('hotpot', $hotpot)) {
             $hotpot->id = $result;
             hotpot_update_events($hotpot);
+            hotpot_grade_item_update(stripslashes_recursive($hotpot));
         }
     } else {
         $result=  false;
@@ -211,6 +212,7 @@ function hotpot_update_instance(&$hotpot) {
         $hotpot->id = $hotpot->instance;
         if ($result = update_record('hotpot', $hotpot)) {
             hotpot_update_events($hotpot);
+            hotpot_grade_item_update(stripslashes_recursive($hotpot));
         }
     } else {
         $result=  false;
@@ -323,18 +325,20 @@ function hotpot_set_form_values(&$hotpot) {
         hotpot_set_name_summary_reference($hotpot);
     }
 
-    switch ($hotpot->displaynext) {
-        // N.B. redirection only works for Moodle 1.5+
-        case HOTPOT_DISPLAYNEXT_COURSE:
-            $hotpot->redirect = true;
-            $hotpot->redirecturl = "view.php?id=$hotpot->course";
-            break;
-        case HOTPOT_DISPLAYNEXT_INDEX:
-            $hotpot->redirect = true;
-            $hotpot->redirecturl = "../mod/hotpot/index.php?id=$hotpot->course";
-            break;
-        default:
-            // use Moodle default action (i.e. go on to display the hotpot quiz)
+    if (isset($hotpot->displaynext)) {
+        switch ($hotpot->displaynext) {
+            // N.B. redirection only works for Moodle 1.5+
+            case HOTPOT_DISPLAYNEXT_COURSE:
+                $hotpot->redirect = true;
+                $hotpot->redirecturl = "view.php?id=$hotpot->course";
+                break;
+            case HOTPOT_DISPLAYNEXT_INDEX:
+                $hotpot->redirect = true;
+                $hotpot->redirecturl = "../mod/hotpot/index.php?id=$hotpot->course";
+                break;
+            default:
+                // use Moodle default action (i.e. go on to display the hotpot quiz)
+        }
     }
 
     // if ($ok && $hotpot->setdefaults) {
@@ -855,20 +859,29 @@ function hotpot_delete_instance($id) {
 /// this function will permanently delete the instance
 /// and any data that depends on it.
 
-    $result = false;
-    if (delete_records("hotpot", "id", "$id")) {
-        $result = true;
-        delete_records("hotpot_questions", "hotpot", "$id");
-        if ($attempts = get_records_select("hotpot_attempts", "hotpot='$id'")) {
-            $ids = implode(',', array_keys($attempts));
-            delete_records_select("hotpot_attempts",  "id IN ($ids)");
-            delete_records_select("hotpot_details",   "attempt IN ($ids)");
-            delete_records_select("hotpot_responses", "attempt IN ($ids)");
-        }
-         // remove calendar events for this hotpot
-        delete_records('event', 'modulename', 'hotpot', 'instance', $id);
-   }
-    return $result;
+    if (! $hotpot = get_record("hotpot", "id", $id)) {
+        return false;
+    }
+
+    if (! delete_records("hotpot", "id", "$id")) {
+        return false;
+    }
+
+    delete_records("hotpot_questions", "hotpot", "$id");
+    if ($attempts = get_records_select("hotpot_attempts", "hotpot='$id'")) {
+        $ids = implode(',', array_keys($attempts));
+        delete_records_select("hotpot_attempts",  "id IN ($ids)");
+        delete_records_select("hotpot_details",   "attempt IN ($ids)");
+        delete_records_select("hotpot_responses", "attempt IN ($ids)");
+    }
+
+     // remove calendar events for this hotpot
+    delete_records('event', 'modulename', 'hotpot', 'instance', $id);
+
+     // remove grade item for this hotpot
+    hotpot_grade_item_delete($hotpot);
+
+    return true;
 }
 function hotpot_delete_and_notify($table, $select, $strtable) {
     $count = max(0, count_records_select($table, $select));
@@ -1181,6 +1194,107 @@ function hotpot_get_precision(&$hotpot) {
     return ($hotpot->grademethod==HOTPOT_GRADEMETHOD_AVERAGE || $hotpot->grade<100) ? 1 : 0;
 }
 
+/**
+ * Return grade for given user or all users.
+ *
+ * @param object $hotpot
+ * @param int $userid optional user id, 0 means all users
+ * @return array array of grades, false if none
+ */
+function hotpot_get_user_grades($hotpot, $userid=0) {
+    $grades = array();
+    if ($hotpotgrades = hotpot_get_grades($hotpot, $userid)) {
+        foreach ($hotpotgrades as $hotpotuserid => $hotpotgrade) {
+            $grades[$hotpotuserid] = new stdClass();
+            $grades[$hotpotuserid]->id        = $hotpotuserid;
+            $grades[$hotpotuserid]->userid    = $hotpotuserid;
+            $grades[$hotpotuserid]->rawgrade  = $hotpotgrade;
+        }
+    }
+    if (count($grades)) {
+        return $grades;
+    } else {
+        return false;
+    }
+}
+/**
+ * Update grades in central gradebook
+ *
+ * @param object $hotpot null means all hotpots
+ * @param int $userid specific user only, 0 mean all
+ */
+function hotpot_update_grades($hotpot=null, $userid=0, $nullifnone=true) {
+    global $CFG;
+    if (! function_exists('grade_update')) { //workaround for buggy PHP versions
+        require_once($CFG->libdir.'/gradelib.php');
+    }
+    if ($hotpot) {
+        if ($grades = hotpot_get_user_grades($hotpot, $userid)) {
+            grade_update('mod/hotpot', $hotpot->course, 'mod', 'hotpot', $hotpot->id, 0, $grades);
+
+        } else if ($userid && $nullifnone) {
+            $grade = new object();
+            $grade->userid   = $userid;
+            $grade->rawgrade = null;
+            grade_update('mod/hotpot', $hotpot->course, 'mod', 'hotpot', $hotpot->id, 0, $grade);
+        }
+    } else {
+        $sql = "SELECT h.*, cm.idnumber as cmidnumber
+                  FROM {$CFG->prefix}hotpot h, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
+                 WHERE m.name='hotpot' AND m.id=cm.module AND cm.instance=s.id";
+        if ($rs = get_recordset_sql($sql)) {
+            if ($rs->RecordCount() > 0) {
+                while ($hotpot = rs_fetch_next_record($rs)) {
+                    hotpot_grade_item_update($hotpot);
+                    hotpot_update_grades($hotpot, 0, false);
+                }
+            }
+            rs_close($rs);
+        }
+    }
+}
+/**
+ * Update/create grade item for given hotpot
+ *
+ * @param object $hotpot object with extra cmidnumber
+ * @return object grade_item
+ */
+function hotpot_grade_item_update($hotpot) {
+    global $CFG;
+    if (!function_exists('grade_update')) { //workaround for buggy PHP versions
+        require_once($CFG->libdir.'/gradelib.php');
+    }
+
+    $params = array('itemname' => $hotpot->name);
+    if (array_key_exists('cmidnumber', $hotpot)) {
+        //cmidnumber may not be always present
+        $params['idnumber'] = $hotpot->cmidnumber;
+    }
+
+    if ($hotpot->grade > 0) {
+        $params['gradetype'] = GRADE_TYPE_VALUE;
+        $params['grademax']  = $hotpot->grade;
+        $params['grademin']  = 0;
+
+    } else {
+        $params['gradetype'] = GRADE_TYPE_NONE;
+    }
+
+    return grade_update('mod/hotpot', $hotpot->course, 'mod', 'hotpot', $hotpot->id, 0, NULL, $params);
+}
+/**
+ * Delete grade item for given hotpot
+ *
+ * @param object $hotpot object
+ * @return object grade_item
+ */
+function hotpot_grade_item_delete($hotpot) {
+    global $CFG;
+    require_once($CFG->libdir.'/gradelib.php');
+
+    return grade_update('mod/hotpot', $hotpot->course, 'mod', 'hotpot', $hotpot->id, 0, NULL, array('deleted'=>1));
+}
+
 function hotpot_get_participants($hotpotid) {
 //Must return an array of user ids who are participants
 //for a given instance of hotpot. Must include every user involved
index 681fcfbabcb8453fe93bd37f156d4a531704cb41..32e2eb1f60294f61066a1d0cd6d8932115a640b5 100644 (file)
@@ -386,6 +386,9 @@ function hotpot_delete_selected_attempts(&$hotpot, $del) {
             $select = 'attempt IN ('.implode(',', array_keys($attempts)).')';
             hotpot_delete_and_notify('hotpot_details', $select, get_string('rawdetails', 'hotpot'));
             hotpot_delete_and_notify('hotpot_responses', $select, get_string('answer', 'quiz'));
+
+            // update grades for all users for this hotpot
+            hotpot_update_grades($hotpot);
         }
     }