diff --git a/course/lib.php b/course/lib.php index bbdad829248..ce72a9cd96b 100644 --- a/course/lib.php +++ b/course/lib.php @@ -1595,14 +1595,6 @@ function set_coursemodule_visible($id, $visible) { } } - // Hide the associated grade items so the teacher doesn't also have to go to the gradebook and hide them there. - $grade_items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename, 'iteminstance'=>$cm->instance, 'courseid'=>$cm->course)); - if ($grade_items) { - foreach ($grade_items as $grade_item) { - $grade_item->set_hidden(!$visible); - } - } - // Updating visible and visibleold to keep them in sync. Only changing a section visibility will // affect visibleold to allow for an original visibility restore. See set_section_visible(). $cminfo = new stdClass(); @@ -1611,6 +1603,22 @@ function set_coursemodule_visible($id, $visible) { $cminfo->visibleold = $visible; $DB->update_record('course_modules', $cminfo); + // Hide the associated grade items so the teacher doesn't also have to go to the gradebook and hide them there. + // Note that this must be done after updating the row in course_modules, in case + // the modules grade_item_update function needs to access $cm->visible. + if (plugin_supports('mod', $modulename, FEATURE_CONTROLS_GRADE_VISIBILITY) && + component_callback_exists('mod_' . $modulename, 'grade_item_update')) { + $instance = $DB->get_record($modulename, array('id' => $cm->instance), '*', MUST_EXIST); + component_callback('mod_' . $modulename, 'grade_item_update', array($instance)); + } else { + $grade_items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename, 'iteminstance'=>$cm->instance, 'courseid'=>$cm->course)); + if ($grade_items) { + foreach ($grade_items as $grade_item) { + $grade_item->set_hidden(!$visible); + } + } + } + rebuild_course_cache($cm->course, true); return true; } diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index 7aa48c5a73d..0fc90d93eda 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -722,8 +722,13 @@ function quiz_grade_item_update($quiz, $grades = null) { if (!$params['hidden']) { // If the grade item is not hidden by the quiz logic, then we need to // hide it if the quiz is hidden from students. - $cm = get_coursemodule_from_instance('quiz', $quiz->id); - $params['hidden'] = !$cm->visible; + if (property_exists($quiz, 'visible')) { + // Saving the quiz form, and cm not yet updated in the database. + $params['hidden'] = !$quiz->visible; + } else { + $cm = get_coursemodule_from_instance('quiz', $quiz->id); + $params['hidden'] = !$cm->visible; + } } if ($grades === 'reset') {