Skip to content

Commit

Permalink
MDL-10223 module grades are now refreshed after unlocking
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Aug 6, 2007
1 parent 6200420 commit 2b0f65e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 21 deletions.
2 changes: 1 addition & 1 deletion grade/lib.php
Expand Up @@ -114,7 +114,7 @@ function print_grade_plugin_selector($courseid, $active_type, $active_plugin, $r
$menu[$url] = get_string('scales');
}

if (!empty($CFG->enableoutcomes) && (has_capability('moodle/grade:manage', $context) or
if (!empty($CFG->enableoutcomes) && (has_capability('moodle/grade:manage', $context) or
has_capability('moodle/course:update', $context))) {
if (has_capability('moodle/course:update', $context)) { // Default to course assignment
$url = 'edit/outcome/course.php?id='.$courseid;
Expand Down
24 changes: 16 additions & 8 deletions lib/grade/grade_category.php
Expand Up @@ -206,7 +206,7 @@ function delete($source=null) {
$category->delete($source);
}
}

if ($items = grade_item::fetch_all(array('courseid'=>$this->courseid))) {
foreach ($items as $item) {
if ($item->id == $grade_item->id) {
Expand All @@ -218,9 +218,9 @@ function delete($source=null) {

} else {
$this->force_regrading();

$parent = $this->load_parent_category();

// Update children's categoryid/parent field first
if ($children = grade_item::fetch_all(array('categoryid'=>$this->id))) {
foreach ($children as $child) {
Expand Down Expand Up @@ -681,7 +681,7 @@ function _fetch_course_tree_recursion($category_array, &$sortorder) {
if ($child_array['type'] == 'courseitem' or $child_array['type'] == 'categoryitem') {
$result['children'][$sortorder] = grade_category::_fetch_course_tree_recursion($child_array, $sortorder);
}
}
}
foreach($category_array['children'] as $oldorder=>$child_array) {
if ($child_array['type'] != 'courseitem' and $child_array['type'] != 'categoryitem') {
$result['children'][++$sortorder] = grade_category::_fetch_course_tree_recursion($child_array, $sortorder);
Expand Down Expand Up @@ -1008,21 +1008,29 @@ function is_locked() {
* Sets the grade_item's locked variable and updates the grade_item.
* Method named after grade_item::set_locked().
* @param int $locked 0, 1 or a timestamp int(10) after which date the item will be locked.
* @param boolean $refresh refresh grades when unlocking
* @return boolean success if category locked (not all children mayb be locked though)
*/
function set_locked($lockedstate) {
function set_locked($lockedstate, $refresh=true) {
$this->load_grade_item();
$result = $this->grade_item->set_locked($lockedstate);

$result = $this->grade_item->set_locked($lockedstate, true);
if ($children = grade_item::fetch_all(array('categoryid'=>$this->id))) {
foreach($children as $child) {
$child->set_locked($lockedstate);
$child->set_locked($lockedstate, false);
if (empty($lockedstate) and $refresh) {
//refresh when unlocking
$child->refresh_grades();
}
}
}
if ($children = grade_category::fetch_all(array('parent'=>$this->id))) {
foreach($children as $child) {
$child->set_locked($lockedstate);
$child->set_locked($lockedstate, true);
}
}


return $result;
}

Expand Down
10 changes: 8 additions & 2 deletions lib/grade/grade_grade.php
Expand Up @@ -258,10 +258,11 @@ function set_excluded($state) {
/**
* Lock/unlock this grade.
*
* @param boolean $lockstate true means lock, false unlock grade
* @param int $locked 0, 1 or a timestamp int(10) after which date the item will be locked.
* @param boolean $refresh refresh grades when unlocking
* @return boolean true if sucessful, false if can not set new lock state for grade
*/
function set_locked($lockedstate) {
function set_locked($lockedstate, $refresh=true) {
$this->load_grade_item();

if ($lockedstate) {
Expand Down Expand Up @@ -293,6 +294,11 @@ function set_locked($lockedstate) {

$this->update();

if ($refresh) {
//refresh when unlocking
$this->grade_item->refresh_grades($this->userid);
}

return true;
}
}
Expand Down
43 changes: 38 additions & 5 deletions lib/grade/grade_item.php
Expand Up @@ -424,11 +424,11 @@ function is_locked($userid=NULL) {

/**
* Locks or unlocks this grade_item and (optionally) all its associated final grades.
* @param boolean $update_final Whether to update final grades too
* @param boolean $new_state Optional new state. Will use inverse of current state otherwise.
* @param int $locked 0, 1 or a timestamp int(10) after which date the item will be locked.
* @param boolean $refresh refresh grades when unlocking
* @return boolean true if grade_item all grades updated, false if at least one update fails
*/
function set_locked($lockedstate) {
function set_locked($lockedstate, $refresh=true) {
if ($lockedstate) {
/// setting lock
if (!empty($this->locked)) {
Expand All @@ -448,7 +448,7 @@ function set_locked($lockedstate) {
foreach($grades as $g) {
$grade = new grade_grade($g, false);
$grade->grade_item =& $this;
if (!$grade->set_locked(true)) {
if (!$grade->set_locked(1, false)) {
$result = false;
}
}
Expand Down Expand Up @@ -478,12 +478,17 @@ function set_locked($lockedstate) {
$result = false; // can not unlock grade that should be already locked
}

if (!$grade->set_locked(false)) {
if (!$grade->set_locked(0, false)) {
$result = false;
}
}
}

if ($refresh) {
//refresh when unlocking
$this->refresh_grades();
}

return $result;

}
Expand Down Expand Up @@ -1169,6 +1174,34 @@ function depends_on() {
}
}

/**
* Refetch grades from moudles, plugins.
* @param int $userid optional, one user only
*/
function refresh_grades($userid=0) {
if ($this->itemtype == 'mod') {
if ($this->is_outcome_item()) {
//nothing to do
return;
}

if (!$activity = get_record($this->itemmodule, 'id', $this->iteminstance)) {
debuggin('Can not find activity');
return;
}

if (! $cm = get_coursemodule_from_instance($this->itemmodule, $activity->id, $this->courseid)) {
debuggin('Can not find course module');
return;
}

$activity->modname = $this->itemmodule;
$activity->cmidnumber = $cm->idnumber;

grade_update_mod_grades($activity);
}
}

/**
* Updates final grade value for given user, this is a only way to update final
* grades from gradebook and import because it logs the change in history table
Expand Down
15 changes: 10 additions & 5 deletions lib/gradelib.php
Expand Up @@ -619,7 +619,7 @@ function grade_grab_grades() {
* @param object $modinstance object with extra cmidnumber and modname property
* @return boolean success
*/
function grade_update_mod_grades($modinstance) {
function grade_update_mod_grades($modinstance, $userid=0) {
global $CFG;

$fullmod = $CFG->dirroot.'/mod/'.$modinstance->modname;
Expand All @@ -635,6 +635,8 @@ function grade_update_mod_grades($modinstance) {
$updateitemfunc = $modinstance->modname.'_grade_item_update';

if (function_exists($gradefunc)) {

// legacy module - not yet converted
if ($oldgrades = $gradefunc($modinstance->id)) {

$grademax = $oldgrades->maxgrade;
Expand All @@ -654,9 +656,12 @@ function grade_update_mod_grades($modinstance) {
}

$grades = array();
foreach ($oldgrades->grades as $userid=>$usergrade) {
foreach ($oldgrades->grades as $uid=>$usergrade) {
if ($userid and $uid != $userid) {
continue;
}
$grade = new object();
$grade->userid = $userid;
$grade->userid = $uid;

if ($usergrade == '-') {
// no grade
Expand All @@ -681,10 +686,10 @@ function grade_update_mod_grades($modinstance) {
} else if (function_exists($updategradesfunc) and function_exists($updateitemfunc)) {
//new grading supported, force updating of grades
$updateitemfunc($modinstance);
$updategradesfunc($modinstance);
$updategradesfunc($modinstance, $userid);

} else {
// mudule does not support grading
// mudule does not support grading??
}

return true;
Expand Down

0 comments on commit 2b0f65e

Please sign in to comment.