public function test_grade_get_grade_items_for_activity() { $this->resetAfterTest(true); $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id)); $cm = get_coursemodule_from_id('forum', $forum->cmid, $forum->course); unset($cm->modname); $grade = grade_get_grade_items_for_activity($cm); }
/** * Get all the modules * * @return array */ function certificate_get_mods() { global $COURSE, $CFG, $DB; $strtopic = get_string("topic"); $strweek = get_string("week"); $strsection = get_string("section"); // Collect modules data get_all_mods($COURSE->id, $mods, $modnames, $modnamesplural, $modnamesused); $modules = array(); $sections = get_all_sections($COURSE->id); // Sort everything the same as the course for ($i = 0; $i <= $COURSE->numsections; $i++) { // should always be true if (isset($sections[$i])) { $section = $sections[$i]; if ($section->sequence) { switch ($COURSE->format) { case "topics": $sectionlabel = $strtopic; break; case "weeks": $sectionlabel = $strweek; break; default: $sectionlabel = $strsection; } $sectionmods = explode(",", $section->sequence); foreach ($sectionmods as $sectionmod) { if (empty($mods[$sectionmod])) { continue; } $mod = $mods[$sectionmod]; $mod->courseid = $COURSE->id; $instance = $DB->get_record($mod->modname, array('id' => $mod->instance)); if ($grade_items = grade_get_grade_items_for_activity($mod)) { $mod_item = grade_get_grades($COURSE->id, 'mod', $mod->modname, $mod->instance); $item = reset($mod_item->items); if (isset($item->grademax)) { $modules[$mod->id] = $sectionlabel . ' ' . $section->section . ' : ' . $instance->name; } } } } } } return $modules; }
/** * Returns whether or not user received grades in main grade item for given activity. * * @param object $cm * @param int $userid * @return bool True if graded false if user not graded yet */ function grade_is_user_graded_in_activity($cm, $userid) { $grade_items = grade_get_grade_items_for_activity($cm, true); if (empty($grade_items)) { return false; } $grade_item = reset($grade_items); if ($grade_item->gradetype == GRADE_TYPE_NONE) { return false; } if ($grade_item->needsupdate) { // activity items should never fail to regrade grade_regrade_final_grades($grade_item->courseid); } if (!($grade = $grade_item->get_final($userid))) { return false; } if (is_null($grade->finalgrade)) { return false; } return true; }
/** * The availability data format was changed in Moodle 2.7. This test * ensures that a Moodle 2.6 backup with this data can still be correctly * restored. */ public function test_restore_legacy_availability() { global $DB, $USER, $CFG; require_once $CFG->dirroot . '/grade/querylib.php'; require_once $CFG->libdir . '/completionlib.php'; $this->resetAfterTest(true); $this->setAdminUser(); $CFG->enableavailability = true; $CFG->enablecompletion = true; // Extract backup file. $backupid = 'abc'; $backuppath = $CFG->tempdir . '/backup/' . $backupid; check_dir_exists($backuppath); get_file_packer('application/vnd.moodle.backup')->extract_to_pathname(__DIR__ . '/fixtures/availability_26_format.mbz', $backuppath); // Do restore to new course with default settings. $generator = $this->getDataGenerator(); $categoryid = $DB->get_field_sql("SELECT MIN(id) FROM {course_categories}"); $newcourseid = restore_dbops::create_new_course('Test fullname', 'Test shortname', $categoryid); $rc = new restore_controller($backupid, $newcourseid, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id, backup::TARGET_NEW_COURSE); $thrown = null; try { $this->assertTrue($rc->execute_precheck()); $rc->execute_plan(); $rc->destroy(); } catch (Exception $e) { $thrown = $e; // Because of the PHPUnit exception behaviour in this situation, we // will not see this message unless it is explicitly echoed (just // using it in a fail() call or similar will not work). echo "\n\nEXCEPTION: " . $thrown->getMessage() . '[' . $thrown->getFile() . ':' . $thrown->getLine() . "]\n\n"; } // Must set restore_controller variable to null so that php // garbage-collects it; otherwise the file will be left open and // attempts to delete it will cause a permission error on Windows // systems, breaking unit tests. $rc = null; $this->assertNull($thrown); // Get information about the resulting course and check that it is set // up correctly. $modinfo = get_fast_modinfo($newcourseid); $pages = array_values($modinfo->get_instances_of('page')); $forums = array_values($modinfo->get_instances_of('forum')); $quizzes = array_values($modinfo->get_instances_of('quiz')); $grouping = $DB->get_record('groupings', array('courseid' => $newcourseid)); // FROM date. $this->assertEquals('{"op":"&","showc":[true],"c":[{"type":"date","d":">=","t":1893456000}]}', $pages[1]->availability); // UNTIL date. $this->assertEquals('{"op":"&","showc":[false],"c":[{"type":"date","d":"<","t":1393977600}]}', $pages[2]->availability); // FROM and UNTIL. $this->assertEquals('{"op":"&","showc":[true,false],"c":[' . '{"type":"date","d":">=","t":1449705600},' . '{"type":"date","d":"<","t":1893456000}' . ']}', $pages[3]->availability); // Grade >= 75%. $grades = array_values(grade_get_grade_items_for_activity($quizzes[0], true)); $gradeid = $grades[0]->id; $coursegrade = grade_item::fetch_course_item($newcourseid); $this->assertEquals('{"op":"&","showc":[true],"c":[{"type":"grade","id":' . $gradeid . ',"min":75}]}', $pages[4]->availability); // Grade < 25%. $this->assertEquals('{"op":"&","showc":[true],"c":[{"type":"grade","id":' . $gradeid . ',"max":25}]}', $pages[5]->availability); // Grade 90-100%. $this->assertEquals('{"op":"&","showc":[true],"c":[{"type":"grade","id":' . $gradeid . ',"min":90,"max":100}]}', $pages[6]->availability); // Email contains frog. $this->assertEquals('{"op":"&","showc":[true],"c":[{"type":"profile","op":"contains","sf":"email","v":"frog"}]}', $pages[7]->availability); // Page marked complete.. $this->assertEquals('{"op":"&","showc":[true],"c":[{"type":"completion","cm":' . $pages[0]->id . ',"e":' . COMPLETION_COMPLETE . '}]}', $pages[8]->availability); // Quiz complete but failed. $this->assertEquals('{"op":"&","showc":[true],"c":[{"type":"completion","cm":' . $quizzes[0]->id . ',"e":' . COMPLETION_COMPLETE_FAIL . '}]}', $pages[9]->availability); // Quiz complete and succeeded. $this->assertEquals('{"op":"&","showc":[true],"c":[{"type":"completion","cm":' . $quizzes[0]->id . ',"e":' . COMPLETION_COMPLETE_PASS . '}]}', $pages[10]->availability); // Quiz not complete. $this->assertEquals('{"op":"&","showc":[true],"c":[{"type":"completion","cm":' . $quizzes[0]->id . ',"e":' . COMPLETION_INCOMPLETE . '}]}', $pages[11]->availability); // Grouping. $this->assertEquals('{"op":"&","showc":[false],"c":[{"type":"grouping","id":' . $grouping->id . '}]}', $pages[12]->availability); // All the options. $this->assertEquals('{"op":"&",' . '"showc":[false,true,false,true,true,true,true,true,true],' . '"c":[' . '{"type":"grouping","id":' . $grouping->id . '},' . '{"type":"date","d":">=","t":1488585600},' . '{"type":"date","d":"<","t":1709510400},' . '{"type":"profile","op":"contains","sf":"email","v":"@"},' . '{"type":"profile","op":"contains","sf":"city","v":"Frogtown"},' . '{"type":"grade","id":' . $gradeid . ',"min":30,"max":35},' . '{"type":"grade","id":' . $coursegrade->id . ',"min":5,"max":10},' . '{"type":"completion","cm":' . $pages[0]->id . ',"e":' . COMPLETION_COMPLETE . '},' . '{"type":"completion","cm":' . $quizzes[0]->id . ',"e":' . COMPLETION_INCOMPLETE . '}' . ']}', $pages[13]->availability); // Group members only forum. $this->assertEquals('{"op":"&","showc":[false],"c":[{"type":"group"}]}', $forums[0]->availability); // Section with lots of conditions. $this->assertEquals('{"op":"&","showc":[false,false,false,false],"c":[' . '{"type":"date","d":">=","t":1417737600},' . '{"type":"profile","op":"contains","sf":"email","v":"@"},' . '{"type":"grade","id":' . $gradeid . ',"min":20},' . '{"type":"completion","cm":' . $pages[0]->id . ',"e":' . COMPLETION_COMPLETE . '}]}', $modinfo->get_section_info(3)->availability); // Section with grouping. $this->assertEquals('{"op":"&","showc":[false],"c":[{"type":"grouping","id":' . $grouping->id . '}]}', $modinfo->get_section_info(4)->availability); }
/** * Get all the modules * * @return array */ function iomadcertificate_get_mods() { global $COURSE, $DB; $strtopic = get_string("topic"); $strweek = get_string("week"); $strsection = get_string("section"); // Collect modules data $modinfo = get_fast_modinfo($COURSE); $mods = $modinfo->get_cms(); $modules = array(); $sections = $modinfo->get_section_info_all(); for ($i = 0; $i <= count($sections) - 1; $i++) { // should always be true if (isset($sections[$i])) { $section = $sections[$i]; if ($section->sequence) { switch ($COURSE->format) { case "topics": $sectionlabel = $strtopic; break; case "weeks": $sectionlabel = $strweek; break; default: $sectionlabel = $strsection; } $sectionmods = explode(",", $section->sequence); foreach ($sectionmods as $sectionmod) { if (empty($mods[$sectionmod])) { continue; } $mod = $mods[$sectionmod]; $instance = $DB->get_record($mod->modname, array('id' => $mod->instance)); if ($grade_items = grade_get_grade_items_for_activity($mod)) { $mod_item = grade_get_grades($COURSE->id, 'mod', $mod->modname, $mod->instance); $item = reset($mod_item->items); if (isset($item->grademax)) { $modules[$mod->id] = $sectionlabel . ' ' . $section->section . ' : ' . $instance->name; } } } } } } return $modules; }
function blended_get_grademax($activity_cm) { $grade_item = grade_get_grade_items_for_activity($activity_cm, true); foreach ($grade_item as $grade) { return $grade->grademax; } }
function certificate_get_mod_grades() { global $course, $CFG; $strgrade = get_string('grade', 'certificate'); /// Collect modules data get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused); $printgrade = array(); $sections = get_all_sections($course->id); // Sort everything the same as the course for ($i = 0; $i <= $course->numsections; $i++) { // should always be true if (isset($sections[$i])) { $section = $sections[$i]; if ($section->sequence) { switch ($course->format) { case 'topics': $sectionlabel = get_string('topic'); break; case 'weeks': $sectionlabel = get_string('week'); break; default: $sectionlabel = get_string('section'); } $sectionmods = explode(",", $section->sequence); foreach ($sectionmods as $sectionmod) { if (empty($mods[$sectionmod])) { continue; } $mod = $mods[$sectionmod]; $mod->courseid = $course->id; $instance = get_record($mod->modname, 'id', $mod->instance); if ($grade_items = grade_get_grade_items_for_activity($mod)) { $mod_item = grade_get_grades($course->id, 'mod', $mod->modname, $mod->instance); $item = reset($mod_item->items); if (isset($item->grademax)) { $printgrade[$mod->id] = $sectionlabel . ' ' . $section->section . ' : ' . $instance->name . ' ' . $strgrade; } } } } } } if (isset($printgrade)) { $gradeoptions['0'] = get_string('no'); $gradeoptions['1'] = get_string('coursegrade', 'certificate'); foreach ($printgrade as $key => $value) { $gradeoptions[$key] = $value; } } else { $gradeoptions['0'] = get_string('nogrades', 'certificate'); } return $gradeoptions; }
/** * Get all the modules * * @return array */ function certificate_get_mods() { global $COURSE, $CFG, $DB; $strtopic = get_string("topic"); $strweek = get_string("week"); $strsection = get_string("section"); // Collect modules data $modinfo = get_fast_modinfo($COURSE); $mods = $modinfo->get_cms(); $modules = array(); // Check what version we are running - really we should have separate branch for 2.4, but // having a branch called master and one called MOODLE_24_STABLE may be confusing. This // module will also be replaced in the future so hack will do. Here we get the course // sections and sort the modules as they appear in the course. if ($CFG->version >= '2012112900') { $sections = $modinfo->get_section_info_all(); } else { $sections = get_all_sections($COURSE->id); } for ($i = 0; $i <= count($sections) - 1; $i++) { // should always be true if (isset($sections[$i])) { $section = $sections[$i]; if ($section->sequence) { switch ($COURSE->format) { case "topics": $sectionlabel = $strtopic; break; case "weeks": $sectionlabel = $strweek; break; default: $sectionlabel = $strsection; } $sectionmods = explode(",", $section->sequence); foreach ($sectionmods as $sectionmod) { if (empty($mods[$sectionmod])) { continue; } $mod = $mods[$sectionmod]; $mod->courseid = $COURSE->id; $instance = $DB->get_record($mod->modname, array('id' => $mod->instance)); if ($grade_items = grade_get_grade_items_for_activity($mod)) { $mod_item = grade_get_grades($COURSE->id, 'mod', $mod->modname, $mod->instance); $item = reset($mod_item->items); if (isset($item->grademax)) { $modules[$mod->id] = $sectionlabel . ' ' . $section->section . ' : ' . $instance->name; } } } } } } return $modules; }
/** * Helper function to return all the grades items for this course. * * @return array the array of gradeable items in the course */ public static function get_grade_items() { global $COURSE, $DB; // Array to store the grade items. $modules = array(); // Collect course modules data. $modinfo = get_fast_modinfo($COURSE); $mods = $modinfo->get_cms(); $sections = $modinfo->get_section_info_all(); // Create the section label depending on course format. switch ($COURSE->format) { case 'topics': $sectionlabel = get_string('topic'); break; case 'weeks': $sectionlabel = get_string('week'); break; default: $sectionlabel = get_string('section'); break; } // Loop through each course section. for ($i = 0; $i <= count($sections) - 1; $i++) { // Confirm the index exists, should always be true. if (isset($sections[$i])) { // Get the individual section. $section = $sections[$i]; // Get the mods for this section. $sectionmods = explode(",", $section->sequence); // Loop through the section mods. foreach ($sectionmods as $sectionmod) { // Should never happen unless DB is borked. if (empty($mods[$sectionmod])) { continue; } $mod = $mods[$sectionmod]; $instance = $DB->get_record($mod->modname, array('id' => $mod->instance)); // Get the grade items for this activity. if ($gradeitems = grade_get_grade_items_for_activity($mod)) { $moditem = grade_get_grades($COURSE->id, 'mod', $mod->modname, $mod->instance); $gradeitem = reset($moditem->items); if (isset($gradeitem->grademax)) { $modules[$mod->id] = $sectionlabel . ' ' . $section->section . ' : ' . $instance->name; } } } } } return $modules; }