/** * Validate that the sync from course role assignment to class instance enrolment works */ public function test_enrolled_course_user_syncstoclass() { global $CFG, $DB; require_once elispm::lib('lib.php'); // Set up import data. $this->load_csv_data(); // Make sure the context is set up. $crsctx = context_course::instance(100); // Set up our test role. $roleid = create_role('gradedrole', 'gradedrole', 'gradedrole'); set_config('gradebookroles', $roleid); // Create role assignments. role_assign($roleid, 100, $crsctx->id); // Attempt the sync. $sync = new \local_elisprogram\moodle\synchronize(); $sync->synchronize_moodle_class_grades(); // Make sure the student record was created. $student = student::find(); $this->assertTrue($student->valid()); // Make sure the student has the right class id. $student = $student->current(); $this->assertEquals(100, $student->classid); }
/** * Get all of the data from Moodle and update the curriculum system. * This should do the following: * - Get all Moodle courses connected with classes. * - Get all users in each Moodle course. * - Get grade records from the class's course and completion elements. * - For each user: * - Check if they have an enrolment record in CM, and add if not. * - Update grade information in the enrollment and grade tables in CM. * * @param int $muserid optional user to update, default(0) updates all users */ function pm_update_student_progress($muserid = 0) { global $CFG; require_once $CFG->dirroot . '/grade/lib.php'; require_once $CFG->dirroot . '/grade/querylib.php'; /// Get all grades in all relevant courses for all relevant users. require_once elispm::lib('data/classmoodlecourse.class.php'); require_once elispm::lib('data/student.class.php'); require_once elispm::lib('data/pmclass.class.php'); require_once elispm::lib('data/course.class.php'); /// Start with the Moodle classes... if ($muserid == 0) { if (in_cron()) { mtrace("Synchronizing Moodle class grades<br />\n"); } } $sync = new \local_elisprogram\moodle\synchronize(); $sync->synchronize_moodle_class_grades($muserid); flush(); // sleep(1); /// Now we need to check all of the student and grade records again, since data may have come from sources /// other than Moodle. if ($muserid == 0) { //running for all users if (in_cron()) { mtrace("Updating all class grade completions.<br />\n"); } pm_update_enrolment_status(); } else { //attempting to run for a particular user $pmuserid = pm_get_crlmuserid($muserid); if ($pmuserid != false) { //user has a matching PM user pm_update_enrolment_status($pmuserid); } } return true; }
/** * Test get_moodlegrades method. */ public function test_get_moodlegrades() { global $DB; // Create grade_items. $gradeitems = array(); foreach (array(10, 20) as $courseid) { $gradeitems[$courseid]['course'] = (object) array('courseid' => $courseid, 'itemname' => null, 'itemtype' => 'course', 'idnumber' => null); $gradeitems[$courseid]['course']->id = $DB->insert_record('grade_items', $gradeitems[$courseid]['course']); $gradeitems[$courseid]['optionalexam'] = (object) array('courseid' => $courseid, 'itemname' => 'Optional Exam', 'itemtype' => 'mod', 'itemmodule' => 'assignment', 'idnumber' => 'optionalexam'); $gradeitems[$courseid]['optionalexam']->id = $DB->insert_record('grade_items', $gradeitems[$courseid]['optionalexam']); $gradeitems[$courseid]['midtermexam'] = (object) array('courseid' => $courseid, 'itemname' => 'Midterm Exam', 'itemtype' => 'mod', 'itemmodule' => 'assignment', 'idnumber' => 'midtermexam'); $gradeitems[$courseid]['midtermexam']->id = $DB->insert_record('grade_items', $gradeitems[$courseid]['midtermexam']); $gradeitems[$courseid]['finalexam'] = (object) array('courseid' => $courseid, 'itemname' => 'Final Exam', 'itemtype' => 'mod', 'itemmodule' => 'assignment', 'idnumber' => 'finalexam'); $gradeitems[$courseid]['finalexam']->id = $DB->insert_record('grade_items', $gradeitems[$courseid]['finalexam']); } // Create grade_grades. $gradegrades = array(); $i = 60; foreach (array(100, 110) as $userid) { $usergrades = array(); foreach ($gradeitems as $courseid => $items) { $usercoursegrades = array(); foreach ($items as $item) { if ($item->idnumber !== 'optionalexam') { $usercoursegrades[$item->id] = (object) array('itemid' => $item->id, 'userid' => $userid, 'finalgrade' => $i); $usercoursegrades[$item->id]->id = $DB->insert_record('grade_grades', $usercoursegrades[$item->id]); $i++; } } $usergrades[$courseid] = $usercoursegrades; } $gradegrades[$userid] = $usergrades; } $userid = 100; $courseid = 10; $gis = array(); foreach ($gradeitems[$courseid] as $item) { $gis[$item->id] = $item; } $sync = new \local_elisprogram\moodle\synchronize(); $moodlegrades = $sync->get_moodlegrades($userid, $courseid, $gis); // Check that we have data for all passed itemids, and only data for the passed itemids. $this->assertEquals(count($gis), count($moodlegrades)); $receiveditemids = array(); foreach ($moodlegrades as $gradegrade) { $receiveditemids[] = $gradegrade->itemid; } $this->assertEquals($receiveditemids, array_keys($gis)); // Assert returned data. foreach ($gradeitems[$courseid] as $item) { $this->assertArrayHasKey($item->id, $moodlegrades); $this->assertTrue($moodlegrades[$item->id] instanceof \grade_grade); $this->assertEquals($userid, $moodlegrades[$item->id]->userid); if ($item->idnumber === 'optionalexam') { // Assert we have a grade_grade object even if no grade data was found in the db for a given item. $this->assertEmpty($moodlegrades[$item->id]->finalgrade); } else { // Assert we have accurate finalgrade data. $this->assertEquals($gradegrades[$userid][$courseid][$item->id]->finalgrade, $moodlegrades[$item->id]->finalgrade); } } }
/** * Test the grade synchronisation when there are duplicate course_module.idnumber values present. */ public function test_sync_with_duplicate_course_module_idnumbers() { global $CFG, $DB; $this->load_csv_data(); $olddebug = null; $olddebugdisplay = null; // Developer debugging must be enabled and displayed for this test to work. if ($CFG->debug < DEBUG_DEVELOPER) { $olddebug = $CFG->debug; $CFG->debug = DEBUG_DEVELOPER; } if ($CFG->debugdisplay == false) { $olddebugdisplay = false; $CFG->debugdisplay = true; } // Set up grade item and completion item. $itemid = $this->create_grade_item('duplicateidnumber'); $this->create_grade_grade($itemid, 100, 75); $completionid = $this->create_course_completion('duplicateidnumber'); // Insert a couple duplicate course_module 'idnumber' balues but for different course ID values. $cmobj = new \stdClass(); $cmobj->course = 1000; $cmobj->module = 20; $cmobj->instance = 100; $cmobj->section = 1; $cmobj->idnumber = 'duplicateidnumber'; $DB->insert_record('course_modules', $cmobj); $cmobj->course = 2000; $DB->insert_record('course_modules', $cmobj); // Using an output buffer here because the following function will throw a debugging error if more than one record is found. ob_start(); $sync = new \local_elisprogram\moodle\synchronize(); $sync->synchronize_moodle_class_grades(); $buffer = ob_get_contents(); ob_end_clean(); $this->assertEquals('', $buffer); // Restore old values if we modified them in this test. if ($olddebug != null) { $CFG->debug = $olddebug; } if ($olddebugdisplay != null) { $CFG->debugdisplay = $olddebugdisplay; } }