function xmldb_glossary_upgrade($oldversion = 0) { global $CFG, $THEME, $db; $result = true; /// And upgrade begins here. For each one, you'll need one /// block of code similar to the next one. Please, delete /// this comment lines once this file start handling proper /// upgrade code. /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php /// $result = result of "/lib/ddllib.php" function calls /// } if ($result && $oldversion < 2006111400) { /// MDL-10475, set override for legacy:student before dropping studentcanpost /// if the glossary disables student postings if ($glossaries = get_records('glossary', 'studentcanpost', '0')) { foreach ($glossaries as $glossary) { if ($cm = get_coursemodule_from_instance('glossary', $glossary->id)) { // add student override in this instance $context = get_context_instance(CONTEXT_MODULE, $cm->id); // find all roles with legacy:student if ($studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) { foreach ($studentroles as $studentrole) { assign_capability('mod/glossary:write', CAP_PREVENT, $studentrole->id, $context->id); } } } } } /// Define field studentcanpost to be dropped from glossary $table = new XMLDBTable('glossary'); $field = new XMLDBField('studentcanpost'); /// Launch drop field studentcanpost $result = $result && drop_field($table, $field); } if ($result && $oldversion < 2007072200) { require_once $CFG->dirroot . '/mod/glossary/lib.php'; // too much debug output $db->debug = false; glossary_update_grades(); $db->debug = true; } //===== 1.9.0 upgrade line ======// return $result; }
/** * Update all grades in gradebook. * * @global object */ function glossary_upgrade_grades() { global $DB; $sql = "SELECT COUNT('x') FROM {glossary} g, {course_modules} cm, {modules} m WHERE m.name='glossary' AND m.id=cm.module AND cm.instance=g.id"; $count = $DB->count_records_sql($sql); $sql = "SELECT g.*, cm.idnumber AS cmidnumber, g.course AS courseid FROM {glossary} g, {course_modules} cm, {modules} m WHERE m.name='glossary' AND m.id=cm.module AND cm.instance=g.id"; $rs = $DB->get_recordset_sql($sql); if ($rs->valid()) { $pbar = new progress_bar('glossaryupgradegrades', 500, true); $i=0; foreach ($rs as $glossary) { $i++; upgrade_set_timeout(60*5); // set up timeout, may also abort execution glossary_update_grades($glossary, 0, false); $pbar->update($i, $count, "Updating Glossary grades ($i/$count)."); } } $rs->close(); }
if ($rating == -999) { $DB->delete_records('glossary_ratings', array('userid' => $oldrating->userid, 'entryid' => $oldrating->entryid)); glossary_update_grades($glossary, $entry->userid); } else { if ($rating != $oldrating->rating) { $oldrating->rating = $rating; $oldrating->time = time(); if (!$DB->update_record("glossary_ratings", $oldrating)) { print_error('cannotinsertrate', 'error', '', (object) array('id' => $entry->id, 'rating' => $rating)); } glossary_update_grades($glossary, $entry->userid); } } } else { if ($rating >= 0) { $newrating = new object(); $newrating->userid = $USER->id; $newrating->time = time(); $newrating->entryid = $entry->id; $newrating->rating = $rating; if (!$DB->insert_record("glossary_ratings", $newrating)) { print_error('cannotinsertrate', 'error', '', (object) array('id' => $entry->id, 'rating' => $rating)); } glossary_update_grades($glossary, $entry->userid); } } } redirect($returnurl, get_string("ratingssaved", "glossary")); } else { print_error('invalidaccess'); }
public function test_course_check_module_updates_since() { global $CFG, $DB, $USER; require_once $CFG->dirroot . '/mod/glossary/lib.php'; require_once $CFG->dirroot . '/rating/lib.php'; require_once $CFG->dirroot . '/comment/lib.php'; $this->resetAfterTest(true); $CFG->enablecompletion = true; $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id, 'completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1, 'allowcomments' => 1, 'assessed' => RATING_AGGREGATE_AVERAGE, 'scale' => 100)); $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary'); $context = context_module::instance($glossary->cmid); $modinfo = get_fast_modinfo($course); $cm = $modinfo->get_cm($glossary->cmid); $user = $this->getDataGenerator()->create_user(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id); $from = time(); $teacher = $this->getDataGenerator()->create_user(); $teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id); assign_capability('mod/glossary:viewanyrating', CAP_ALLOW, $studentrole->id, $context->id, true); // Check nothing changed right now. $updates = course_check_module_updates_since($cm, $from); $this->assertFalse($updates->configuration->updated); $this->assertFalse($updates->completion->updated); $this->assertFalse($updates->gradeitems->updated); $this->assertFalse($updates->comments->updated); $this->assertFalse($updates->ratings->updated); $this->assertFalse($updates->introfiles->updated); $this->assertFalse($updates->outcomes->updated); $this->waitForSecond(); // Do some changes. $this->setUser($user); $entry = $glossarygenerator->create_content($glossary); $this->setUser($teacher); // Name. set_coursemodule_name($glossary->cmid, 'New name'); // Add some ratings. $rm = new rating_manager(); $result = $rm->add_rating($cm, $context, 'mod_glossary', 'entry', $entry->id, 100, 50, $user->id, RATING_AGGREGATE_AVERAGE); // Change grades. $glossary->cmidnumber = $glossary->cmid; glossary_update_grades($glossary, $user->id); $this->setUser($user); // Completion status. glossary_view($glossary, $course, $cm, $context, 'letter'); // Add one comment. $args = new stdClass(); $args->context = $context; $args->course = $course; $args->cm = $cm; $args->area = 'glossary_entry'; $args->itemid = $entry->id; $args->client_id = 1; $args->component = 'mod_glossary'; $manager = new comment($args); $manager->add('blah blah blah'); // Check upgrade status. $updates = course_check_module_updates_since($cm, $from); $this->assertTrue($updates->configuration->updated); $this->assertTrue($updates->completion->updated); $this->assertTrue($updates->gradeitems->updated); $this->assertTrue($updates->comments->updated); $this->assertTrue($updates->ratings->updated); $this->assertFalse($updates->introfiles->updated); $this->assertFalse($updates->outcomes->updated); }
/** * Update grades by firing grade_updated event * * @param object $glossary null means all glossaries (with extra cmidnumber property) * @param int $userid specific user only, 0 mean all */ function glossary_update_grades($glossary = null, $userid = 0, $nullifnone = true) { global $CFG; require_once $CFG->libdir . '/gradelib.php'; if ($glossary != null) { if ($grades = glossary_get_user_grades($glossary, $userid)) { glossary_grade_item_update($glossary, $grades); } else { if ($userid and $nullifnone) { $grade = new object(); $grade->userid = $userid; $grade->rawgrade = NULL; glossary_grade_item_update($glossary, $grade); } else { glossary_grade_item_update($glossary); } } } else { $sql = "SELECT g.*, cm.idnumber as cmidnumber\n FROM {$CFG->prefix}glossary g, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m\n WHERE m.name='glossary' AND m.id=cm.module AND cm.instance=g.id"; if ($rs = get_recordset_sql($sql)) { while ($glossary = rs_fetch_next_record($rs)) { if ($glossary->assessed) { glossary_update_grades($glossary, 0, false); } else { glossary_grade_item_update($glossary); } } rs_close($rs); } } }