/** * Test the event. */ public function test_event() { global $CFG; require_once "{$CFG->libdir}/gradelib.php"; $this->resetAfterTest(); $course = $this->getDataGenerator()->create_course(); $user = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($user->id, $course->id); $grade_category = grade_category::fetch_course_category($course->id); $grade_category->load_grade_item(); $grade_item = $grade_category->grade_item; $grade_item->update_final_grade($user->id, 10, 'gradebook'); $grade_grade = new grade_grade(array('userid' => $user->id, 'itemid' => $grade_item->id), true); $grade_grade->grade_item = $grade_item; $event = \core\event\user_graded::create_from_grade($grade_grade); $this->assertEventLegacyLogData(array($course->id, 'grade', 'update', '/report/grader/index.php?id=' . $course->id, $grade_item->itemname . ': ' . fullname($user)), $event); $this->assertEquals(context_course::instance($course->id), $event->get_context()); $this->assertSame($event->objecttable, 'grade_grades'); $this->assertEquals($event->objectid, $grade_grade->id); $this->assertEquals($event->other['itemid'], $grade_item->id); $this->assertTrue($event->other['overridden']); $this->assertEquals(10, $event->other['finalgrade']); // Trigger the events. $sink = $this->redirectEvents(); $event->trigger(); $result = $sink->get_events(); $sink->close(); $this->assertCount(1, $result); $event = reset($result); $this->assertEventContextNotUsed($event); $grade = $event->get_grade(); $this->assertInstanceOf('grade_grade', $grade); $this->assertEquals($grade_grade->id, $grade->id); }
/** * Adds a grade category and moves the specified item into it. * Uses locks to prevent race conditions (see MDL-37055). */ public function execute() { $customdata = $this->get_custom_data(); // Get lock timeout. $timeout = 5; // A namespace for the locks. $locktype = 'block_mhaairs_add_category'; // Resource key - course id and category name. $resource = "course: {$customdata->courseid}; catname: {$customdata->catname}"; // Get an instance of the currently configured lock_factory. $lockfactory = \core\lock\lock_config::get_lock_factory($locktype); // Open a lock. $lock = $lockfactory->get_lock($resource, $timeout); // Add the category. $catparams = array('fullname' => $customdata->catname, 'courseid' => $customdata->courseid); if (!($category = \grade_category::fetch($catparams))) { // If the category does not exist we create it. $gradeaggregation = get_config('core', 'grade_aggregation'); if ($gradeaggregation === false) { $gradeaggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2; } // Parent category is automatically added(created) during insert. $catparams['hidden'] = false; $catparams['aggregation'] = $gradeaggregation; try { $category = new \grade_category($catparams, false); $category->id = $category->insert(); } catch (Exception $e) { // Must release the locks. $lock->release(); // Rethrow to reschedule task. throw $e; } } // Release locks. $lock->release(); // Add the item to the category. $gitem = \grade_item::fetch(array('id' => $customdata->itemid)); $gitem->categoryid = $category->id; $gitem->update(); }
public function cleanup() { // cleanup any clickers before the test $user_id = iclicker_service::require_user(); $results = iclicker_service::get_registrations_by_user($user_id); if ($results) { echo "cleanup registrations for user: {$user_id} " . PHP_EOL; foreach ($results as $reg) { if ($reg->clicker_id == $this->clicker_id) { iclicker_service::remove_registration($reg->id); echo "cleanup: {$reg->id} " . PHP_EOL; } } } // cleanup the test grades $def_grade_cats = grade_category::fetch_all(array('courseid' => $this->courseid, 'fullname' => iclicker_service::GRADE_CATEGORY_NAME)); $stuff_grade_cats = grade_category::fetch_all(array('courseid' => $this->courseid, 'fullname' => 'stuff')); $grade_cats = $def_grade_cats; if (is_array($def_grade_cats) && is_array($stuff_grade_cats)) { $grade_cats = array_merge($def_grade_cats, $stuff_grade_cats); } else { if (is_array($stuff_grade_cats)) { $grade_cats = $stuff_grade_cats; } } if ($grade_cats) { foreach ($grade_cats as $cat) { $grade_items = grade_item::fetch_all(array('courseid' => $this->courseid, 'categoryid' => $cat->id)); if ($grade_items) { foreach ($grade_items as $item) { $grades = grade_grade::fetch_all(array('itemid' => $item->id)); if ($grades) { foreach ($grades as $grade) { $grade->delete("cleanup"); } } $item->delete("cleanup"); } } $cat->delete("cleanup"); } } }
/** * This function creates all the gradebook data from xml */ function restore_create_gradebook($restore, $xml_file) { global $CFG; $status = true; //Check it exists if (!file_exists($xml_file)) { return false; } // Get info from xml // info will contain the number of record to process $info = restore_read_xml_gradebook($restore, $xml_file); // If we have info, then process if (empty($info)) { return $status; } if (empty($CFG->disablegradehistory) and isset($info->gradebook_histories) and $info->gradebook_histories == "true") { $restore_histories = true; } else { $restore_histories = false; } // make sure top course category exists $course_category = grade_category::fetch_course_category($restore->course_id); $course_category->load_grade_item(); // we need to know if all grade items that were backed up are being restored // if that is not the case, we do not restore grade categories nor gradeitems of category type or course type // i.e. the aggregated grades of that category $restoreall = true; // set to false if any grade_item is not selected/restored or already exist $importing = !empty($SESSION->restore->importing); if ($importing) { $restoreall = false; } else { $prev_grade_items = grade_item::fetch_all(array('courseid' => $restore->course_id)); $prev_grade_cats = grade_category::fetch_all(array('courseid' => $restore->course_id)); // if any categories already present, skip restore of categories from backup - course item or category already exist if (count($prev_grade_items) > 1 or count($prev_grade_cats) > 1) { $restoreall = false; } unset($prev_grade_items); unset($prev_grade_cats); if ($restoreall) { if ($recs = get_records_select("backup_ids", "table_name = 'grade_items' AND backup_code = {$restore->backup_unique_code}", "", "old_id")) { foreach ($recs as $rec) { if ($data = backup_getid($restore->backup_unique_code, 'grade_items', $rec->old_id)) { $info = $data->info; // do not restore if this grade_item is a mod, and $itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']); if ($itemtype == 'mod') { $olditeminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']); $itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']); if (empty($restore->mods[$itemmodule]->granular)) { continue; } else { if (!empty($restore->mods[$itemmodule]->instances[$olditeminstance]->restore)) { continue; } } // at least one activity should not be restored - do not restore categories and manual items at all $restoreall = false; break; } } } } } } // Start ul if (!defined('RESTORE_SILENTLY')) { echo '<ul>'; } // array of restored categories - speedup ;-) $cached_categories = array(); $outcomes = array(); /// Process letters $context = get_context_instance(CONTEXT_COURSE, $restore->course_id); // respect current grade letters if defined if ($status and $restoreall and !record_exists('grade_letters', 'contextid', $context->id)) { if (!defined('RESTORE_SILENTLY')) { echo '<li>' . get_string('gradeletters', 'grades') . '</li>'; } // Fetch recordset_size records in each iteration $recs = get_records_select("backup_ids", "table_name = 'grade_letters' AND backup_code = {$restore->backup_unique_code}", "", "old_id"); if ($recs) { foreach ($recs as $rec) { // Get the full record from backup_ids $data = backup_getid($restore->backup_unique_code, 'grade_letters', $rec->old_id); if ($data) { $info = $data->info; $dbrec = new object(); $dbrec->contextid = $context->id; $dbrec->lowerboundary = backup_todb($info['GRADE_LETTER']['#']['LOWERBOUNDARY']['0']['#']); $dbrec->letter = backup_todb($info['GRADE_LETTER']['#']['LETTER']['0']['#']); insert_record('grade_letters', $dbrec); } } } } /// Preprocess outcomes - do not store them yet! if ($status and !$importing and $restoreall) { if (!defined('RESTORE_SILENTLY')) { echo '<li>' . get_string('gradeoutcomes', 'grades') . '</li>'; } $recs = get_records_select("backup_ids", "table_name = 'grade_outcomes' AND backup_code = '{$restore->backup_unique_code}'", "", "old_id"); if ($recs) { foreach ($recs as $rec) { //Get the full record from backup_ids $data = backup_getid($restore->backup_unique_code, 'grade_outcomes', $rec->old_id); if ($data) { $info = $data->info; //first find out if outcome already exists $shortname = backup_todb($info['GRADE_OUTCOME']['#']['SHORTNAME']['0']['#']); if ($candidates = get_records_sql("SELECT *\n FROM {$CFG->prefix}grade_outcomes\n WHERE (courseid IS NULL OR courseid = {$restore->course_id})\n AND shortname = '{$shortname}'\n ORDER BY courseid ASC, id ASC")) { $grade_outcome = reset($candidates); $outcomes[$rec->old_id] = $grade_outcome; continue; } $dbrec = new object(); if (has_capability('moodle/grade:manageoutcomes', get_context_instance(CONTEXT_SYSTEM))) { $oldoutcome = backup_todb($info['GRADE_OUTCOME']['#']['COURSEID']['0']['#']); if (empty($oldoutcome)) { //site wide $dbrec->courseid = null; } else { //course only $dbrec->courseid = $restore->course_id; } } else { // no permission to add site outcomes $dbrec->courseid = $restore->course_id; } //Get the fields $dbrec->shortname = backup_todb($info['GRADE_OUTCOME']['#']['SHORTNAME']['0']['#'], false); $dbrec->fullname = backup_todb($info['GRADE_OUTCOME']['#']['FULLNAME']['0']['#'], false); $dbrec->scaleid = backup_todb($info['GRADE_OUTCOME']['#']['SCALEID']['0']['#'], false); $dbrec->description = backup_todb($info['GRADE_OUTCOME']['#']['DESCRIPTION']['0']['#'], false); $dbrec->timecreated = backup_todb($info['GRADE_OUTCOME']['#']['TIMECREATED']['0']['#'], false); $dbrec->timemodified = backup_todb($info['GRADE_OUTCOME']['#']['TIMEMODIFIED']['0']['#'], false); $dbrec->usermodified = backup_todb($info['GRADE_OUTCOME']['#']['USERMODIFIED']['0']['#'], false); //Need to recode the scaleid if ($scale = backup_getid($restore->backup_unique_code, 'scale', $dbrec->scaleid)) { $dbrec->scaleid = $scale->new_id; } //Need to recode the usermodified if ($modifier = backup_getid($restore->backup_unique_code, 'user', $dbrec->usermodified)) { $dbrec->usermodified = $modifier->new_id; } $grade_outcome = new grade_outcome($dbrec, false); $outcomes[$rec->old_id] = $grade_outcome; } } } } /// Process grade items and grades if ($status) { if (!defined('RESTORE_SILENTLY')) { echo '<li>' . get_string('gradeitems', 'grades') . '</li>'; } $counter = 0; //Fetch recordset_size records in each iteration $recs = get_records_select("backup_ids", "table_name = 'grade_items' AND backup_code = '{$restore->backup_unique_code}'", "id", "old_id"); if ($recs) { foreach ($recs as $rec) { //Get the full record from backup_ids $data = backup_getid($restore->backup_unique_code, 'grade_items', $rec->old_id); if ($data) { $info = $data->info; // first find out if category or normal item $itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false); if ($itemtype == 'course' or $itemtype == 'category') { if (!$restoreall or $importing) { continue; } $oldcat = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#'], false); if (!($cdata = backup_getid($restore->backup_unique_code, 'grade_categories', $oldcat))) { continue; } $cinfo = $cdata->info; unset($cdata); if ($itemtype == 'course') { $course_category->fullname = backup_todb($cinfo['GRADE_CATEGORY']['#']['FULLNAME']['0']['#'], false); $course_category->aggregation = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#'], false); $course_category->keephigh = backup_todb($cinfo['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#'], false); $course_category->droplow = backup_todb($cinfo['GRADE_CATEGORY']['#']['DROPLOW']['0']['#'], false); $course_category->aggregateonlygraded = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEONLYGRADED']['0']['#'], false); $course_category->aggregateoutcomes = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEOUTCOMES']['0']['#'], false); $course_category->aggregatesubcats = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATESUBCATS']['0']['#'], false); $course_category->timecreated = backup_todb($cinfo['GRADE_CATEGORY']['#']['TIMECREATED']['0']['#'], false); $course_category->update('restore'); $status = backup_putid($restore->backup_unique_code, 'grade_categories', $oldcat, $course_category->id) && $status; $cached_categories[$oldcat] = $course_category; $grade_item = $course_category->get_grade_item(); } else { $oldparent = backup_todb($cinfo['GRADE_CATEGORY']['#']['PARENT']['0']['#'], false); if (empty($cached_categories[$oldparent])) { debugging('parent not found ' . $oldparent); continue; // parent not found, sorry } $grade_category = new grade_category(); $grade_category->courseid = $restore->course_id; $grade_category->parent = $cached_categories[$oldparent]->id; $grade_category->fullname = backup_todb($cinfo['GRADE_CATEGORY']['#']['FULLNAME']['0']['#'], false); $grade_category->aggregation = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#'], false); $grade_category->keephigh = backup_todb($cinfo['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#'], false); $grade_category->droplow = backup_todb($cinfo['GRADE_CATEGORY']['#']['DROPLOW']['0']['#'], false); $grade_category->aggregateonlygraded = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEONLYGRADED']['0']['#'], false); $grade_category->aggregateoutcomes = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEOUTCOMES']['0']['#'], false); $grade_category->aggregatesubcats = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATESUBCATS']['0']['#'], false); $grade_category->timecreated = backup_todb($cinfo['GRADE_CATEGORY']['#']['TIMECREATED']['0']['#'], false); $grade_category->insert('restore'); $status = backup_putid($restore->backup_unique_code, 'grade_categories', $oldcat, $grade_category->id) && $status; $cached_categories[$oldcat] = $grade_category; $grade_item = $grade_category->get_grade_item(); // creates grade_item too } unset($cinfo); $idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#'], false); if (grade_verify_idnumber($idnumber, $restore->course_id)) { $grade_item->idnumber = $idnumber; } $grade_item->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#'], false); $grade_item->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#'], false); $grade_item->gradetype = backup_todb($info['GRADE_ITEM']['#']['GRADETYPE']['0']['#'], false); $grade_item->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#'], false); $grade_item->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#'], false); $grade_item->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#'], false); $grade_item->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#'], false); $grade_item->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#'], false); $grade_item->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#'], false); $grade_item->aggregationcoef = backup_todb($info['GRADE_ITEM']['#']['AGGREGATIONCOEF']['0']['#'], false); $grade_item->display = backup_todb($info['GRADE_ITEM']['#']['DISPLAY']['0']['#'], false); $grade_item->decimals = backup_todb($info['GRADE_ITEM']['#']['DECIMALS']['0']['#'], false); $grade_item->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#'], false); $grade_item->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#'], false); $grade_item->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#'], false); $grade_item->timecreated = backup_todb($info['GRADE_ITEM']['#']['TIMECREATED']['0']['#'], false); if (backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false)) { $scale = backup_getid($restore->backup_unique_code, "scale", backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false)); $grade_item->scaleid = $scale->new_id; } if (backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'], false)) { $outcome = backup_getid($restore->backup_unique_code, "grade_outcomes", backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'], false)); $grade_item->outcomeid = $outcome->new_id; } $grade_item->update('restore'); $status = backup_putid($restore->backup_unique_code, "grade_items", $rec->old_id, $grade_item->id) && $status; } else { if ($itemtype != 'mod' and (!$restoreall or $importing)) { // not extra gradebook stuff if restoring individual activities or something already there continue; } $dbrec = new object(); $dbrec->courseid = $restore->course_id; $dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false); $dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#'], false); if ($itemtype == 'mod') { // iteminstance should point to new mod $olditeminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#'], false); $mod = backup_getid($restore->backup_unique_code, $dbrec->itemmodule, $olditeminstance); $dbrec->iteminstance = $mod->new_id; if (!($cm = get_coursemodule_from_instance($dbrec->itemmodule, $mod->new_id))) { // item not restored - no item continue; } // keep in sync with activity idnumber $dbrec->idnumber = $cm->idnumber; } else { $idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#'], false); if (grade_verify_idnumber($idnumber, $restore->course_id)) { //make sure the new idnumber is unique $dbrec->idnumber = $idnumber; } } $dbrec->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#'], false); $dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false); $dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#'], false); $dbrec->itemnumber = backup_todb($info['GRADE_ITEM']['#']['ITEMNUMBER']['0']['#'], false); $dbrec->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#'], false); $dbrec->gradetype = backup_todb($info['GRADE_ITEM']['#']['GRADETYPE']['0']['#'], false); $dbrec->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#'], false); $dbrec->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#'], false); $dbrec->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#'], false); $dbrec->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#'], false); $dbrec->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#'], false); $dbrec->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#'], false); $dbrec->aggregationcoef = backup_todb($info['GRADE_ITEM']['#']['AGGREGATIONCOEF']['0']['#'], false); $dbrec->display = backup_todb($info['GRADE_ITEM']['#']['DISPLAY']['0']['#'], false); $dbrec->decimals = backup_todb($info['GRADE_ITEM']['#']['DECIMALS']['0']['#'], false); $dbrec->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#'], false); $dbrec->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#'], false); $dbrec->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#'], false); $dbrec->timecreated = backup_todb($info['GRADE_ITEM']['#']['TIMECREATED']['0']['#'], false); if (backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false)) { $scale = backup_getid($restore->backup_unique_code, "scale", backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false)); $dbrec->scaleid = $scale->new_id; } if (backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'])) { $oldoutcome = backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#']); if (empty($outcomes[$oldoutcome])) { continue; // error! } if (empty($outcomes[$oldoutcome]->id)) { $outcomes[$oldoutcome]->insert('restore'); $outcomes[$oldoutcome]->use_in($restore->course_id); backup_putid($restore->backup_unique_code, "grade_outcomes", $oldoutcome, $outcomes[$oldoutcome]->id); } $dbrec->outcomeid = $outcomes[$oldoutcome]->id; } $grade_item = new grade_item($dbrec, false); $grade_item->insert('restore'); if ($restoreall) { // set original parent if restored $oldcat = $info['GRADE_ITEM']['#']['CATEGORYID']['0']['#']; if (!empty($cached_categories[$oldcat])) { $grade_item->set_parent($cached_categories[$oldcat]->id); } } $status = backup_putid($restore->backup_unique_code, "grade_items", $rec->old_id, $grade_item->id) && $status; } // no need to restore grades if user data is not selected or importing activities if ($importing or $grade_item->itemtype == 'mod' and !restore_userdata_selected($restore, $grade_item->itemmodule, $olditeminstance)) { // module instance not selected when restored using granular // skip this item continue; } /// now, restore grade_grades if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'])) { //Iterate over items foreach ($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'] as $g_info) { $grade = new grade_grade(); $grade->itemid = $grade_item->id; $olduser = backup_todb($g_info['#']['USERID']['0']['#'], false); $user = backup_getid($restore->backup_unique_code, "user", $olduser); $grade->userid = $user->new_id; $grade->rawgrade = backup_todb($g_info['#']['RAWGRADE']['0']['#'], false); $grade->rawgrademax = backup_todb($g_info['#']['RAWGRADEMAX']['0']['#'], false); $grade->rawgrademin = backup_todb($g_info['#']['RAWGRADEMIN']['0']['#'], false); // need to find scaleid if (backup_todb($g_info['#']['RAWSCALEID']['0']['#'])) { $scale = backup_getid($restore->backup_unique_code, "scale", backup_todb($g_info['#']['RAWSCALEID']['0']['#'], false)); $grade->rawscaleid = $scale->new_id; } if (backup_todb($g_info['#']['USERMODIFIED']['0']['#'])) { if ($modifier = backup_getid($restore->backup_unique_code, "user", backup_todb($g_info['#']['USERMODIFIED']['0']['#'], false))) { $grade->usermodified = $modifier->new_id; } } $grade->finalgrade = backup_todb($g_info['#']['FINALGRADE']['0']['#'], false); $grade->hidden = backup_todb($g_info['#']['HIDDEN']['0']['#'], false); $grade->locked = backup_todb($g_info['#']['LOCKED']['0']['#'], false); $grade->locktime = backup_todb($g_info['#']['LOCKTIME']['0']['#'], false); $grade->exported = backup_todb($g_info['#']['EXPORTED']['0']['#'], false); $grade->overridden = backup_todb($g_info['#']['OVERRIDDEN']['0']['#'], false); $grade->excluded = backup_todb($g_info['#']['EXCLUDED']['0']['#'], false); $grade->feedback = backup_todb($g_info['#']['FEEDBACK']['0']['#'], false); $grade->feedbackformat = backup_todb($g_info['#']['FEEDBACKFORMAT']['0']['#'], false); $grade->information = backup_todb($g_info['#']['INFORMATION']['0']['#'], false); $grade->informationformat = backup_todb($g_info['#']['INFORMATIONFORMAT']['0']['#'], false); $grade->timecreated = backup_todb($g_info['#']['TIMECREATED']['0']['#'], false); $grade->timemodified = backup_todb($g_info['#']['TIMEMODIFIED']['0']['#'], false); $grade->insert('restore'); backup_putid($restore->backup_unique_code, "grade_grades", backup_todb($g_info['#']['ID']['0']['#']), $grade->id); $counter++; if ($counter % 20 == 0) { if (!defined('RESTORE_SILENTLY')) { echo "."; if ($counter % 400 == 0) { echo "<br />"; } } backup_flush(300); } } } } } } } /// add outcomes that are not used when doing full restore if ($status and $restoreall) { foreach ($outcomes as $oldoutcome => $grade_outcome) { if (empty($grade_outcome->id)) { $grade_outcome->insert('restore'); $grade_outcome->use_in($restore->course_id); backup_putid($restore->backup_unique_code, "grade_outcomes", $oldoutcome, $grade_outcome->id); } } } if ($status and !$importing and $restore_histories) { /// following code is very inefficient $gchcount = count_records('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_categories_history'); $gghcount = count_records('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_grades_history'); $gihcount = count_records('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_items_history'); $gohcount = count_records('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_outcomes_history'); // Number of records to get in every chunk $recordset_size = 2; // process histories if ($gchcount && $status) { if (!defined('RESTORE_SILENTLY')) { echo '<li>' . get_string('gradecategoryhistory', 'grades') . '</li>'; } $counter = 0; while ($counter < $gchcount) { //Fetch recordset_size records in each iteration $recs = get_records_select("backup_ids", "table_name = 'grade_categories_history' AND backup_code = '{$restore->backup_unique_code}'", "old_id", "old_id", $counter, $recordset_size); if ($recs) { foreach ($recs as $rec) { //Get the full record from backup_ids $data = backup_getid($restore->backup_unique_code, 'grade_categories_history', $rec->old_id); if ($data) { //Now get completed xmlized object $info = $data->info; //traverse_xmlize($info); //Debug //print_object ($GLOBALS['traverse_array']); //Debug //$GLOBALS['traverse_array']=""; //Debug $oldobj = backup_getid($restore->backup_unique_code, "grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['OLDID']['0']['#'])); if (empty($oldobj->new_id)) { // if the old object is not being restored, can't restoring its history $counter++; continue; } $dbrec->oldid = $oldobj->new_id; $dbrec->action = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['ACTION']['0']['#']); $dbrec->source = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['SOURCE']['0']['#']); $dbrec->timemodified = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['TIMEMODIFIED']['0']['#']); // loggeduser might not be restored, e.g. admin if ($oldobj = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) { $dbrec->loggeduser = $oldobj->new_id; } // this item might not have a parent at all, do not skip it if no parent is specified if (backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#'])) { $oldobj = backup_getid($restore->backup_unique_code, "grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#'])); if (empty($oldobj->new_id)) { // if the parent category not restored $counter++; continue; } } $dbrec->parent = $oldobj->new_id; $dbrec->depth = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DEPTH']['0']['#']); // path needs to be rebuilt if ($path = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PATH']['0']['#'])) { // to preserve the path and make it work, we need to replace the categories one by one // we first get the list of categories in current path if ($paths = explode("/", $path)) { $newpath = ''; foreach ($paths as $catid) { if ($catid) { // find the new corresponding path $oldpath = backup_getid($restore->backup_unique_code, "grade_categories", $catid); $newpath .= "/{$oldpath->new_id}"; } } $dbrec->path = $newpath; } } $dbrec->fullname = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['FULLNAME']['0']['#']); $dbrec->aggregation = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGRETGATION']['0']['#']); $dbrec->keephigh = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['KEEPHIGH']['0']['#']); $dbrec->droplow = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DROPLOW']['0']['#']); $dbrec->aggregateonlygraded = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATEONLYGRADED']['0']['#']); $dbrec->aggregateoutcomes = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATEOUTCOMES']['0']['#']); $dbrec->aggregatesubcats = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATESUBCATS']['0']['#']); $dbrec->courseid = $restore->course_id; insert_record('grade_categories_history', $dbrec); unset($dbrec); } //Increment counters $counter++; //Do some output if ($counter % 1 == 0) { if (!defined('RESTORE_SILENTLY')) { echo "."; if ($counter % 20 == 0) { echo "<br />"; } } backup_flush(300); } } } } } // process histories if ($gghcount && $status) { if (!defined('RESTORE_SILENTLY')) { echo '<li>' . get_string('gradegradeshistory', 'grades') . '</li>'; } $counter = 0; while ($counter < $gghcount) { //Fetch recordset_size records in each iteration $recs = get_records_select("backup_ids", "table_name = 'grade_grades_history' AND backup_code = '{$restore->backup_unique_code}'", "old_id", "old_id", $counter, $recordset_size); if ($recs) { foreach ($recs as $rec) { //Get the full record from backup_ids $data = backup_getid($restore->backup_unique_code, 'grade_grades_history', $rec->old_id); if ($data) { //Now get completed xmlized object $info = $data->info; //traverse_xmlize($info); //Debug //print_object ($GLOBALS['traverse_array']); //Debug //$GLOBALS['traverse_array']=""; //Debug $oldobj = backup_getid($restore->backup_unique_code, "grade_grades", backup_todb($info['GRADE_GRADES_HISTORY']['#']['OLDID']['0']['#'])); if (empty($oldobj->new_id)) { // if the old object is not being restored, can't restoring its history $counter++; continue; } $dbrec->oldid = $oldobj->new_id; $dbrec->action = backup_todb($info['GRADE_GRADES_HISTORY']['#']['ACTION']['0']['#']); $dbrec->source = backup_todb($info['GRADE_GRADES_HISTORY']['#']['SOURCE']['0']['#']); $dbrec->timemodified = backup_todb($info['GRADE_GRADES_HISTORY']['#']['TIMEMODIFIED']['0']['#']); if ($oldobj = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) { $dbrec->loggeduser = $oldobj->new_id; } $oldobj = backup_getid($restore->backup_unique_code, "grade_items", backup_todb($info['GRADE_GRADES_HISTORY']['#']['ITEMID']['0']['#'])); $dbrec->itemid = $oldobj->new_id; if (empty($dbrec->itemid)) { $counter++; continue; // grade item not being restored } $oldobj = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERID']['0']['#'])); $dbrec->userid = $oldobj->new_id; $dbrec->rawgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADE']['0']['#']); $dbrec->rawgrademax = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMAX']['0']['#']); $dbrec->rawgrademin = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMIN']['0']['#']); if ($oldobj = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERMODIFIED']['0']['#']))) { $dbrec->usermodified = $oldobj->new_id; } if (backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWSCALEID']['0']['#'])) { $scale = backup_getid($restore->backup_unique_code, "scale", backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWSCALEID']['0']['#'])); $dbrec->rawscaleid = $scale->new_id; } $dbrec->finalgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['FINALGRADE']['0']['#']); $dbrec->hidden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['HIDDEN']['0']['#']); $dbrec->locked = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKED']['0']['#']); $dbrec->locktime = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKTIME']['0']['#']); $dbrec->exported = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXPORTED']['0']['#']); $dbrec->overridden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['OVERRIDDEN']['0']['#']); $dbrec->excluded = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXCLUDED']['0']['#']); $dbrec->feedback = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACK']['0']['#']); $dbrec->feedbackformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACKFORMAT']['0']['#']); $dbrec->information = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATION']['0']['#']); $dbrec->informationformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATIONFORMAT']['0']['#']); insert_record('grade_grades_history', $dbrec); unset($dbrec); } //Increment counters $counter++; //Do some output if ($counter % 1 == 0) { if (!defined('RESTORE_SILENTLY')) { echo "."; if ($counter % 20 == 0) { echo "<br />"; } } backup_flush(300); } } } } } // process histories if ($gihcount && $status) { if (!defined('RESTORE_SILENTLY')) { echo '<li>' . get_string('gradeitemshistory', 'grades') . '</li>'; } $counter = 0; while ($counter < $gihcount) { //Fetch recordset_size records in each iteration $recs = get_records_select("backup_ids", "table_name = 'grade_items_history' AND backup_code = '{$restore->backup_unique_code}'", "old_id", "old_id", $counter, $recordset_size); if ($recs) { foreach ($recs as $rec) { //Get the full record from backup_ids $data = backup_getid($restore->backup_unique_code, 'grade_items_history', $rec->old_id); if ($data) { //Now get completed xmlized object $info = $data->info; //traverse_xmlize($info); //Debug //print_object ($GLOBALS['traverse_array']); //Debug //$GLOBALS['traverse_array']=""; //Debug $oldobj = backup_getid($restore->backup_unique_code, "grade_items", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OLDID']['0']['#'])); if (empty($oldobj->new_id)) { // if the old object is not being restored, can't restoring its history $counter++; continue; } $dbrec->oldid = $oldobj->new_id; $dbrec->action = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ACTION']['0']['#']); $dbrec->source = backup_todb($info['GRADE_ITEM_HISTORY']['#']['SOURCE']['0']['#']); $dbrec->timemodified = backup_todb($info['GRADE_ITEM_HISTORY']['#']['TIMEMODIFIED']['0']['#']); if ($oldobj = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOGGEDUSER']['0']['#']))) { $dbrec->loggeduser = $oldobj->new_id; } $dbrec->courseid = $restore->course_id; $oldobj = backup_getid($restore->backup_unique_code, 'grade_categories', backup_todb($info['GRADE_ITEM_HISTORY']['#']['CATEGORYID']['0']['#'])); $oldobj->categoryid = $category->new_id; if (empty($oldobj->categoryid)) { $counter++; continue; // category not restored } $dbrec->itemname = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNAME']['0']['#']); $dbrec->itemtype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMTYPE']['0']['#']); $dbrec->itemmodule = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMMODULE']['0']['#']); // code from grade_items restore $iteminstance = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINSTANCE']['0']['#']); // do not restore if this grade_item is a mod, and if ($dbrec->itemtype == 'mod') { if (!restore_userdata_selected($restore, $dbrec->itemmodule, $iteminstance)) { // module instance not selected when restored using granular // skip this item $counter++; continue; } // iteminstance should point to new mod $mod = backup_getid($restore->backup_unique_code, $dbrec->itemmodule, $iteminstance); $dbrec->iteminstance = $mod->new_id; } else { if ($dbrec->itemtype == 'category') { // the item instance should point to the new grade category // only proceed if we are restoring all grade items if ($restoreall) { $category = backup_getid($restore->backup_unique_code, 'grade_categories', $iteminstance); $dbrec->iteminstance = $category->new_id; } else { // otherwise we can safely ignore this grade item and subsequent // grade_raws, grade_finals etc continue; } } elseif ($dbrec->itemtype == 'course') { // We don't restore course type to avoid duplicate course items if ($restoreall) { // TODO any special code needed here to restore course item without duplicating it? // find the course category with depth 1, and course id = current course id // this would have been already restored $cat = get_record('grade_categories', 'depth', 1, 'courseid', $restore->course_id); $dbrec->iteminstance = $cat->id; } else { $counter++; continue; } } } $dbrec->itemnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNUMBER']['0']['#']); $dbrec->iteminfo = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINFO']['0']['#']); $dbrec->idnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['IDNUMBER']['0']['#']); $dbrec->calculation = backup_todb($info['GRADE_ITEM_HISTORY']['#']['CALCULATION']['0']['#']); $dbrec->gradetype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADETYPE']['0']['#']); $dbrec->grademax = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMAX']['0']['#']); $dbrec->grademin = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMIN']['0']['#']); if ($oldobj = backup_getid($restore->backup_unique_code, "scale", backup_todb($info['GRADE_ITEM_HISTORY']['#']['SCALEID']['0']['#']))) { // scaleid is optional $dbrec->scaleid = $oldobj->new_id; } if ($oldobj = backup_getid($restore->backup_unique_code, "grade_outcomes", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OUTCOMEID']['0']['#']))) { // outcome is optional $dbrec->outcomeid = $oldobj->new_id; } $dbrec->gradepass = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEPASS']['0']['#']); $dbrec->multfactor = backup_todb($info['GRADE_ITEM_HISTORY']['#']['MULTFACTOR']['0']['#']); $dbrec->plusfactor = backup_todb($info['GRADE_ITEM_HISTORY']['#']['PLUSFACTOR']['0']['#']); $dbrec->aggregationcoef = backup_todb($info['GRADE_ITEM_HISTORY']['#']['AGGREGATIONCOEF']['0']['#']); $dbrec->sortorder = backup_todb($info['GRADE_ITEM_HISTORY']['#']['SORTORDER']['0']['#']); $dbrec->display = backup_todb($info['GRADE_ITEM_HISTORY']['#']['DISPLAY']['0']['#']); $dbrec->decimals = backup_todb($info['GRADE_ITEM_HISTORY']['#']['DECIMALS']['0']['#']); $dbrec->hidden = backup_todb($info['GRADE_ITEM_HISTORY']['#']['HIDDEN']['0']['#']); $dbrec->locked = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKED']['0']['#']); $dbrec->locktime = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKTIME']['0']['#']); $dbrec->needsupdate = backup_todb($info['GRADE_ITEM_HISTORY']['#']['NEEDSUPDATE']['0']['#']); insert_record('grade_items_history', $dbrec); unset($dbrec); } //Increment counters $counter++; //Do some output if ($counter % 1 == 0) { if (!defined('RESTORE_SILENTLY')) { echo "."; if ($counter % 20 == 0) { echo "<br />"; } } backup_flush(300); } } } } } // process histories if ($gohcount && $status) { if (!defined('RESTORE_SILENTLY')) { echo '<li>' . get_string('gradeoutcomeshistory', 'grades') . '</li>'; } $counter = 0; while ($counter < $gohcount) { //Fetch recordset_size records in each iteration $recs = get_records_select("backup_ids", "table_name = 'grade_outcomes_history' AND backup_code = '{$restore->backup_unique_code}'", "old_id", "old_id", $counter, $recordset_size); if ($recs) { foreach ($recs as $rec) { //Get the full record from backup_ids $data = backup_getid($restore->backup_unique_code, 'grade_outcomes_history', $rec->old_id); if ($data) { //Now get completed xmlized object $info = $data->info; //traverse_xmlize($info); //Debug //print_object ($GLOBALS['traverse_array']); //Debug //$GLOBALS['traverse_array']=""; //Debug $oldobj = backup_getid($restore->backup_unique_code, "grade_outcomes", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['OLDID']['0']['#'])); if (empty($oldobj->new_id)) { // if the old object is not being restored, can't restoring its history $counter++; continue; } $dbrec->oldid = $oldobj->new_id; $dbrec->action = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['ACTION']['0']['#']); $dbrec->source = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SOURCE']['0']['#']); $dbrec->timemodified = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['TIMEMODIFIED']['0']['#']); if ($oldobj = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['LOGGEDUSER']['0']['#']))) { $dbrec->loggeduser = $oldobj->new_id; } $dbrec->courseid = $restore->course_id; $dbrec->shortname = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SHORTNAME']['0']['#']); $dbrec->fullname = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['FULLNAME']['0']['#']); $oldobj = backup_getid($restore->backup_unique_code, "scale", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SCALEID']['0']['#'])); $dbrec->scaleid = $oldobj->new_id; $dbrec->description = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['DESCRIPTION']['0']['#']); insert_record('grade_outcomes_history', $dbrec); unset($dbrec); } //Increment counters $counter++; //Do some output if ($counter % 1 == 0) { if (!defined('RESTORE_SILENTLY')) { echo "."; if ($counter % 20 == 0) { echo "<br />"; } } backup_flush(300); } } } } } } if (!defined('RESTORE_SILENTLY')) { //End ul echo '</ul>'; } return $status; }
/** * Determines whether the grade tree item can be displayed. * This is particularly targeted for grade categories that have no total (None) when rendering the grade tree. * It checks if the grade tree item is of type 'category', and makes sure that the category, or at least one of children, * can be output. * * @param array $element The grade category element. * @return bool True if the grade tree item can be displayed. False, otherwise. */ public static function can_output_item($element) { $canoutput = true; if ($element['type'] === 'category') { $object = $element['object']; $category = grade_category::fetch(array('id' => $object->id)); // Category has total, we can output this. if ($category->get_grade_item()->gradetype != GRADE_TYPE_NONE) { return true; } // Category has no total and has no children, no need to output this. if (empty($element['children'])) { return false; } $canoutput = false; // Loop over children and make sure at least one child can be output. foreach ($element['children'] as $child) { $canoutput = self::can_output_item($child); if ($canoutput) { break; } } } return $canoutput; }
protected function process_grade_item($data) { global $DB; $data = (object) $data; $oldid = $data->id; // We'll need these later $oldparentid = $data->categoryid; $courseid = $this->get_courseid(); // make sure top course category exists, all grade items will be associated // to it. Later, if restoring the whole gradebook, categories will be introduced $coursecat = grade_category::fetch_course_category($courseid); $coursecatid = $coursecat->id; // Get the categoryid to be used $idnumber = null; if (!empty($data->idnumber)) { // Don't get any idnumber from course module. Keep them as they are in grade_item->idnumber // Reason: it's not clear what happens with outcomes->idnumber or activities with multiple items (workshop) // so the best is to keep the ones already in the gradebook // Potential problem: duplicates if same items are restored more than once. :-( // This needs to be fixed in some way (outcomes & activities with multiple items) // $data->idnumber = get_coursemodule_from_instance($data->itemmodule, $data->iteminstance)->idnumber; // In any case, verify always for uniqueness $sql = "SELECT cm.id\n FROM {course_modules} cm\n WHERE cm.course = :courseid AND\n cm.idnumber = :idnumber AND\n cm.id <> :cmid"; $params = array('courseid' => $courseid, 'idnumber' => $data->idnumber, 'cmid' => $this->task->get_moduleid()); if (!$DB->record_exists_sql($sql, $params) && !$DB->record_exists('grade_items', array('courseid' => $courseid, 'idnumber' => $data->idnumber))) { $idnumber = $data->idnumber; } } unset($data->id); $data->categoryid = $coursecatid; $data->courseid = $this->get_courseid(); $data->iteminstance = $this->task->get_activityid(); $data->idnumber = $idnumber; $data->scaleid = $this->get_mappingid('scale', $data->scaleid); $data->outcomeid = $this->get_mappingid('outcome', $data->outcomeid); $data->timecreated = $this->apply_date_offset($data->timecreated); $data->timemodified = $this->apply_date_offset($data->timemodified); $gradeitem = new grade_item($data, false); $gradeitem->insert('restore'); //sortorder is automatically assigned when inserting. Re-instate the previous sortorder $gradeitem->sortorder = $data->sortorder; $gradeitem->update('restore'); // Set mapping, saving the original category id into parentitemid // gradebook restore (final task) will need it to reorganise items $this->set_mapping('grade_item', $oldid, $gradeitem->id, false, null, $oldparentid); }
/** * 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); }
protected function sub_test_grade_item_set_parent() { $grade_item = new grade_item($this->grade_items[0], false); $this->assertTrue(method_exists($grade_item, 'set_parent')); $old = $grade_item->get_parent_category(); $new = new grade_category($this->grade_categories[3], false); $new_item = $new->get_grade_item(); $this->assertTrue($grade_item->set_parent($new->id)); $new_item->update_from_db(); $grade_item->update_from_db(); $this->assertEquals($grade_item->categoryid, $new->id); }
/** * Common create/update module module actions that need to be processed as soon as a module is created/updaded. * For example:create grade parent category, add outcomes, rebuild caches, regrade, save plagiarism settings... * Please note this api does not trigger events as of MOODLE 2.6. Please trigger events before calling this api. * * @param object $moduleinfo the module info * @param object $course the course of the module * * @return object moduleinfo update with grading management info */ function edit_module_post_actions($moduleinfo, $course) { global $CFG; require_once $CFG->libdir . '/gradelib.php'; $modcontext = context_module::instance($moduleinfo->coursemodule); $hasgrades = plugin_supports('mod', $moduleinfo->modulename, FEATURE_GRADE_HAS_GRADE, false); $hasoutcomes = plugin_supports('mod', $moduleinfo->modulename, FEATURE_GRADE_OUTCOMES, true); // Sync idnumber with grade_item. if ($hasgrades && ($grade_item = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => $moduleinfo->modulename, 'iteminstance' => $moduleinfo->instance, 'itemnumber' => 0, 'courseid' => $course->id)))) { if ($grade_item->idnumber != $moduleinfo->cmidnumber) { $grade_item->idnumber = $moduleinfo->cmidnumber; $grade_item->update(); } } if ($hasgrades) { $items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $moduleinfo->modulename, 'iteminstance' => $moduleinfo->instance, 'courseid' => $course->id)); } else { $items = array(); } // Create parent category if requested and move to correct parent category. if ($items and isset($moduleinfo->gradecat)) { if ($moduleinfo->gradecat == -1) { $grade_category = new grade_category(); $grade_category->courseid = $course->id; $grade_category->fullname = $moduleinfo->name; $grade_category->insert(); if ($grade_item) { $parent = $grade_item->get_parent_category(); $grade_category->set_parent($parent->id); } $moduleinfo->gradecat = $grade_category->id; } $gradecategory = $grade_item->get_parent_category(); foreach ($items as $itemid => $unused) { $items[$itemid]->set_parent($moduleinfo->gradecat); if ($itemid == $grade_item->id) { // Use updated grade_item. $grade_item = $items[$itemid]; } if (!empty($moduleinfo->add)) { if (grade_category::aggregation_uses_aggregationcoef($gradecategory->aggregation)) { if ($gradecategory->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) { $grade_item->aggregationcoef = 1; } else { $grade_item->aggregationcoef = 0; } $grade_item->update(); } } } } require_once $CFG->libdir . '/grade/grade_outcome.php'; // Add outcomes if requested. if ($hasoutcomes && ($outcomes = grade_outcome::fetch_all_available($course->id))) { $grade_items = array(); // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes. $max_itemnumber = 999; if ($items) { foreach ($items as $item) { if ($item->itemnumber > $max_itemnumber) { $max_itemnumber = $item->itemnumber; } } } foreach ($outcomes as $outcome) { $elname = 'outcome_' . $outcome->id; if (property_exists($moduleinfo, $elname) and $moduleinfo->{$elname}) { // So we have a request for new outcome grade item? if ($items) { $outcomeexists = false; foreach ($items as $item) { if ($item->outcomeid == $outcome->id) { $outcomeexists = true; break; } } if ($outcomeexists) { continue; } } $max_itemnumber++; $outcome_item = new grade_item(); $outcome_item->courseid = $course->id; $outcome_item->itemtype = 'mod'; $outcome_item->itemmodule = $moduleinfo->modulename; $outcome_item->iteminstance = $moduleinfo->instance; $outcome_item->itemnumber = $max_itemnumber; $outcome_item->itemname = $outcome->fullname; $outcome_item->outcomeid = $outcome->id; $outcome_item->gradetype = GRADE_TYPE_SCALE; $outcome_item->scaleid = $outcome->scaleid; $outcome_item->insert(); // Move the new outcome into correct category and fix sortorder if needed. if ($grade_item) { $outcome_item->set_parent($grade_item->categoryid); $outcome_item->move_after_sortorder($grade_item->sortorder); } else { if (isset($moduleinfo->gradecat)) { $outcome_item->set_parent($moduleinfo->gradecat); } } $gradecategory = $outcome_item->get_parent_category(); if ($outcomeexists == false) { if (grade_category::aggregation_uses_aggregationcoef($gradecategory->aggregation)) { if ($gradecategory->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) { $outcome_item->aggregationcoef = 1; } else { $outcome_item->aggregationcoef = 0; } $outcome_item->update(); } } } } } if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_ADVANCED_GRADING, false) and has_capability('moodle/grade:managegradingforms', $modcontext)) { require_once $CFG->dirroot . '/grade/grading/lib.php'; $gradingman = get_grading_manager($modcontext, 'mod_' . $moduleinfo->modulename); $showgradingmanagement = false; foreach ($gradingman->get_available_areas() as $areaname => $aretitle) { $formfield = 'advancedgradingmethod_' . $areaname; if (isset($moduleinfo->{$formfield})) { $gradingman->set_area($areaname); $methodchanged = $gradingman->set_active_method($moduleinfo->{$formfield}); if (empty($moduleinfo->{$formfield})) { // Going back to the simple direct grading is not a reason to open the management screen. $methodchanged = false; } $showgradingmanagement = $showgradingmanagement || $methodchanged; } } // Update grading management information. $moduleinfo->gradingman = $gradingman; $moduleinfo->showgradingmanagement = $showgradingmanagement; } rebuild_course_cache($course->id, true); if ($hasgrades) { grade_regrade_final_grades($course->id); } require_once $CFG->libdir . '/plagiarismlib.php'; plagiarism_save_form_elements($moduleinfo); return $moduleinfo; }
/** * Sets the grade_item's hidden variable and updates the grade_item. * Method named after grade_item::set_hidden(). * @param int $hidden 0, 1 or a timestamp int(10) after which date the item will be hidden. * @param boolean $cascade apply to child objects too * @return void */ function set_hidden($hidden, $cascade = false) { $this->load_grade_item(); $this->grade_item->set_hidden($hidden); if ($cascade) { if ($children = grade_item::fetch_all(array('categoryid' => $this->id))) { foreach ($children as $child) { $child->set_hidden($hidden, $cascade); } } if ($children = grade_category::fetch_all(array('parent' => $this->id))) { foreach ($children as $child) { $child->set_hidden($hidden, $cascade); } } } }
} if ($parent_category->aggregation == GRADE_AGGREGATE_SUM) { $item->aggregationcoef2 = format_float($item->aggregationcoef2 * 100.0); } $item->cancontrolvisibility = $grade_item->can_control_visibility(); $mform = new edit_item_form(null, array('current' => $item, 'gpr' => $gpr)); if ($mform->is_cancelled()) { redirect($returnurl); } else { if ($data = $mform->get_data(false)) { // This is a new item, and the category chosen is different than the default category. if (empty($grade_item->id) && isset($data->parentcategory) && $parent_category->id != $data->parentcategory) { $parent_category = grade_category::fetch(array('id' => $data->parentcategory)); } // If unset, give the aggregation values a default based on parent aggregation method. $defaults = grade_category::get_default_aggregation_coefficient_values($parent_category->aggregation); if (!isset($data->aggregationcoef) || $data->aggregationcoef == '') { $data->aggregationcoef = $defaults['aggregationcoef']; } if (!isset($data->weightoverride)) { $data->weightoverride = $defaults['weightoverride']; } if (!isset($data->gradepass) || $data->gradepass == '') { $data->gradepass = 0; } if (!isset($data->grademin) || $data->grademin == '') { $data->grademin = 0; } $hidden = empty($data->hidden) ? 0 : $data->hidden; $hiddenuntil = empty($data->hiddenuntil) ? 0 : $data->hiddenuntil; unset($data->hidden);
/** * Update the grade items categories if they are changed via mod_form.php * * We must do it manually here in the workshopplus module because modedit supports only * single grade item while we use two. * * @param stdClass $workshopplus An object from the form in mod_form.php */ function workshopplus_grade_item_category_update($workshopplus) { $gradeitems = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => 'workshopplus', 'iteminstance' => $workshopplus->id, 'courseid' => $workshopplus->course)); if (!empty($gradeitems)) { foreach ($gradeitems as $gradeitem) { if ($gradeitem->itemnumber == 0) { if ($gradeitem->categoryid != $workshopplus->gradecategory) { $gradeitem->set_parent($workshopplus->gradecategory); } } else { if ($gradeitem->itemnumber == 1) { if ($gradeitem->categoryid != $workshopplus->gradinggradecategory) { $gradeitem->set_parent($workshopplus->gradinggradecategory); } } } if (!empty($workshopplus->add)) { $gradecategory = $gradeitem->get_parent_category(); if (grade_category::aggregation_uses_aggregationcoef($gradecategory->aggregation)) { if ($gradecategory->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) { $gradeitem->aggregationcoef = 1; } else { $gradeitem->aggregationcoef = 0; } $gradeitem->update(); } } } } }
/** * Load grade_item data into the database, and adds the corresponding objects to this class' variable. */ function load_grade_items() { global $DB; $course_category = grade_category::fetch_course_category($this->courseid); // id = 0 $grade_item = new stdClass(); $grade_item->courseid = $this->courseid; $grade_item->categoryid = $this->grade_categories[1]->id; $grade_item->itemname = 'unittestgradeitem1'; $grade_item->itemtype = 'mod'; $grade_item->itemmodule = 'quiz'; $grade_item->iteminstance = 1; $grade_item->gradetype = GRADE_TYPE_VALUE; $grade_item->grademin = 30; $grade_item->grademax = 110; $grade_item->itemnumber = 1; $grade_item->idnumber = 'item id 0'; $grade_item->iteminfo = 'Grade item 0 used for unit testing'; $grade_item->timecreated = mktime(); $grade_item->timemodified = mktime(); $grade_item->sortorder = 3; $grade_item->id = $DB->insert_record('grade_items', $grade_item); $this->grade_items[0] = $grade_item; // id = 1 $grade_item = new stdClass(); $grade_item->courseid = $this->courseid; $grade_item->categoryid = $this->grade_categories[1]->id; $grade_item->itemname = 'unittestgradeitem2'; $grade_item->itemtype = 'import'; $grade_item->itemmodule = 'assignment'; $grade_item->calculation = '= ##gi' . $this->grade_items[0]->id . '## + 30 + [[item id 0]] - [[item id 0]]'; $grade_item->gradetype = GRADE_TYPE_VALUE; $grade_item->iteminstance = 2; $grade_item->itemnumber = null; $grade_item->grademin = 0; $grade_item->grademax = 100; $grade_item->iteminfo = 'Grade item 1 used for unit testing'; $grade_item->timecreated = mktime(); $grade_item->timemodified = mktime(); $grade_item->sortorder = 4; $grade_item->id = $DB->insert_record('grade_items', $grade_item); $this->grade_items[1] = $grade_item; // id = 2 $grade_item = new stdClass(); $grade_item->courseid = $this->courseid; $grade_item->categoryid = $this->grade_categories[2]->id; $grade_item->itemname = 'unittestgradeitem3'; $grade_item->itemtype = 'mod'; $grade_item->itemmodule = 'forum'; $grade_item->iteminstance = 3; $grade_item->gradetype = GRADE_TYPE_SCALE; $grade_item->scaleid = $this->scale[0]->id; $grade_item->grademin = 0; $grade_item->grademax = $this->scalemax[0]; $grade_item->iteminfo = 'Grade item 2 used for unit testing'; $grade_item->timecreated = mktime(); $grade_item->timemodified = mktime(); $grade_item->sortorder = 6; $grade_item->id = $DB->insert_record('grade_items', $grade_item); $this->grade_items[2] = $grade_item; // Load grade_items associated with the 3 categories // id = 3 $grade_item = new stdClass(); $grade_item->courseid = $this->courseid; $grade_item->iteminstance = $this->grade_categories[0]->id; $grade_item->itemname = 'unittestgradeitemcategory1'; $grade_item->needsupdate = 0; $grade_item->itemtype = 'category'; $grade_item->gradetype = GRADE_TYPE_VALUE; $grade_item->grademin = 0; $grade_item->grademax = 100; $grade_item->iteminfo = 'Grade item 3 used for unit testing'; $grade_item->timecreated = mktime(); $grade_item->timemodified = mktime(); $grade_item->sortorder = 1; $grade_item->id = $DB->insert_record('grade_items', $grade_item); $this->grade_items[3] = $grade_item; // id = 4 $grade_item = new stdClass(); $grade_item->courseid = $this->courseid; $grade_item->iteminstance = $this->grade_categories[1]->id; $grade_item->itemname = 'unittestgradeitemcategory2'; $grade_item->itemtype = 'category'; $grade_item->gradetype = GRADE_TYPE_VALUE; $grade_item->needsupdate = 0; $grade_item->grademin = 0; $grade_item->grademax = 100; $grade_item->iteminfo = 'Grade item 4 used for unit testing'; $grade_item->timecreated = mktime(); $grade_item->timemodified = mktime(); $grade_item->sortorder = 2; $grade_item->id = $DB->insert_record('grade_items', $grade_item); $this->grade_items[4] = $grade_item; // id = 5 $grade_item = new stdClass(); $grade_item->courseid = $this->courseid; $grade_item->iteminstance = $this->grade_categories[2]->id; $grade_item->itemname = 'unittestgradeitemcategory3'; $grade_item->itemtype = 'category'; $grade_item->gradetype = GRADE_TYPE_VALUE; $grade_item->needsupdate = true; $grade_item->grademin = 0; $grade_item->grademax = 100; $grade_item->iteminfo = 'Grade item 5 used for unit testing'; $grade_item->timecreated = mktime(); $grade_item->timemodified = mktime(); $grade_item->sortorder = 5; $grade_item->id = $DB->insert_record('grade_items', $grade_item); $this->grade_items[5] = $grade_item; // Orphan grade_item // id = 6 $grade_item = new stdClass(); $grade_item->courseid = $this->courseid; $grade_item->categoryid = $course_category->id; $grade_item->itemname = 'unittestorphangradeitem1'; $grade_item->itemtype = 'mod'; $grade_item->itemmodule = 'quiz'; $grade_item->iteminstance = 5; $grade_item->itemnumber = 0; $grade_item->gradetype = GRADE_TYPE_VALUE; $grade_item->grademin = 10; $grade_item->grademax = 120; $grade_item->locked = time(); $grade_item->iteminfo = 'Orphan Grade 6 item used for unit testing'; $grade_item->timecreated = mktime(); $grade_item->timemodified = mktime(); $grade_item->sortorder = 7; $grade_item->id = $DB->insert_record('grade_items', $grade_item); $this->grade_items[6] = $grade_item; // 2 grade items under level1category // id = 7 $grade_item = new stdClass(); $grade_item->courseid = $this->courseid; $grade_item->categoryid = $this->grade_categories[3]->id; $grade_item->itemname = 'singleparentitem1'; $grade_item->itemtype = 'mod'; $grade_item->itemmodule = 'forum'; $grade_item->iteminstance = 7; $grade_item->gradetype = GRADE_TYPE_SCALE; $grade_item->scaleid = $this->scale[0]->id; $grade_item->grademin = 0; $grade_item->grademax = $this->scalemax[0]; $grade_item->iteminfo = 'Grade item 7 used for unit testing'; $grade_item->timecreated = mktime(); $grade_item->timemodified = mktime(); $grade_item->sortorder = 9; $grade_item->id = $DB->insert_record('grade_items', $grade_item); $this->grade_items[7] = $grade_item; // id = 8 $grade_item = new stdClass(); $grade_item->courseid = $this->courseid; $grade_item->categoryid = $this->grade_categories[3]->id; $grade_item->itemname = 'singleparentitem2'; $grade_item->itemtype = 'mod'; $grade_item->itemmodule = 'forum'; $grade_item->iteminstance = 9; $grade_item->gradetype = GRADE_TYPE_VALUE; $grade_item->grademin = 0; $grade_item->grademax = 100; $grade_item->iteminfo = 'Grade item 8 used for unit testing'; $grade_item->timecreated = mktime(); $grade_item->timemodified = mktime(); $grade_item->sortorder = 10; $grade_item->id = $DB->insert_record('grade_items', $grade_item); $this->grade_items[8] = $grade_item; // Grade_item for level1category // id = 9 $grade_item = new stdClass(); $grade_item->courseid = $this->courseid; $grade_item->itemname = 'grade_item for level1 category'; $grade_item->itemtype = 'category'; $grade_item->itemmodule = 'quiz'; $grade_item->iteminstance = $this->grade_categories[3]->id; $grade_item->needsupdate = true; $grade_item->gradetype = GRADE_TYPE_VALUE; $grade_item->grademin = 0; $grade_item->grademax = 100; $grade_item->iteminfo = 'Orphan Grade item 9 used for unit testing'; $grade_item->timecreated = mktime(); $grade_item->timemodified = mktime(); $grade_item->sortorder = 8; $grade_item->id = $DB->insert_record('grade_items', $grade_item); $this->grade_items[9] = $grade_item; // Manual grade_item // id = 10 $grade_item = new stdClass(); $grade_item->courseid = $this->courseid; $grade_item->categoryid = $course_category->id; $grade_item->itemname = 'manual grade_item'; $grade_item->itemtype = 'manual'; $grade_item->itemnumber = 0; $grade_item->needsupdate = false; $grade_item->gradetype = GRADE_TYPE_VALUE; $grade_item->grademin = 0; $grade_item->grademax = 100; $grade_item->iteminfo = 'Manual grade item 10 used for unit testing'; $grade_item->timecreated = mktime(); $grade_item->timemodified = mktime(); $grade_item->id = $DB->insert_record('grade_items', $grade_item); $this->grade_items[10] = $grade_item; }
function test_upgrade_calculated_grade_items_regrade() { global $DB, $CFG; $this->resetAfterTest(); // Create a user. $user = $this->getDataGenerator()->create_user(); // Create a course. $course = $this->getDataGenerator()->create_course(); // Enrol the user in the course. $studentrole = $DB->get_record('role', array('shortname' => 'student')); $maninstance1 = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST); $manual = enrol_get_plugin('manual'); $manual->enrol_user($maninstance1, $user->id, $studentrole->id); set_config('upgrade_calculatedgradeitemsonlyregrade', 1); // Creating a category for a grade item. $gradecategory = new grade_category(); $gradecategory->fullname = 'calculated grade category'; $gradecategory->courseid = $course->id; $gradecategory->insert(); $gradecategoryid = $gradecategory->id; // This is a manual grade item. $gradeitem = new grade_item(); $gradeitem->itemname = 'grade item one'; $gradeitem->itemtype = 'manual'; $gradeitem->categoryid = $gradecategoryid; $gradeitem->courseid = $course->id; $gradeitem->idnumber = 'gi1'; $gradeitem->insert(); // Changing the category into a calculated grade category. $gradecategoryitem = grade_item::fetch(array('iteminstance' => $gradecategory->id)); $gradecategoryitem->calculation = '=##gi' . $gradeitem->id . '##/2'; $gradecategoryitem->grademax = 50; $gradecategoryitem->grademin = 15; $gradecategoryitem->update(); // Setting a grade for the student. $grade = $gradeitem->get_grade($user->id, true); $grade->finalgrade = 50; $grade->update(); grade_regrade_final_grades($course->id); $grade = grade_grade::fetch(array('itemid' => $gradecategoryitem->id, 'userid' => $user->id)); $grade->rawgrademax = 100; $grade->rawgrademin = 0; $grade->update(); $this->assertNotEquals($gradecategoryitem->grademax, $grade->rawgrademax); $this->assertNotEquals($gradecategoryitem->grademin, $grade->rawgrademin); // This is the function that we are testing. If we comment out this line, then the test fails because the grade items // are not flagged for regrading. upgrade_calculated_grade_items(); grade_regrade_final_grades($course->id); $grade = grade_grade::fetch(array('itemid' => $gradecategoryitem->id, 'userid' => $user->id)); $this->assertEquals($gradecategoryitem->grademax, $grade->rawgrademax); $this->assertEquals($gradecategoryitem->grademin, $grade->rawgrademin); }
/** * Tests the gradebookservice update grade function for adding an item in a category, * where multiple categories with the same name already exist. The item should be assigned * to the oldest category. When moved to a category with a different name, the item should * be assigned to the new category. No tasks should be created and no locking should be triggered. * * @return void */ public function test_update_item_multiple_categories() { global $DB, $CFG; $this->set_user('admin'); // No tasks. $this->assertEquals(0, $DB->count_records('task_adhoc')); $catname = 'testcat'; $catname1 = 'testcat1'; $catparams = array('fullname' => $catname, 'courseid' => $this->course->id, 'hidden' => false); // Create category. $category = new \grade_category($catparams, false); $categoryid1 = $category->insert(); // Create second category with the same name. $category = new \grade_category($catparams, false); $categoryid2 = $category->insert(); // Create another category with the different name. $catparams['fullname'] = $catname1; $category = new \grade_category($catparams, false); $categoryid3 = $category->insert(); // Add item 101 to $catname. $this->add_grade_item('101', $catname); // No tasks. $this->assertEquals(0, $DB->count_records('task_adhoc')); // The items should be assigned to category 1. $params = array('iteminstance' => '101', 'categoryid' => $categoryid1, 'courseid' => $this->course->id); $this->assertEquals(1, $DB->count_records('grade_items', $params)); // Add item 102 to $catname. $this->add_grade_item('102', $catname); // No tasks. $this->assertEquals(0, $DB->count_records('task_adhoc')); // The item should be assigned to the category 1. $params = array('iteminstance' => '102', 'categoryid' => $categoryid1, 'courseid' => $this->course->id); $this->assertEquals(1, $DB->count_records('grade_items', $params)); // Add item 103 to $catname1. $this->add_grade_item('103', $catname1); // No tasks. $this->assertEquals(0, $DB->count_records('task_adhoc')); // The item should be assigned to the category 3. $params = array('iteminstance' => '103', 'categoryid' => $categoryid3, 'courseid' => $this->course->id); $this->assertEquals(1, $DB->count_records('grade_items', $params)); // Move item 102 to $catname1. $this->add_grade_item('102', $catname1); // No tasks. $this->assertEquals(0, $DB->count_records('task_adhoc')); // There should be two items assigned to the category 3. $params = array('categoryid' => $categoryid3, 'courseid' => $this->course->id); $this->assertEquals(2, $DB->count_records('grade_items', $params)); // The item should be assigned to the category 3. $params['iteminstance'] = '102'; $this->assertEquals(1, $DB->count_records('grade_items', $params)); }
/** * Create a grade_category. * * @param array|stdClass $record * @return stdClass the grade category record */ public function create_grade_category($record = null) { global $CFG; $this->gradecategorycounter++; $i = $this->gradecategorycounter; $record = (array) $record; if (empty($record['courseid'])) { throw new coding_exception('courseid must be present in testing::create_grade_category() $record'); } if (!isset($record['fullname'])) { $record['fullname'] = 'Grade category ' . $i; } // For gradelib classes. require_once $CFG->libdir . '/gradelib.php'; // Create new grading category in this course. $gradecategory = new grade_category(array('courseid' => $record['courseid']), false); $gradecategory->apply_default_settings(); grade_category::set_properties($gradecategory, $record); $gradecategory->apply_forced_settings(); $gradecategory->insert(); // This creates a default grade item for the category $gradeitem = $gradecategory->load_grade_item(); $gradecategory->update_from_db(); return $gradecategory->get_record_data(); }
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; }
/** * Recurses through the entire grade tree to find and return the maximum depth of the tree. * This should be run only once from the root element (course category), and is used for the * indentation of the Name column's cells (colspan) * * @param array $element An array of values representing a grade tree's element (all grade items in this case) * @param int $level The level of the current recursion * @param int $deepest_level A value passed to each subsequent level of recursion and incremented if $level > $deepest_level * @return int Deepest level */ function get_deepest_level($element, $level = 0, $deepest_level = 1) { $object = $element['object']; $level++; $coefstring = $element['object']->get_coefstring(); if ($element['type'] == 'category') { if ($coefstring == 'aggregationcoefweight' || $coefstring == 'aggregationcoefextraweightsum' || $coefstring == 'aggregationcoefextraweight') { $this->uses_weight = true; } foreach ($element['children'] as $child_el) { if ($level > $deepest_level) { $deepest_level = $level; } $deepest_level = $this->get_deepest_level($child_el, $level, $deepest_level); } $category = grade_category::fetch(array('id' => $object->id)); $item = $category->get_grade_item(); if ($item->gradetype == GRADE_TYPE_NONE) { // Add 1 more level for grade category that has no total. $deepest_level++; } } return $deepest_level; }
/** * Remove all grade related course data * Grade history is kept * * @param int $courseid The course ID * @param bool $showfeedback If true success notifications will be displayed */ function remove_course_grades($courseid, $showfeedback) { global $DB, $OUTPUT; $fs = get_file_storage(); $strdeleted = get_string('deleted'); $course_category = grade_category::fetch_course_category($courseid); $course_category->delete('coursedelete'); $fs->delete_area_files(get_context_instance(CONTEXT_COURSE, $courseid)->id, 'grade', 'feedback'); if ($showfeedback) { echo $OUTPUT->notification($strdeleted . ' - ' . get_string('grades', 'grades') . ', ' . get_string('items', 'grades') . ', ' . get_string('categories', 'grades'), 'notifysuccess'); } if ($outcomes = grade_outcome::fetch_all(array('courseid' => $courseid))) { foreach ($outcomes as $outcome) { $outcome->delete('coursedelete'); } } $DB->delete_records('grade_outcomes_courses', array('courseid' => $courseid)); if ($showfeedback) { echo $OUTPUT->notification($strdeleted . ' - ' . get_string('outcomes', 'grades'), 'notifysuccess'); } if ($scales = grade_scale::fetch_all(array('courseid' => $courseid))) { foreach ($scales as $scale) { $scale->delete('coursedelete'); } } if ($showfeedback) { echo $OUTPUT->notification($strdeleted . ' - ' . get_string('scales'), 'notifysuccess'); } $DB->delete_records('grade_settings', array('courseid' => $courseid)); if ($showfeedback) { echo $OUTPUT->notification($strdeleted . ' - ' . get_string('settings', 'grades'), 'notifysuccess'); } }
/** * Sets the grade_item's hidden variable and updates the grade_item. * * Overrides grade_item::set_hidden() to add cascading of the hidden value to grade items in this grade category * * @param int $hidden 0 mean always visible, 1 means always hidden and a number > 1 is a timestamp to hide until * @param bool $cascade apply to child objects too */ public function set_hidden($hidden, $cascade = false) { $this->load_grade_item(); //this hides the associated grade item (the course total) $this->grade_item->set_hidden($hidden, $cascade); //this hides the category itself and everything it contains parent::set_hidden($hidden, $cascade); if ($cascade) { if ($children = grade_item::fetch_all(array('categoryid' => $this->id))) { foreach ($children as $child) { if ($child->can_control_visibility()) { $child->set_hidden($hidden, $cascade); } } } if ($children = grade_category::fetch_all(array('parent' => $this->id))) { foreach ($children as $child) { $child->set_hidden($hidden, $cascade); } } } //if marking category visible make sure parent category is visible MDL-21367 if (!$hidden) { $category_array = grade_category::fetch_all(array('id' => $this->parent)); if ($category_array && array_key_exists($this->parent, $category_array)) { $category = $category_array[$this->parent]; //call set_hidden on the category regardless of whether it is hidden as its parent might be hidden //if($category->is_hidden()) { $category->set_hidden($hidden, false); //} } } }
/** * Remove all grade related course data - history is kept * @param int $courseid * @param bool @showfeedback print feedback */ function remove_course_grades($courseid, $showfeedback) { $strdeleted = get_string('deleted'); $course_category = grade_category::fetch_course_category($courseid); $course_category->delete('coursedelete'); if ($showfeedback) { notify($strdeleted . ' - ' . get_string('grades', 'grades') . ', ' . get_string('items', 'grades') . ', ' . get_string('categories', 'grades')); } if ($outcomes = grade_outcome::fetch_all(array('courseid' => $courseid))) { foreach ($outcomes as $outcome) { $outcome->delete('coursedelete'); } } delete_records('grade_outcomes_courses', 'courseid', $courseid); if ($showfeedback) { notify($strdeleted . ' - ' . get_string('outcomes', 'grades')); } if ($scales = grade_scale::fetch_all(array('courseid' => $courseid))) { foreach ($scales as $scale) { $scale->delete('coursedelete'); } } if ($showfeedback) { notify($strdeleted . ' - ' . get_string('scales')); } }
$grade_item->update(); grade_regrade_final_grades($courseid); // Grade category checkbox inputs } elseif (preg_match('/aggregate(onlygraded|subcats|outcomes)_original_([0-9]*)/', $key, $matches) && confirm_sesskey()) { $setting = optional_param('aggregate' . $matches[1] . '_' . $matches[2], null, PARAM_BOOL); $original_value = required_param($matches[0], PARAM_BOOL); $a->id = $matches[2]; $newvalue = null; if ($original_value == 1 && is_null($setting)) { $newvalue = 0; } elseif ($original_value == 0 && $setting == 1) { $newvalue = 1; } else { continue; } $grade_category = grade_category::fetch(array('id' => $a->id, 'courseid' => $courseid)); $grade_category->{'aggregate' . $matches[1]} = $newvalue; $grade_category->update(); grade_regrade_final_grades($courseid); } } } // Print Table of categories and items print_box_start('gradetreebox generalbox'); echo '<form id="gradetreeform" method="post" action="' . $returnurl . '">'; echo '<div>'; echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />'; // Build up an array of categories for move drop-down (by reference) $categories = array(); $level = 0; $row_count = 0;
} } else { if ($category->aggregation == GRADE_AGGREGATE_SUM && !$grade_item->is_calculated()) { // Input fields for grademin and grademax are disabled for the "Natural" category, // this means they will be ignored if user does not change aggregation method. // But if user does change aggregation method the default values should be used. // This does not apply to calculated category totals. $category->grademax = 100; $category->grade_item_grademax = 100; $category->grademin = 0; $category->grade_item_grademin = 0; } } } else { $heading = get_string('newcategory', 'grades'); $grade_category = new grade_category(array('courseid' => $courseid), false); $grade_category->apply_default_settings(); $grade_category->apply_forced_settings(); $category = $grade_category->get_record_data(); $grade_item = new grade_item(array('courseid' => $courseid, 'itemtype' => 'manual'), false); foreach ($grade_item->get_record_data() as $key => $value) { $category->{"grade_item_{$key}"} = $value; } } $mform = new edit_category_form(null, array('current' => $category, 'gpr' => $gpr)); if ($mform->is_cancelled()) { redirect($returnurl); } else { if ($data = $mform->get_data(false)) { // If no fullname is entered for a course category, put ? in the DB if (!isset($data->fullname) || $data->fullname == '') {
protected function sub_test_grade_category_set_locked() { $category = new grade_category($this->grade_categories[0]); $this->assertTrue(method_exists($category, 'set_locked')); //will return false as cannot lock a grade that needs updating $this->assertFalse($category->set_locked(1)); grade_regrade_final_grades($this->courseid); //get the category from the db again $category = new grade_category($this->grade_categories[0]); $this->assertTrue($category->set_locked(1)); }
/** * Save the new settings passed in $data * * @todo Add vartype handling to ensure $data is array * @param array $data Associative array of value=>xx, forced=>xx, adv=>xx * @return string empty or error message */ public function write_setting($data) { global $CFG; $value = $data['value']; $forced = empty($data['forced']) ? 0 : 1; $adv = empty($data['adv']) ? 0 : 2; $flag = $forced | $adv; //bitwise or if (!in_array($value, array_keys($this->choices))) { return 'Error setting '; } $oldvalue = $this->config_read($this->name); $oldflag = (int) $this->config_read($this->name . '_flag'); $oldforced = 1 & $oldflag; // first bit $result1 = $this->config_write($this->name, $value); $result2 = $this->config_write($this->name . '_flag', $flag); // force regrade if needed if ($oldforced != $forced or $forced and $value != $oldvalue) { require_once $CFG->libdir . '/gradelib.php'; grade_category::updated_forced_settings(); } if ($result1 and $result2) { return ''; } else { return get_string('errorsetting', 'admin'); } }
/** * Fetch all grade categories from the specified course. * * @param int $courseid the course id * @return array */ function certificate_get_grade_categories($courseid) { $grade_category_options = array(); if ($grade_categories = grade_category::fetch_all(array('courseid' => $courseid))) { foreach ($grade_categories as $grade_category) { if (!$grade_category->is_course_category()) { $grade_category_options[-$grade_category->id] = get_string('category') . ' : ' . $grade_category->get_name(); } } } return $grade_category_options; }
function definition() { global $COURSE, $CFG; $mform =& $this->_form; /// visible elements $mform->addElement('header', 'general', get_string('gradeoutcomeitem', 'grades')); $mform->addElement('text', 'itemname', get_string('itemname', 'grades')); $mform->addRule('itemname', get_string('required'), 'required', null, 'client'); $mform->addElement('text', 'iteminfo', get_string('iteminfo', 'grades')); $mform->setHelpButton('iteminfo', array('iteminfo', get_string('iteminfo', 'grades'), 'grade'), true); $mform->addElement('text', 'idnumber', get_string('idnumbermod')); $mform->setHelpButton('idnumber', array('idnumber', get_string('idnumber', 'grades'), 'grade'), true); // allow setting of outcomes on module items too $options = array(); if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) { foreach ($outcomes as $outcome) { $options[$outcome->id] = $outcome->get_name(); } } $mform->addElement('selectwithlink', 'outcomeid', get_string('outcome', 'grades'), $options, null, array('link' => $CFG->wwwroot . '/grade/edit/outcome/course.php?id=' . $COURSE->id, 'label' => get_string('outcomeassigntocourse', 'grades'))); $mform->setHelpButton('outcomeid', array('outcomeid', get_string('outcomeid', 'grades'), 'grade'), true); $mform->addRule('outcomeid', get_string('required'), 'required'); $options = array(0 => get_string('none')); if ($coursemods = get_course_mods($COURSE->id)) { foreach ($coursemods as $coursemod) { if ($mod = get_coursemodule_from_id($coursemod->modname, $coursemod->id)) { $options[$coursemod->id] = format_string($mod->name); } } } $mform->addElement('select', 'cmid', get_string('linkedactivity', 'grades'), $options); $mform->setHelpButton('cmid', array('linkedactivity', get_string('linkedactivity', 'grades'), 'grade'), true); $mform->setDefault('cmid', 0); /*$mform->addElement('text', 'gradepass', get_string('gradepass', 'grades')); $mform->setHelpButton('gradepass', array(false, get_string('gradepass', 'grades'), false, true, false, get_string('gradepasshelp', 'grades')));*/ /// hiding /// advcheckbox is not compatible with disabledIf !! $mform->addElement('checkbox', 'hidden', get_string('hidden', 'grades')); $mform->setHelpButton('hidden', array('hidden', get_string('hidden', 'grades'), 'grade')); $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional' => true)); $mform->setHelpButton('hiddenuntil', array('hiddenuntil', get_string('hiddenuntil', 'grades'), 'grade')); $mform->disabledIf('hidden', 'hiddenuntil[off]', 'notchecked'); //locking $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades')); $mform->setHelpButton('locked', array('locked', get_string('locked', 'grades'), 'grade')); $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional' => true)); $mform->setHelpButton('locktime', array('lockedafter', get_string('locktime', 'grades'), 'grade')); /// parent category related settings $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades')); $options = array(); $default = ''; $coefstring = ''; $categories = grade_category::fetch_all(array('courseid' => $COURSE->id)); foreach ($categories as $cat) { $cat->apply_forced_settings(); $options[$cat->id] = $cat->get_name(); if ($cat->is_course_category()) { $default = $cat->id; } if ($cat->is_aggregationcoef_used()) { if ($cat->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) { $coefstring = ($coefstring == '' or $coefstring == 'aggregationcoefweight') ? 'aggregationcoefweight' : 'aggregationcoef'; } else { if ($cat->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) { $coefstring = ($coefstring == '' or $coefstring == 'aggregationcoefextrasum') ? 'aggregationcoefextrasum' : 'aggregationcoef'; } else { if ($cat->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN) { $coefstring = ($coefstring == '' or $coefstring == 'aggregationcoefextra') ? 'aggregationcoefextra' : 'aggregationcoef'; } else { if ($cat->aggregation == GRADE_AGGREGATE_SUM) { $coefstring = ($coefstring == '' or $coefstring == 'aggregationcoefextrasum') ? 'aggregationcoefextrasum' : 'aggregationcoef'; } else { $coefstring = 'aggregationcoef'; } } } } } else { $mform->disabledIf('aggregationcoef', 'parentcategory', 'eq', $cat->id); } } if (count($categories) > 1) { $mform->addElement('select', 'parentcategory', get_string('gradecategory', 'grades'), $options); $mform->disabledIf('parentcategory', 'cmid', 'noteq', 0); } if ($coefstring !== '') { if ($coefstring == 'aggregationcoefextrasum') { // advcheckbox is not compatible with disabledIf! $mform->addElement('checkbox', 'aggregationcoef', get_string($coefstring, 'grades')); } else { $mform->addElement('text', 'aggregationcoef', get_string($coefstring, 'grades')); } $mform->setHelpButton('aggregationcoef', array($coefstring, get_string($coefstring, 'grades'), 'grade'), true); } /// hidden params $mform->addElement('hidden', 'id', 0); $mform->setType('id', PARAM_INT); $mform->addElement('hidden', 'courseid', $COURSE->id); $mform->setType('courseid', PARAM_INT); /// add return tracking info $gpr = $this->_customdata['gpr']; $gpr->add_mform_elements($mform); /// mark advanced according to site settings if (isset($CFG->grade_item_advanced)) { $advanced = explode(',', $CFG->grade_item_advanced); foreach ($advanced as $el) { if ($mform->elementExists($el)) { $mform->setAdvanced($el); } } } //------------------------------------------------------------------------------- // buttons $this->add_action_buttons(); }
function definition() { global $COURSE, $CFG, $DB; $mform =& $this->_form; $item = $this->_customdata['current']; /// visible elements $mform->addElement('header', 'general', get_string('gradeitem', 'grades')); $mform->addElement('text', 'itemname', get_string('itemname', 'grades')); $mform->addElement('text', 'iteminfo', get_string('iteminfo', 'grades')); $mform->setHelpButton('iteminfo', array('iteminfo', get_string('iteminfo', 'grades'), 'grade'), true); $mform->addElement('text', 'idnumber', get_string('idnumbermod')); $mform->setHelpButton('idnumber', array('idnumber', get_string('idnumber', 'grades'), 'grade'), true); $options = array(GRADE_TYPE_NONE => get_string('typenone', 'grades'), GRADE_TYPE_VALUE => get_string('typevalue', 'grades'), GRADE_TYPE_SCALE => get_string('typescale', 'grades'), GRADE_TYPE_TEXT => get_string('typetext', 'grades')); $mform->addElement('select', 'gradetype', get_string('gradetype', 'grades'), $options); $mform->setHelpButton('gradetype', array('gradetype', get_string('gradetype', 'grades'), 'grade'), true); $mform->setDefault('gradetype', GRADE_TYPE_VALUE); //$mform->addElement('text', 'calculation', get_string('calculation', 'grades')); //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_TEXT); //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_NONE); $options = array(0 => get_string('usenoscale', 'grades')); if ($scales = grade_scale::fetch_all_local($COURSE->id)) { foreach ($scales as $scale) { $options[$scale->id] = $scale->get_name(); } } if ($scales = grade_scale::fetch_all_global()) { foreach ($scales as $scale) { $options[$scale->id] = $scale->get_name(); } } // ugly BC hack - it was possbile to use custom scale from other courses :-( if (!empty($item->scaleid) and !isset($options[$item->scaleid])) { if ($scale = grade_scale::fetch(array('id' => $item->scaleid))) { $options[$scale->id] = $scale->get_name() . get_string('incorrectcustomscale', 'grades'); } } $mform->addElement('select', 'scaleid', get_string('scale'), $options); $mform->setHelpButton('scaleid', array('scaleid', get_string('scaleid', 'grades'), 'grade'), true); $mform->disabledIf('scaleid', 'gradetype', 'noteq', GRADE_TYPE_SCALE); $mform->addElement('text', 'grademax', get_string('grademax', 'grades')); $mform->setHelpButton('grademax', array('grademax', get_string('grademax', 'grades'), 'grade'), true); $mform->disabledIf('grademax', 'gradetype', 'noteq', GRADE_TYPE_VALUE); $mform->addElement('text', 'grademin', get_string('grademin', 'grades')); $mform->setHelpButton('grademin', array('grademin', get_string('grademin', 'grades'), 'grade'), true); $mform->disabledIf('grademin', 'gradetype', 'noteq', GRADE_TYPE_VALUE); $mform->addElement('text', 'gradepass', get_string('gradepass', 'grades')); $mform->setHelpButton('gradepass', array('gradepass', get_string('gradepass', 'grades'), 'grade'), true); $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_NONE); $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_TEXT); $mform->addElement('text', 'multfactor', get_string('multfactor', 'grades')); $mform->setHelpButton('multfactor', array('multfactor', get_string('multfactor', 'grades'), 'grade'), true); $mform->setAdvanced('multfactor'); $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_NONE); $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT); $mform->addElement('text', 'plusfactor', get_string('plusfactor', 'grades')); $mform->setHelpButton('plusfactor', array('plusfactor', get_string('plusfactor', 'grades'), 'grade'), true); $mform->setAdvanced('plusfactor'); $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_NONE); $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT); /// grade display prefs $default_gradedisplaytype = grade_get_setting($COURSE->id, 'displaytype', $CFG->grade_displaytype); $options = array(GRADE_DISPLAY_TYPE_DEFAULT => get_string('default', 'grades'), GRADE_DISPLAY_TYPE_REAL => get_string('real', 'grades'), GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'), GRADE_DISPLAY_TYPE_LETTER => get_string('letter', 'grades'), GRADE_DISPLAY_TYPE_REAL_PERCENTAGE => get_string('realpercentage', 'grades'), GRADE_DISPLAY_TYPE_REAL_LETTER => get_string('realletter', 'grades'), GRADE_DISPLAY_TYPE_LETTER_REAL => get_string('letterreal', 'grades'), GRADE_DISPLAY_TYPE_LETTER_PERCENTAGE => get_string('letterpercentage', 'grades'), GRADE_DISPLAY_TYPE_PERCENTAGE_LETTER => get_string('percentageletter', 'grades'), GRADE_DISPLAY_TYPE_PERCENTAGE_REAL => get_string('percentagereal', 'grades')); asort($options); foreach ($options as $key => $option) { if ($key == $default_gradedisplaytype) { $options[GRADE_DISPLAY_TYPE_DEFAULT] = get_string('defaultprev', 'grades', $option); break; } } $mform->addElement('select', 'display', get_string('gradedisplaytype', 'grades'), $options); $mform->setHelpButton('display', array('gradedisplaytype', get_string('gradedisplaytype', 'grades'), 'grade'), true); $default_gradedecimals = grade_get_setting($COURSE->id, 'decimalpoints', $CFG->grade_decimalpoints); $options = array(-1 => get_string('defaultprev', 'grades', $default_gradedecimals), 0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5); $mform->addElement('select', 'decimals', get_string('decimalpoints', 'grades'), $options); $mform->setHelpButton('decimals', array('decimalpoints', get_string('decimalpoints', 'grades'), 'grade'), true); $mform->setDefault('decimals', -1); $mform->disabledIf('decimals', 'display', 'eq', GRADE_DISPLAY_TYPE_LETTER); if ($default_gradedisplaytype == GRADE_DISPLAY_TYPE_LETTER) { $mform->disabledIf('decimals', 'display', "eq", GRADE_DISPLAY_TYPE_DEFAULT); } /// hiding // advcheckbox is not compatible with disabledIf! $mform->addElement('checkbox', 'hidden', get_string('hidden', 'grades')); $mform->setHelpButton('hidden', array('hidden', get_string('hidden', 'grades'), 'grade')); $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional' => true)); $mform->setHelpButton('hiddenuntil', array('hiddenuntil', get_string('hiddenuntil', 'grades'), 'grade')); $mform->disabledIf('hidden', 'hiddenuntil[off]', 'notchecked'); /// locking $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades')); $mform->setHelpButton('locked', array('locked', get_string('locked', 'grades'), 'grade')); $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional' => true)); $mform->setHelpButton('locktime', array('lockedafter', get_string('locktime', 'grades'), 'grade')); $mform->disabledIf('locktime', 'gradetype', 'eq', GRADE_TYPE_NONE); /// parent category related settings $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades')); $options = array(); $coefstring = ''; $categories = grade_category::fetch_all(array('courseid' => $COURSE->id)); foreach ($categories as $cat) { $cat->apply_forced_settings(); $options[$cat->id] = $cat->get_name(); } if (count($categories) > 1) { $mform->addElement('select', 'parentcategory', get_string('gradecategory', 'grades'), $options); } /// hidden params $mform->addElement('hidden', 'id', 0); $mform->setType('id', PARAM_INT); $mform->addElement('hidden', 'courseid', $COURSE->id); $mform->setType('courseid', PARAM_INT); $mform->addElement('hidden', 'itemtype', 'manual'); // all new items are manual only $mform->setType('itemtype', PARAM_ALPHA); /// add return tracking info $gpr = $this->_customdata['gpr']; $gpr->add_mform_elements($mform); /// mark advanced according to site settings if (isset($CFG->grade_item_advanced)) { $advanced = explode(',', $CFG->grade_item_advanced); foreach ($advanced as $el) { if ($mform->elementExists($el)) { $mform->setAdvanced($el); } } } //------------------------------------------------------------------------------- // buttons $this->add_action_buttons(); //------------------------------------------------------------------------------- $this->set_data($item); }
require_login($course); $context = context_course::instance($course->id); require_capability('moodle/grade:manage', $context); // todo $PAGE->requires->js_module() should be used here instead $PAGE->requires->js('/grade/edit/tree/functions.js'); /// return tracking object $gpr = new grade_plugin_return(array('type' => 'edit', 'plugin' => 'tree', 'courseid' => $courseid)); $returnurl = $gpr->get_return_url(null); // Change category aggregation if requested if (!is_null($category) && !is_null($aggregationtype) && confirm_sesskey()) { if (!($grade_category = grade_category::fetch(array('id' => $category, 'courseid' => $courseid)))) { print_error('invalidcategoryid'); } $data = new stdClass(); $data->aggregation = $aggregationtype; grade_category::set_properties($grade_category, $data); $grade_category->update(); grade_regrade_final_grades($courseid); } //first make sure we have proper final grades - we need it for locking changes $normalisationmessage = null; $originalweights = grade_helper::fetch_all_natural_weights_for_course($courseid); grade_regrade_final_grades($courseid); $alteredweights = grade_helper::fetch_all_natural_weights_for_course($courseid); if (array_diff($originalweights, $alteredweights)) { $normalisationmessage = get_string('weightsadjusted', 'grades'); } // get the grading tree object // note: total must be first for moving to work correctly, if you want it last moving code must be rewritten! $gtree = new grade_tree($courseid, false, false); if (empty($eid)) {
/** * Sets this item's categoryid. A generic method shared by objects that have a parent id of some kind. * @param int $parentid * @return boolean success; */ function set_parent($parentid) { if ($this->is_course_item() or $this->is_category_item()) { error('Can not set parent for category or course item!'); } if ($this->categoryid == $parentid) { return true; } // find parent and check course id if (!($parent_category = grade_category::fetch(array('id' => $parentid, 'courseid' => $this->courseid)))) { return false; } $this->force_regrading(); // set new parent $this->categoryid = $parent_category->id; $this->parent_category =& $parent_category; return $this->update(); }