]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-9516 update_grade(); now required $source of grading to allow modification of...
authorskodak <skodak>
Wed, 6 Jun 2007 23:04:24 +0000 (23:04 +0000)
committerskodak <skodak>
Wed, 6 Jun 2007 23:04:24 +0000 (23:04 +0000)
grade/import/xml/index.php
lib/db/events.php
lib/db/install.xml
lib/gradelib.php
mod/assignment/db/events.php
mod/assignment/lib.php
mod/assignment/version.php
mod/data/lib.php
mod/forum/lib.php
mod/glossary/lib.php

index 4111ae0eeb361d16da905c64a76fd5e4283d5444..36ce58859b1101c7734643405b85d5aedebfee7f 100755 (executable)
@@ -60,8 +60,10 @@ if ( $formdata = $mform->get_data() ) {
             $eventdata->userid = $result['#']['student'][0]['#'];
             $eventdata->gradevalue = $result['#']['score'][0]['#'];
             
+            /* TODO: use grade_update() instead
             trigger_event('grade_updated_external', $eventdata);           
-            echo "<br/>triggering event for $eventdata->idnumber... student id is $eventdata->userid and grade is $eventdata->gradevalue";          
+            echo "<br/>triggering event for $eventdata->idnumber... student id is $eventdata->userid and grade is $eventdata->gradevalue";
+            */          
         }      
     }    
   
index 5def23b9d70e6b513e7468eae37eacb34e5970e4..d4a782d25fca97e74a35c8e0de607a5e5fe24596 100644 (file)
 ///////////////////////////////////////////////////////////////////////////
 
 
-$handlers = array (
-/*
- * Grades created/modified outside of activities (import, gradebook overrides, etc.)
- * 
- * required parameters (object or array):
- *  idnumber       - idnumber from grade_items table
- *  userid         - each grade must be associated with existing user
- *
- * optional params:
- *  gradevalue     - raw grade value
- *  feedback       - graders feedback
- *  feedbackformat - text format of the feedback
- */
-   'grade_update_request' => array (
-        'handlerfile'      => '/lib/gradelib.php',
-        'handlerfunction'  => 'grade_handler', 
-        'schedule'         => 'instant'
-    )
-);
+$handlers = array (); // no handlers for now in core
 
 ?>
index 7634a0fd62dd109c793112e888754fe13304935a..cd11c8a7ec3c73df9504f31b68b0c69486311cf6 100644 (file)
         <INDEX NAME="mnethostid_username" UNIQUE="true" FIELDS="mnet_host_id, username"/>
       </INDEXES>
     </TABLE>
-    <TABLE NAME="events_handlers" COMMENT="This table is for storing which components requests what type of event, and the location of the responsible handlers. For example, the grade book can register 'grade_updated' event with a function grade_handler() that should be called event time an 'grade_updated' event is triggered by a module." PREVIOUS="mnet_sso_access_control" NEXT="events_queue">
+    <TABLE NAME="events_handlers" COMMENT="This table is for storing which components requests what type of event, and the location of the responsible handlers. For example, the assignment registers 'grade_updated' event with a function assignment_grade_handler() that should be called event time an 'grade_updated' event is triggered by grade_update() function." PREVIOUS="mnet_sso_access_control" NEXT="events_queue">
       <FIELDS>
         <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="true" ENUM="false" COMMENT="id of the table, please edit me" NEXT="eventname"/>
         <FIELD NAME="eventname" TYPE="char" LENGTH="166" NOTNULL="true" SEQUENCE="false" ENUM="false" COMMENT="name of the event, e.g. 'grade_updated'" PREVIOUS="id" NEXT="handlermodule"/>
index 34b1b2ada0b831db70f186dfb01792d2995a4d54..40c62c309f0f8ce321e1dd177fd0d8cd6adfbfba 100644 (file)
@@ -69,7 +69,7 @@ require_once($CFG->libdir . '/grade/grade_tree.php');
 
 /***** PUBLIC GRADE API *****/
 
