/** * Constructor, retrieves and stores a hierarchical array of all grade_category and grade_item * objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed. * * @param int $courseid The Course ID * @param bool $fillers include fillers and colspans, make the levels var "rectangular" * @param bool $category_grade_last category grade item is the last child * @param array $collapsed array of collapsed categories * @param bool $nooutcomes Whether or not outcomes should be included */ public function grade_tree($courseid, $fillers = true, $category_grade_last = false, $collapsed = null, $nooutcomes = false) { global $USER, $CFG, $COURSE, $DB; $this->courseid = $courseid; $this->levels = array(); $this->context = context_course::instance($courseid); if (!empty($COURSE->id) && $COURSE->id == $this->courseid) { $course = $COURSE; } else { $course = $DB->get_record('course', array('id' => $this->courseid)); } $this->modinfo = get_fast_modinfo($course); // get course grade tree $this->top_element = grade_category::fetch_course_tree($courseid, true); // collapse the categories if requested if (!empty($collapsed)) { grade_tree::category_collapse($this->top_element, $collapsed); } // no otucomes if requested if (!empty($nooutcomes)) { grade_tree::no_outcomes($this->top_element); } // move category item to last position in category if ($category_grade_last) { grade_tree::category_grade_last($this->top_element); } if ($fillers) { // inject fake categories == fillers grade_tree::inject_fillers($this->top_element, 0); // add colspans to categories and fillers grade_tree::inject_colspans($this->top_element); } grade_tree::fill_levels($this->levels, $this->top_element, 0); }
/** * Constructor, retrieves and stores a hierarchical array of all grade_category and grade_item * objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed. * * @param int $courseid The Course ID * @param bool $fillers include fillers and colspans, make the levels var "rectangular" * @param bool $category_grade_last category grade item is the last child * @param array $collapsed array of collapsed categories * @param bool $nooutcomes Whether or not outcomes should be included */ public function grade_tree($courseid, $fillers = true, $category_grade_last = false, $collapsed = null, $nooutcomes = false) { global $USER, $CFG; $this->courseid = $courseid; $this->levels = array(); $this->context = get_context_instance(CONTEXT_COURSE, $courseid); // get course grade tree $this->top_element = grade_category::fetch_course_tree($courseid, true); // collapse the categories if requested if (!empty($collapsed)) { grade_tree::category_collapse($this->top_element, $collapsed); } // no otucomes if requested if (!empty($nooutcomes)) { grade_tree::no_outcomes($this->top_element); } // move category item to last position in category if ($category_grade_last) { grade_tree::category_grade_last($this->top_element); } if ($fillers) { // inject fake categories == fillers grade_tree::inject_fillers($this->top_element, 0); // add colspans to categories and fillers grade_tree::inject_colspans($this->top_element); } grade_tree::fill_levels($this->levels, $this->top_element, 0); }
function backup_gradebook_info($bf, $preferences) { global $CFG; require_once $CFG->libdir . '/gradelib.php'; //first make sure items are properly sorted and everything is ok grade_category::fetch_course_tree($preferences->backup_course, true); grade_regrade_final_grades($preferences->backup_course); $status = true; // see if ALL grade items of type mod of this course are being backed up // if not, we do not need to backup grade category and associated grade items/grades $backupall = true; if ($grade_items = get_records_sql("SELECT *\n FROM {$CFG->prefix}grade_items\n WHERE courseid = {$preferences->backup_course}\n AND itemtype = 'mod'")) { foreach ($grade_items as $grade_item) { // get module information // if some activities not selected, we do not backup categories at all if (!backup_mod_selected($preferences, $grade_item->itemmodule, $grade_item->iteminstance)) { $backupall = false; break; } } unset($grade_items); //free memory } //Gradebook header fwrite($bf, start_tag("GRADEBOOK", 2, true)); $status = backup_gradebook_outcomes_info($bf, $preferences); $status = backup_gradebook_grade_letters_info($bf, $preferences); // Now backup grade_item (inside grade_category) if ($backupall) { $status = backup_gradebook_category_info($bf, $preferences); } $status = backup_gradebook_item_info($bf, $preferences, $backupall); // backup gradebook histories (only if grade history is enabled and selected) if (empty($CFG->disablegradehistory) && $preferences->backup_gradebook_history) { $status = backup_gradebook_outcomes_history($bf, $preferences); $status = backup_gradebook_categories_history_info($bf, $preferences); $status = backup_gradebook_items_history_info($bf, $preferences); $status = backup_gradebook_grades_history_info($bf, $preferences); } //Gradebook footer $status = fwrite($bf, end_tag("GRADEBOOK", 2, true)); return $status; }
/** * Test can_output_item. */ public function test_can_output_item() { $this->resetAfterTest(); $generator = $this->getDataGenerator(); // Course level grade category. $course = $generator->create_course(); // Grade tree looks something like: // - Test course (Rendered). $gradetree = grade_category::fetch_course_tree($course->id); $this->assertTrue(grade_tree::can_output_item($gradetree)); // Add a grade category with default settings. $generator->create_grade_category(array('courseid' => $course->id)); // Grade tree now looks something like: // - Test course n (Rendered). // -- Grade category n (Rendered). $gradetree = grade_category::fetch_course_tree($course->id); $this->assertNotEmpty($gradetree['children']); foreach ($gradetree['children'] as $child) { $this->assertTrue(grade_tree::can_output_item($child)); } // Add a grade category with grade type = None. $nototalcategory = 'No total category'; $nototalparams = ['courseid' => $course->id, 'fullname' => $nototalcategory, 'aggregation' => GRADE_AGGREGATE_WEIGHTED_MEAN]; $nototal = $generator->create_grade_category($nototalparams); $catnototal = grade_category::fetch(array('id' => $nototal->id)); // Set the grade type of the grade item associated to the grade category. $catitemnototal = $catnototal->load_grade_item(); $catitemnototal->gradetype = GRADE_TYPE_NONE; $catitemnototal->update(); // Grade tree looks something like: // - Test course n (Rendered). // -- Grade category n (Rendered). // -- No total category (Not rendered). $gradetree = grade_category::fetch_course_tree($course->id); foreach ($gradetree['children'] as $child) { if ($child['object']->fullname == $nototalcategory) { $this->assertFalse(grade_tree::can_output_item($child)); } else { $this->assertTrue(grade_tree::can_output_item($child)); } } // Add another grade category with default settings under 'No total category'. $normalinnototalparams = ['courseid' => $course->id, 'fullname' => 'Normal category in no total category', 'parent' => $nototal->id]; $generator->create_grade_category($normalinnototalparams); // Grade tree looks something like: // - Test course n (Rendered). // -- Grade category n (Rendered). // -- No total category (Rendered). // --- Normal category in no total category (Rendered). $gradetree = grade_category::fetch_course_tree($course->id); foreach ($gradetree['children'] as $child) { // All children are now visible. $this->assertTrue(grade_tree::can_output_item($child)); if (!empty($child['children'])) { foreach ($child['children'] as $grandchild) { $this->assertTrue(grade_tree::can_output_item($grandchild)); } } } // Add a grade category with grade type = None. $nototalcategory2 = 'No total category 2'; $nototal2params = ['courseid' => $course->id, 'fullname' => $nototalcategory2, 'aggregation' => GRADE_AGGREGATE_WEIGHTED_MEAN]; $nototal2 = $generator->create_grade_category($nototal2params); $catnototal2 = grade_category::fetch(array('id' => $nototal2->id)); // Set the grade type of the grade item associated to the grade category. $catitemnototal2 = $catnototal2->load_grade_item(); $catitemnototal2->gradetype = GRADE_TYPE_NONE; $catitemnototal2->update(); // Add a category with no total under 'No total category'. $nototalinnototalcategory = 'Category with no total in no total category'; $nototalinnototalparams = ['courseid' => $course->id, 'fullname' => $nototalinnototalcategory, 'aggregation' => GRADE_AGGREGATE_WEIGHTED_MEAN, 'parent' => $nototal2->id]; $nototalinnototal = $generator->create_grade_category($nototalinnototalparams); $catnototalinnototal = grade_category::fetch(array('id' => $nototalinnototal->id)); // Set the grade type of the grade item associated to the grade category. $catitemnototalinnototal = $catnototalinnototal->load_grade_item(); $catitemnototalinnototal->gradetype = GRADE_TYPE_NONE; $catitemnototalinnototal->update(); // Grade tree looks something like: // - Test course n (Rendered). // -- Grade category n (Rendered). // -- No total category (Rendered). // --- Normal category in no total category (Rendered). // -- No total category 2 (Not rendered). // --- Category with no total in no total category (Not rendered). $gradetree = grade_category::fetch_course_tree($course->id); foreach ($gradetree['children'] as $child) { if ($child['object']->fullname == $nototalcategory2) { $this->assertFalse(grade_tree::can_output_item($child)); } else { $this->assertTrue(grade_tree::can_output_item($child)); } if (!empty($child['children'])) { foreach ($child['children'] as $grandchild) { if ($grandchild['object']->fullname == $nototalinnototalcategory) { $this->assertFalse(grade_tree::can_output_item($grandchild)); } else { $this->assertTrue(grade_tree::can_output_item($grandchild)); } } } } }