/** * Loads some data to be used by the different tests * @param int $s1grade Student 1 grade * @param int $s2grade Student 2 grade * @return array Course and users instances */ private function load_data($s1grade, $s2grade) { global $DB; $course = $this->getDataGenerator()->create_course(array('groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1)); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $student1 = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id); $student2 = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id); $teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); $teacher = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id); $context = context_course::instance($course->id); assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $teacherrole->id, $context); accesslib_clear_all_caches_for_unit_testing(); $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); groups_add_member($group1->id, $student1->id); groups_add_member($group1->id, $teacher->id); groups_add_member($group2->id, $student2->id); $assignment = $this->getDataGenerator()->create_module('assign', array('name' => "Test assign", 'course' => $course->id)); $modcontext = get_coursemodule_from_instance('assign', $assignment->id, $course->id); $assignment->cmidnumber = $modcontext->id; $student1grade = array('userid' => $student1->id, 'rawgrade' => $s1grade); $student2grade = array('userid' => $student2->id, 'rawgrade' => $s2grade); $studentgrades = array($student1->id => $student1grade, $student2->id => $student2grade); assign_grade_item_update($assignment, $studentgrades); return array($course, $teacher, $student1, $student2, $assignment); }
/** * Set up for every test */ public function setUp() { global $DB; $this->resetAfterTest(true); $s1grade1 = 80; $s1grade2 = 40; $s2grade = 60; $this->course1 = $this->getDataGenerator()->create_course(); $this->course2 = $this->getDataGenerator()->create_course(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); $this->student1 = $this->getDataGenerator()->create_user(); $this->teacher = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course1->id, $teacherrole->id); $this->getDataGenerator()->enrol_user($this->student1->id, $this->course1->id, $studentrole->id); $this->getDataGenerator()->enrol_user($this->student1->id, $this->course2->id, $studentrole->id); $this->student2 = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($this->student2->id, $this->course1->id, $studentrole->id); $this->getDataGenerator()->enrol_user($this->student2->id, $this->course2->id, $studentrole->id); $assignment1 = $this->getDataGenerator()->create_module('assign', array('name' => "Test assign", 'course' => $this->course1->id)); $assignment2 = $this->getDataGenerator()->create_module('assign', array('name' => "Test assign", 'course' => $this->course2->id)); $modcontext1 = get_coursemodule_from_instance('assign', $assignment1->id, $this->course1->id); $modcontext2 = get_coursemodule_from_instance('assign', $assignment2->id, $this->course2->id); $assignment1->cmidnumber = $modcontext1->id; $assignment2->cmidnumber = $modcontext2->id; $this->student1grade1 = array('userid' => $this->student1->id, 'rawgrade' => $s1grade1); $this->student1grade2 = array('userid' => $this->student1->id, 'rawgrade' => $s1grade2); $this->student2grade = array('userid' => $this->student2->id, 'rawgrade' => $s2grade); $studentgrades = array($this->student1->id => $this->student1grade1, $this->student2->id => $this->student2grade); assign_grade_item_update($assignment1, $studentgrades); $studentgrades = array($this->student1->id => $this->student1grade2); assign_grade_item_update($assignment2, $studentgrades); grade_get_setting($this->course1->id, 'report_overview_showrank', 1); }
/** * Loads some data to be used by the different tests * @param int $s1grade Student 1 grade * @param int $s2grade Student 2 grade * @return array Course and users instances */ private function load_data($s1grade, $s2grade) { global $DB; $course = $this->getDataGenerator()->create_course(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $student1 = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id); $student2 = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id); $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); $teacher = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id); $assignment = $this->getDataGenerator()->create_module('assign', array('name' => "Test assign", 'course' => $course->id)); $modcontext = get_coursemodule_from_instance('assign', $assignment->id, $course->id); $assignment->cmidnumber = $modcontext->id; $student1grade = array('userid' => $student1->id, 'rawgrade' => $s1grade); $student2grade = array('userid' => $student2->id, 'rawgrade' => $s2grade); $studentgrades = array($student1->id => $student1grade, $student2->id => $student2grade); assign_grade_item_update($assignment, $studentgrades); return array($course, $teacher, $student1, $student2); }
/** * Update grades in the gradebook. * * @param mixed $submission stdClass|null * @param mixed $grade stdClass|null * @return bool */ protected function gradebook_item_update($submission = null, $grade = null) { // Do not push grade to gradebook if blind marking is active as // the gradebook would reveal the students. if ($this->is_blind_marking()) { return false; } // If marking workflow is enabled and grade is not released then don't send to gradebook yet. if ($this->get_instance()->markingworkflow && !empty($grade)) { $flags = $this->get_user_flags($grade->userid, false); if (empty($flags->workflowstate) || $flags->workflowstate != ASSIGN_MARKING_WORKFLOW_STATE_RELEASED) { return false; } } if ($submission != null) { if ($submission->userid == 0) { // This is a group submission update. $team = groups_get_members($submission->groupid, 'u.id'); foreach ($team as $member) { $membersubmission = clone $submission; $membersubmission->groupid = 0; $membersubmission->userid = $member->id; $this->gradebook_item_update($membersubmission, null); } return; } $gradebookgrade = $this->convert_submission_for_gradebook($submission); } else { $gradebookgrade = $this->convert_grade_for_gradebook($grade); } // Grading is disabled, return. if ($this->grading_disabled($gradebookgrade['userid'])) { return false; } $assign = clone $this->get_instance(); $assign->cmidnumber = $this->get_course_module()->idnumber; return assign_grade_item_update($assign, $gradebookgrade); }
/** * Update activity grades * * @param stdClass $assign database record * @param int $userid specific user only, 0 means all * @param bool $nullifnone - not used */ function assign_update_grades($assign, $userid = 0, $nullifnone = true) { global $CFG; require_once $CFG->libdir . '/gradelib.php'; if ($assign->grade == 0) { assign_grade_item_update($assign); } else { if ($grades = assign_get_user_grades($assign, $userid)) { foreach ($grades as $k => $v) { if ($v->rawgrade == -1) { $grades[$k]->rawgrade = null; } } assign_grade_item_update($assign, $grades); } else { assign_grade_item_update($assign); } } }
/** * Update grades in the gradebook. * * @param mixed $submission stdClass|null * @param mixed $grade stdClass|null * @return bool */ protected function gradebook_item_update($submission = null, $grade = null) { global $CFG; require_once $CFG->dirroot . '/mod/assign/lib.php'; // Do not push grade to gradebook if blind marking is active as // the gradebook would reveal the students. if ($this->is_blind_marking()) { return false; } // If marking workflow is enabled and grade is not released then remove any grade that may exist in the gradebook. if ($this->get_instance()->markingworkflow && !empty($grade) && $this->get_grading_status($grade->userid) != ASSIGN_MARKING_WORKFLOW_STATE_RELEASED) { // Remove the grade (if it exists) from the gradebook as it is not 'final'. $grade->grade = -1; $grade->feedbacktext = ''; } if ($submission != null) { if ($submission->userid == 0) { // This is a group submission update. $team = groups_get_members($submission->groupid, 'u.id'); foreach ($team as $member) { $membersubmission = clone $submission; $membersubmission->groupid = 0; $membersubmission->userid = $member->id; $this->gradebook_item_update($membersubmission, null); } return; } $gradebookgrade = $this->convert_submission_for_gradebook($submission); } else { $gradebookgrade = $this->convert_grade_for_gradebook($grade); } // Grading is disabled, return. if ($this->grading_disabled($gradebookgrade['userid'])) { return false; } $assign = clone $this->get_instance(); $assign->cmidnumber = $this->get_course_module()->idnumber; // Set assign gradebook feedback plugin status (enabled and visible). $assign->gradefeedbackenabled = $this->is_gradebook_feedback_enabled(); return assign_grade_item_update($assign, $gradebookgrade) == GRADE_UPDATE_OK; }
/** * update grades in the gradebook * * @param mixed $submission stdClass|null * @param mixed $grade stdClass|null * @return bool */ private function gradebook_item_update($submission=NULL, $grade=NULL) { // Do not push grade to gradebook if blind marking is active as the gradebook would reveal the students. if ($this->is_blind_marking()) { return false; } if ($submission != NULL) { if ($submission->userid == 0) { // This is a group submission update. $team = groups_get_members($submission->groupid, 'u.id'); foreach ($team as $member) { $submission->groupid = 0; $submission->userid = $member->id; $this->gradebook_item_update($submission, null); } return; } $gradebookgrade = $this->convert_submission_for_gradebook($submission); } else { $gradebookgrade = $this->convert_grade_for_gradebook($grade); } // Grading is disabled, return. if ($this->grading_disabled($gradebookgrade['userid'])) { return false; } $assign = clone $this->get_instance(); $assign->cmidnumber = $this->get_course_module()->id; return assign_grade_item_update($assign, $gradebookgrade); }
/** * Update grades in the gradebook. * * @param mixed $submission stdClass|null * @param mixed $grade stdClass|null * @return bool */ protected function gradebook_item_update($submission=null, $grade=null) { global $CFG; require_once($CFG->dirroot.'/mod/assign/lib.php'); // Do not push grade to gradebook if blind marking is active as // the gradebook would reveal the students. if ($this->is_blind_marking()) { return false; } if ($submission != null) { if ($submission->userid == 0) { // This is a group submission update. $team = groups_get_members($submission->groupid, 'u.id'); foreach ($team as $member) { $membersubmission = clone $submission; $membersubmission->groupid = 0; $membersubmission->userid = $member->id; $this->gradebook_item_update($membersubmission, null); } return; } $gradebookgrade = $this->convert_submission_for_gradebook($submission); } else { $gradebookgrade = $this->convert_grade_for_gradebook($grade); } // Grading is disabled, return. if ($this->grading_disabled($gradebookgrade['userid'])) { return false; } $assign = clone $this->get_instance(); $assign->cmidnumber = $this->get_course_module()->idnumber; return assign_grade_item_update($assign, $gradebookgrade); }
/** * Updates the grade of a user in the given assign module instance. * * @param stdClass $assignrow Assignment row from database * @param int $userid User id * @param float $grade Grade */ protected static function set_grade($assignrow, $userid, $grade) { $grades = array(); $grades[$userid] = (object) array('rawgrade' => $grade, 'userid' => $userid); $assignrow->cmidnumber = null; assign_grade_item_update($assignrow, $grades); }
/** * update grades in the gradebook * * @param mixed $submission stdClass|null * @param mixed $grade stdClass|null * @return bool */ private function gradebook_item_update($submission = NULL, $grade = NULL) { if ($submission != NULL) { $gradebookgrade = $this->convert_submission_for_gradebook($submission); } else { $gradebookgrade = $this->convert_grade_for_gradebook($grade); } // Grading is disabled, return. if ($this->grading_disabled($gradebookgrade['userid'])) { return false; } $assign = clone $this->get_instance(); $assign->cmidnumber = $this->get_course_module()->id; return assign_grade_item_update($assign, $gradebookgrade); }
/** * Load initial test information * * @param string $assignmentname Assignment name * @param int $student1rawgrade Student 1 grade * @param int $student2rawgrade Student 2 grade * @return array Array of vars with test information */ protected function load_test_data($assignmentname, $student1rawgrade, $student2rawgrade) { global $DB; // Adds a course, a teacher, 2 students, an assignment and grades for the students. $course = $this->getDataGenerator()->create_course(); $coursecontext = context_course::instance($course->id); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $student1 = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id); $student2 = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id); $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); $teacher = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id); $parent = $this->getDataGenerator()->create_user(); $this->setUser($parent); $student1context = context_user::instance($student1->id); // Creates a new role, gives it the capability and gives $USER that role. $parentroleid = $this->assignUserCapability('moodle/grade:viewall', $student1context->id); // Enrol the user in the course using the new role. $this->getDataGenerator()->enrol_user($parent->id, $course->id, $parentroleid); $assignment = $this->getDataGenerator()->create_module('assign', array('name' => $assignmentname, 'course' => $course->id)); $modcontext = get_coursemodule_from_instance('assign', $assignment->id, $course->id); $assignment->cmidnumber = $modcontext->id; $student1grade = array('userid' => $student1->id, 'rawgrade' => $student1rawgrade); $student2grade = array('userid' => $student2->id, 'rawgrade' => $student2rawgrade); $studentgrades = array($student1->id => $student1grade, $student2->id => $student2grade); assign_grade_item_update($assignment, $studentgrades); // Insert a custom grade scale to be used by an outcome. $gradescale = new grade_scale(); $gradescale->name = 'unittestscale3'; $gradescale->courseid = $course->id; $gradescale->userid = 0; $gradescale->scale = 'Distinction, Very Good, Good, Pass, Fail'; $gradescale->description = 'This scale is used to mark standard assignments.'; $gradescale->insert(); // Insert an outcome. $data = new stdClass(); $data->courseid = $course->id; $data->fullname = 'Team work'; $data->shortname = 'Team work'; $data->scaleid = $gradescale->id; $outcome = new grade_outcome($data, false); $outcome->insert(); $outcomegradeitem = new grade_item(); $outcomegradeitem->itemname = $outcome->shortname; $outcomegradeitem->itemtype = 'mod'; $outcomegradeitem->itemmodule = 'assign'; $outcomegradeitem->iteminstance = $assignment->id; $outcomegradeitem->outcomeid = $outcome->id; $outcomegradeitem->cmid = 0; $outcomegradeitem->courseid = $course->id; $outcomegradeitem->aggregationcoef = 0; $outcomegradeitem->itemnumber = 1; // The activity's original grade item will be 0. $outcomegradeitem->gradetype = GRADE_TYPE_SCALE; $outcomegradeitem->scaleid = $outcome->scaleid; // This next two values for testing that returns parameters are correcly formatted. $outcomegradeitem->set_locked(true); $outcomegradeitem->hidden = ''; $outcomegradeitem->insert(); $assignmentgradeitem = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'assign', 'iteminstance' => $assignment->id, 'itemnumber' => 0, 'courseid' => $course->id)); $outcomegradeitem->set_parent($assignmentgradeitem->categoryid); $outcomegradeitem->move_after_sortorder($assignmentgradeitem->sortorder); return array($course, $assignment, $student1, $student2, $teacher, $parent); }
/** * update grades in the gradebook * * @param mixed $submission stdClass|null * @param mixed $grade stdClass|null * @return bool */ private function gradebook_item_update($submission = NULL, $grade = NULL) { if ($submission != NULL) { $gradebookgrade = $this->convert_submission_for_gradebook($submission); } else { $gradebookgrade = $this->convert_grade_for_gradebook($grade); } $assign = clone $this->get_instance(); $assign->cmidnumber = $this->get_course_module()->id; return assign_grade_item_update($assign, $gradebookgrade); }
public function test_course_completion_progress() { global $CFG, $DB; $CFG->enablecompletion = true; $DB->update_record('course', (object) ['id' => $this->course->id, 'enablecompletion' => 1]); $this->course = $DB->get_record('course', ['id' => $this->course->id]); $actual = local::course_completion_progress($this->course); $this->assertNull($actual); $this->create_extra_users(); $this->setUser($this->extrasuspendedstudents[0]); $actual = local::course_completion_progress($this->course); $this->assertNull($actual); $this->setUser($this->students[0]); $actual = local::course_completion_progress($this->course); $this->assertNull($actual); // Create an assignment that is enabled for completion. $this->setUSer($this->teachers[0]); $assign = $this->create_instance(['course' => $this->course->id, 'name' => 'Assign!', 'completion' => COMPLETION_TRACKING_AUTOMATIC]); // Should now have something that can be tracked for progress. $this->setUser($this->students[0]); $actual = local::course_completion_progress($this->course); $this->assertInstanceOf('stdClass', $actual); $this->assertEquals(0, $actual->complete); $this->assertEquals(1, $actual->total); $this->assertEquals(0, $actual->progress); // Make sure completion updates on grading. $DB->set_field('course_modules', 'completiongradeitemnumber', 0, ['id' => $assign->get_course_module()->id]); $gradeitem = $assign->get_grade_item(); grade_object::set_properties($gradeitem, array('gradepass' => 50.0)); $gradeitem->update(); $assignrow = $assign->get_instance(); $grades = array(); $grades[$this->students[0]->id] = (object) ['rawgrade' => 60, 'userid' => $this->students[0]->id]; $assignrow->cmidnumber = null; assign_grade_item_update($assignrow, $grades); $actual = local::course_completion_progress($this->course); $this->assertInstanceOf('stdClass', $actual); $this->assertEquals(1, $actual->complete); $this->assertEquals(1, $actual->total); $this->assertEquals(100, $actual->progress); // Make sure course completion returns null when disabled at site level. $CFG->enablecompletion = false; $actual = local::course_completion_progress($this->course); $this->assertNull($actual); // Make sure course completion returns null when disabled at course level. $CFG->enablecompletion = true; $DB->update_record('course', (object) ['id' => $this->course->id, 'enablecompletion' => 0]); $this->course = $DB->get_record('course', ['id' => $this->course->id]); $actual = local::course_completion_progress($this->course); $this->assertNull($actual); }