-function grade_update($courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grade=NULL, $itemdetails=NULL) {
+function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grades=NULL, $itemdetails=NULL) {
 
     // only following grade_item properties can be changed/used in this function
     $allowed = array('itemname', 'idnumber', 'gradetype', 'grademax', 'grademin', 'scaleid', 'deleted');
@@ -146,7 +146,7 @@ function grade_update($courseid, $itemtype, $itemmodule, $iteminstance, $itemnum
     }
 
     // no grade submitted
-    if (empty($grade)) {
+    if (empty($grades)) {
         return GRADE_UPDATE_OK;
     }
 
@@ -157,18 +157,14 @@ function grade_update($courseid, $itemtype, $itemmodule, $iteminstance, $itemnum
     }
 
 /// Finally start processing of grades
-    if (is_object($grade)) {
-        $grades = array($grade);
+    if (is_object($grades)) {
+        $grades = array($grades);
     } else {
-        if (array_key_exists('userid', $grade)) {
-            $grades = array($grade);
-        } else {
-            $grades = $grade;
+        if (array_key_exists('userid', $grades)) {
+            $grades = array($grades);
         }
     }
 
-    unset($grade);
-
     foreach ($grades as $grade) {
         $grade = (array)$grade;
         if (empty($grade['userid'])) {
@@ -205,16 +201,46 @@ function grade_update($courseid, $itemtype, $itemmodule, $iteminstance, $itemnum
             $rawgrade->insert();
         }
 
-        //trigger grade_updated event notification
+        // trigger grade_updated event notification
         $eventdata = new object();
-        $eventdata->itemid = $grade_item->id;
-        $eventdata->grade  = $grade;
+        $eventdata->source       = $source;
+        $eventdata->itemid       = $grade_item->id;
+        $eventdata->courseid     = $grade_item->courseid;
+        $eventdata->itemtype     = $grade_item->itemtype;
+        $eventdata->itemmodule   = $grade_item->itemmodule;
+        $eventdata->iteminstance = $grade_item->iteminstance;
+        $eventdata->itemnumber    = $grade_item->itemnumber;
+        $eventdata->idnumber     = $grade_item->idnumber;
+        $eventdata->grade        = $grade;
         events_trigger('grade_updated', $eventdata);
     }
 
     return GRADE_UPDATE_OK;
 }
 
+
+/**
+* Tells a module whether a grade (or grade_item if $userid is not given) is currently locked or not.
+* This is a combination of the actual settings in the grade tables and a check on moodle/course:editgradeswhenlocked.
+* If it's locked to the current use then the module can print a nice message or prevent editing in the module.
+* If no $userid is given, the method will always return the grade_item's locked state.
+* If a $userid is given, the method will first check the grade_item's locked state (the column). If it is locked,
+* the method will return true no matter the locked state of the specific grade being checked. If unlocked, it will
+* return the locked state of the specific grade.
+*
+* @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc
+* @param string $itemmodule 'forum, 'quiz', 'csv' etc
+* @param int $iteminstance id of the item module
+* @param int $itemnumber Optional number of the item to check
+* @param int $userid ID of the user who owns the grade
+* @return boolean Whether the grade is locked or not
+*/
+function grade_is_locked($itemtype, $itemmodule, $iteminstance, $itemnumber=NULL, $userid=NULL) {
+    $grade_item = new grade_item(compact('itemtype', 'itemmodule', 'iteminstance', 'itemnumber'));
+    return $grade_item->is_locked($userid);
+}
+
+
 /***** END OF PUBLIC API *****/
 
 /**
@@ -281,28 +307,6 @@ function grade_create_category($courseid, $fullname, $items, $aggregation=GRADE_
     }
 }
 
-
-/**
-* Tells a module whether a grade (or grade_item if $userid is not given) is currently locked or not.
-* This is a combination of the actual settings in the grade tables and a check on moodle/course:editgradeswhenlocked.
-* If it's locked to the current use then the module can print a nice message or prevent editing in the module.
-* If no $userid is given, the method will always return the grade_item's locked state.
-* If a $userid is given, the method will first check the grade_item's locked state (the column). If it is locked,
-* the method will return true no matter the locked state of the specific grade being checked. If unlocked, it will
-* return the locked state of the specific grade.
-*
-* @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc
-* @param string $itemmodule 'forum, 'quiz', 'csv' etc
-* @param int $iteminstance id of the item module
-* @param int $itemnumber Optional number of the item to check
-* @param int $userid ID of the user who owns the grade
-* @return boolean Whether the grade is locked or not
-*/
-function grade_is_locked($itemtype, $itemmodule, $iteminstance, $itemnumber=NULL, $userid=NULL) {
-    $grade_item = new grade_item(compact('itemtype', 'itemmodule', 'iteminstance', 'itemnumber'));
-    return $grade_item->is_locked($userid);
-}
-
 /**
  * Updates all grade_grades_final for each grade_item matching the given attributes.
  * The search is further restricted, so that only grade_items that have needs_update == TRUE
@@ -518,71 +522,4 @@ function standardise_score($gradevalue, $source_min, $source_max, $target_min, $
 }
 
 
-/**
- * Handles some specific grade_update_request events,
- * see lib/db/events.php for description of $eventdata format.
- *
- * @param object $eventdata contains all the data for the event
- * @return boolean success
- *
- */
-function grade_handler($eventdata) {
-    $eventdata = (array)$eventdata;
-
-    // each grade must belong to some user
-    if (empty($eventdata['userid'])) {
-        debugging('Missing user id in event data!');
-        return true;
-    }
-
-    // grade item idnumber must be specified or else it could be accidentally duplicated,
-    if (empty($eventdata['idnumber'])) {
-        debugging('Missing grade item idnumber in event!');
-        return true;
-    }
-
-    // get the grade item from db
-    $grade_item = new grade_item(array('idnumber'=>$eventdata['idnumber']), false);
-    if (!$grade_items = $grade_item->fetch_all_using_this()) {
-        // TODO: create a new one - tricky, watch out for duplicates !!
-        //       use only special type 'import'(y) or 'manual'(?)
-        debugging('Can not create new grade items yet :-(');
-        return true;
-
-    } else if (count($grade_items) == 1) {
-        $grade_item = reset($grade_items);
-        unset($grade_items); //release memory
-
-    } else {
-        debugging('More than one grade item matched, grade update request failed');
-        return true;
-    }
-
-    // !! TODO: whitelist only some types such as 'import'(?) 'manual'(?) and ignore the rest!!
-    if ($grade_item->itemtype == 'mod') {
-        // modules must have own handlers for grade update requests
-        return true;
-    }
-
-    $grade = new object();
-    $grade->userid = $eventdata['userid'];
-    if (isset($eventdata['gradevalue'])) {
-        $grade->feedback = $eventdata['gradevalue'];
-    }
-    if (isset($eventdata['feedback'])) {
-        $grade->feedback = $eventdata['feedback'];
-    }
-    if (isset($eventdata['feedbackformat'])) {
-        $grade->feedbackformat = $eventdata['feedbackformat'];
-    }
-
-    grade_update($grade_item->courseid, $grade_item->itemtype, $grade_item->itemmodule,
-                 $grade_item->iteminstance, $grade_item->itemnumber, $grade, $eventdata);
-
-    // everything ok :-)
-    return true;
-
-}
-
-
 ?>
index bdc56107401bbaa3761f39e7675b73e33e981a85..2fdb74ea9478925b1ebda89aa76a50ec7ab23ea6 100644 (file)
 
 $handlers = array (
 
-/*
- * Grades created/modified outside of activities (import, gradebook overrides, etc.)
- * see description in lib/db/events.php
- */
-    'grade_updated_external' => array (
+ /*
+  * Grade created or modified notification.
+  * See event format in lib/gradelib.php function update_grade()
 */
+    'grade_updated' => array (
         'handlerfile'      => '/mod/assignment/lib.php',
-        'handlerfunction'  => 'assignment_grade_update_handler',
+        'handlerfunction'  => 'assignment_grade_handler',
         'schedule'         => 'instant'
     )
 );
index 95b40f9c1d78108545915f15fe61febabb5d795f..a123823808b2b66a66fdf7db4c5615f930bf4276 100644 (file)
@@ -1843,7 +1843,7 @@ function assignment_update_grades($assignment=null, $userid=0, $nullifnone=true)
                     $grades[$k]->gradevalue = null;
                 }
             }
-            grade_update($assignment->courseid, 'mod', 'assignment', $assignment->id, 0, $grades);
+            grade_update('mod/assignment', $assignment->courseid, 'mod', 'assignment', $assignment->id, 0, $grades);
         }
 
     } else {
@@ -1895,7 +1895,7 @@ function assignment_grade_item_update($assignment) {
         $params['gradetype'] = GRADE_TYPE_NONE;
     }
 
-    return grade_update($assignment->courseid, 'mod', 'assignment', $assignment->id, 0, NULL, $params);
+    return grade_update('mod/assignment', $assignment->courseid, 'mod', 'assignment', $assignment->id, 0, NULL, $params);
 }
 
 /**
@@ -1912,19 +1912,18 @@ function assignment_grade_item_delete($assignment) {
         $assignment->courseid = $assignment->course;
     }
 
-    return grade_update($assignment->courseid, 'mod', 'assignment', $assignment->id, 0, NULL, array('deleted'=>1));
+    return grade_update('mod/assignment', $assignment->courseid, 'mod', 'assignment', $assignment->id, 0, NULL, array('deleted'=>1));
 }
 
 /**
- * Something wants to change the grade from outside using "grade_updated_external" event.
- * Final static method - do not override!
+ * Something wants to change the grade from outside using "grade_updated" event.
  *
- * see eventdata description in lib/db/events.php
  */
-function assignment_grade_update_handler($eventdata) {
+function assignment_grade_handler($eventdata) {
     global $CFG, $USER;
 
     //TODO: ...
+    // check source to prevent infinite loops ;-)
 
     return true;
 }
index 01272114fa6a0519ae4b875715cc38c876d93029..bf82d41dc2de6ce59e3c6e3e2910bee201c6841d 100644 (file)
@@ -5,7 +5,7 @@
 //  This fragment is called by /admin/index.php
 ////////////////////////////////////////////////////////////////////////////////
 
-$module->version  = 2007060600;
+$module->version  = 2007060700;
 $module->requires = 2007052800;  // Requires this Moodle version
 $module->cron     = 60;
 
index ef10f9afb61b29ca287b14d78e4c4e5cf724c796..a5c92e9cf43e0b7b94511616d39cdee1d185ab95 100755 (executable)
@@ -769,14 +769,14 @@ function data_update_grades($data=null, $userid=0, $nullifnone=true) {
 
     if ($data != null) {
         if ($grades = data_get_user_grades($data, $userid)) {
-            grade_update($data->course, 'mod', 'data', $data->id, 0, $grades);
+            grade_update('mod/data', $data->course, 'mod', 'data', $data->id, 0, $grades);
 
         } else if ($userid and $nullifnone) {
             $grade = new object();
             $grade->itemid     = $data->id;
             $grade->userid     = $userid;
             $grade->gradevalue = NULL;
-            grade_update($data->course, 'mod', 'data', $data->id, 0, $grade);
+            grade_update('mod/data', $data->course, 'mod', 'data', $data->id, 0, $grade);
         }
 
     } else {
@@ -824,7 +824,7 @@ function data_grade_item_update($data) {
         $params['scaleid']   = -$data->scale;
     }
 
-    return grade_update($data->course, 'mod', 'data', $data->id, 0, NULL, $params);
+    return grade_update('mod/data', $data->course, 'mod', 'data', $data->id, 0, NULL, $params);
 }
 
 /**
@@ -837,7 +837,7 @@ function data_grade_item_delete($data) {
     global $CFG;
     require_once($CFG->libdir.'/gradelib.php');
 
-    return grade_update($data->course, 'mod', 'data', $data->id, 0, NULL, array('deleted'=>1));
+    return grade_update('mod/data', $data->course, 'mod', 'data', $data->id, 0, NULL, array('deleted'=>1));
 }
 
 /************************************************************************
index 426f3a4a68dbd72f5e95c87600e68350be6d4af6..a0303bdb6d2df9b3c32113ea5bdcd76d740b0af8 100644 (file)
@@ -1134,14 +1134,14 @@ function forum_update_grades($forum=null, $userid=0, $nullifnone=true) {
 
     if ($forum != null) {
         if ($grades = forum_get_user_grades($forum, $userid)) {
-            grade_update($forum->course, 'mod', 'forum', $forum->id, 0, $grades);
+            grade_update('mod/forum', $forum->course, 'mod', 'forum', $forum->id, 0, $grades);
 
         } else if ($userid and $nullifnone) {
             $grade = new object();
             $grade->itemid     = $forum->id;
             $grade->userid     = $userid;
             $grade->gradevalue = NULL;
-            grade_update($data->course, 'mod', 'forum', $forum->id, 0, $grade);
+            grade_update('mod/forum', $data->course, 'mod', 'forum', $forum->id, 0, $grade);
         }
 
     } else {
@@ -1189,7 +1189,7 @@ function forum_grade_item_update($forum) {
         $params['scaleid']   = -$forum->scale;
     }
 
-    return grade_update($forum->course, 'mod', 'forum', $forum->id, 0, NULL, $params);
+    return grade_update('mod/forum', $forum->course, 'mod', 'forum', $forum->id, 0, NULL, $params);
 }
 
 /**
@@ -1202,7 +1202,7 @@ function forum_grade_item_delete($forum) {
     global $CFG;
     require_once($CFG->libdir.'/gradelib.php');
 
-    return grade_update($forum->course, 'mod', 'forum', $forum->id, 0, NULL, array('deleted'=>1));
+    return grade_update('mod/forum', $forum->course, 'mod', 'forum', $forum->id, 0, NULL, array('deleted'=>1));
 }
 
 
index faea0816dec6ed4fff91367f625add54b9a5ed73..7147c130af7b6f1f1f0f4b185e27d3a2d8ac5bcd 100644 (file)
@@ -344,14 +344,14 @@ function glossary_update_grades($glossary=null, $userid=0, $nullifnone=true) {
 
     if ($glossary != null) {
         if ($grades = glossary_get_user_grades($glossary, $userid)) {
-            grade_update($glossary->course, 'mod', 'glossary', $glossary->id, 0, $grades);
+            grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, $grades);
 
         } else if ($userid and $nullifnone) {
             $grade = new object();
             $grade->itemid     = $glossary->id;
             $grade->userid     = $userid;
             $grade->gradevalue = NULL;
-            grade_update($glossary->course, 'mod', 'glossary', $glossary->id, 0, $grade);
+            grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, $grade);
         }
 
     } else {
@@ -399,7 +399,7 @@ function glossary_grade_item_update($glossary) {
         $params['scaleid']   = -$glossary->scale;
     }
 
-    return grade_update($glossary->course, 'mod', 'glossary', $glossary->id, 0, NULL, $params);
+    return grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, NULL, $params);
 }
 
 /**
@@ -411,7 +411,7 @@ function glossary_grade_item_delete($glossary) {
     global $CFG;
     require_once($CFG->libdir.'/gradelib.php');
 
-    return grade_update($glossary->course, 'mod', 'glossary', $glossary->id, 0, NULL, array('deleted'=>1));
+    return grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, NULL, array('deleted'=>1));
 }
 
 function glossary_get_participants($glossaryid) {