Beispiel #1
0
 /**
  * When a role is selected from the sync configuration, create class
  * enrolments for the specified role assignments.
  */
 static function instructor_sync_role_set()
 {
     global $DB;
     $roleid = get_config('elisprogram_enrolrolesync', 'instructor_role');
     if (empty($roleid)) {
         //not configured
         return;
     }
     //include dependencies
     require_once elispm::lib('data/instructor.class.php');
     // find all class role assignments
     $sql = "SELECT ra.id, cu.id AS userid, ctx.instanceid AS classid\n                  FROM {role_assignments} ra\n                  JOIN {user} mu ON ra.userid = mu.id\n                  JOIN {" . user::TABLE . "} cu ON mu.idnumber = cu.idnumber\n                  JOIN {context} ctx ON ctx.id = ra.contextid\n                  LEFT JOIN {" . instructor::TABLE . "} inst\n                    ON cu.id = inst.userid\n                    AND ctx.instanceid = inst.classid\n                 WHERE ctx.contextlevel = :contextlevel\n                   AND ra.roleid = :roleid\n                   AND inst.id IS NULL";
     $params = array('contextlevel' => CONTEXT_ELIS_CLASS, 'roleid' => $roleid);
     $instructorswanted = $DB->get_recordset_sql($sql, $params);
     $instructorswanted = $instructorswanted ? $instructorswanted : array();
     //iterate and add where needed
     foreach ($instructorswanted as $instructor) {
         unset($instructor->id);
         $instructor = new instructor($instructor);
         $instructor->assigntime = time();
         try {
             $instructor->save();
         } catch (Exception $e) {
             //validation failed
         }
     }
     return true;
 }
 /**
  * Validate that mappings are applied during the instructor enrolment delete action
  */
 public function test_mapping_applied_during_instructor_enrolment_delete()
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/local/elisprogram/lib/data/course.class.php';
     require_once $CFG->dirroot . '/local/elisprogram/lib/data/instructor.class.php';
     require_once $CFG->dirroot . '/local/elisprogram/lib/data/pmclass.class.php';
     $this->init_mapping();
     $userid = $this->create_test_user();
     $course = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => ''));
     $course->save();
     $pmclass = new pmclass(array('courseid' => $course->id, 'idnumber' => 'testclassidnumber'));
     $pmclass->save();
     $instructor = new instructor(array('classid' => $pmclass->id, 'userid' => $userid));
     $instructor->save();
     // Run the instructor enrolment delete action.
     $record = new stdClass();
     $record->customaction = 'delete';
     $record->customcontext = 'class_testclassidnumber';
     $record->customuser_username = '******';
     $record->customuser_email = '*****@*****.**';
     $record->customuser_idnumber = 'testuseridnumber';
     $record->customrole = 'instructor';
     $this->run_enrolment_import((array) $record);
     // Validation.
     $this->assertEquals(0, $DB->count_records(instructor::TABLE));
 }
 /**
  * Validate that the role field is handled as needed during unassignment
  *
  * @param string $role The input value for the role field
  * @dataProvider role_provider
  */
 public function test_elis_user_instructor_unenrolment_handles_role($role)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php';
     require_once elispm::lib('data/course.class.php');
     require_once elispm::lib('data/instructor.class.php');
     require_once elispm::lib('data/pmclass.class.php');
     require_once elispm::lib('data/user.class.php');
     set_config('coursecontact', 'teacher,editingteacher');
     set_config('default_instructor_role', 'teacher', 'local_elisprogram');
     $user = new user(array('idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'country' => 'CA'));
     $user->save();
     $course = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => ''));
     $course->save();
     $class = new pmclass(array('courseid' => $course->id, 'idnumber' => 'testclassidnumber'));
     $class->save();
     $instructor = new instructor(array('userid' => $user->id, 'classid' => $class->id));
     $instructor->save();
     // Validate setup.
     $this->assertTrue($DB->record_exists(instructor::TABLE, array('userid' => $user->id, 'classid' => $class->id)));
     // Run the instructor assignment delete action.
     $record = new stdClass();
     $record->context = 'class_testclassidnumber';
     $record->user_username = '******';
     $record->role = $role;
     $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis');
     $importplugin->fslogger = new silent_fslogger(null);
     $importplugin->class_enrolment_delete($record, 'bogus', 'testclassidnumber');
     // Validation.
     $this->assertEquals(0, $DB->count_records(instructor::TABLE));
 }
 /**
  * Enrol the test user in the provided context
  *
  * @param string $contextlevel The string descriptor of the context level
  * @param string $role The shortname of the import record's role column
  */
 private function create_enrolment($contextlevel, $role)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php';
     switch ($contextlevel) {
         case 'curriculum':
             // Program enrolment.
             require_once elispm::lib('data/curriculumstudent.class.php');
             $data = array('curriculumid' => 1, 'userid' => 1);
             $curriculumstudent = new curriculumstudent($data);
             $curriculumstudent->save();
             break;
         case 'track':
             // Track enrolment.
             require_once elispm::lib('data/usertrack.class.php');
             $data = array('trackid' => 1, 'userid' => 1);
             $usertrack = new usertrack($data);
             $usertrack->save();
             break;
         case 'cluster':
             // User set enrolment.
             require_once elispm::lib('data/clusterassignment.class.php');
             $data = array('clusterid' => 1, 'userid' => 1);
             $clusterassignment = new clusterassignment($data);
             $clusterassignment->save();
             break;
         case 'class':
             if ($role == 'instructor') {
                 // Class instance instructor enrolment.
                 require_once elispm::lib('data/instructor.class.php');
                 $data = array('classid' => 1, 'userid' => 1);
                 $instructor = new instructor($data);
                 $instructor->save();
             } else {
                 // Class instance student enrolment.
                 require_once elispm::lib('data/student.class.php');
                 $data = array('classid' => 1, 'userid' => 1);
                 $student = new student($data);
                 $student->save();
             }
             break;
         case 'user':
             // Moodle user role assignment.
             $roleid = $DB->get_field('role', 'id', array('shortname' => $role));
             $userid = $DB->get_field('user', 'id', array('idnumber' => 'testuseridnumber'));
             $context = context_user::instance($userid);
             role_assign($roleid, $userid, $context->id);
             break;
         default:
             break;
     }
 }
Beispiel #5
0
/**
 *
 * Takes a role assignment event from Moodle and assigns class instructors
 * in curriculum admin appropriately, based on "course manager" roles
 *
 * @param  stdClass  $eventdata  The appropriate role_assignments record
 *
 */
function pm_assign_instructor_from_mdl($eventdata)
{
    global $CFG, $DB;
    //make sure we have course manager roles defined
    if (empty($CFG->coursecontact)) {
        return;
    }
    //retrieve the appropriate roles
    $valid_instructor_roles = explode(',', $CFG->coursecontact);
    //make sure the assigned role is one of the ones we care about
    if (!in_array($eventdata->roleid, $valid_instructor_roles)) {
        return;
    }
    //get the id of the appropriate curriculum user
    if (!($instructorid = pm_get_crlmuserid($eventdata->userid))) {
        return;
    }
    //get the curriculum user object
    $instructor = new user($instructorid);
    //get the role assignment context
    if (!($context = $DB->get_record('context', array('id' => $eventdata->contextid)))) {
        return;
    }
    //make sure we're using a course context
    if ($context->contextlevel != CONTEXT_COURSE) {
        return;
    }
    //make sure the Moodle course is not tied to other curriculum administartion classes
    if ($DB->count_records(classmoodlecourse::TABLE, array('moodlecourseid' => $context->instanceid)) != 1) {
        return true;
    }
    //make sure the course is tied to at least one class
    if (!($crlm_class = $DB->get_record(classmoodlecourse::TABLE, array('moodlecourseid' => $context->instanceid)))) {
        return;
    }
    //add user as instructor for the appropriate class
    if (!$DB->record_exists(instructor::TABLE, array('classid' => $crlm_class->classid, 'userid' => $instructorid))) {
        $ins_record = new instructor(array('classid' => $crlm_class->classid, 'userid' => $instructorid, 'assigntime' => $eventdata->timestart, 'completetime' => $eventdata->timeend));
        $ins_record->save();
    }
}
Beispiel #6
0
 /**
  * Update an instructor class instance assignment
  *
  * @param object $record One record of import data
  * @param string $filename The import file name, used for logging
  * @param string $idnumber The idnumber of the class instance
  *
  * @return boolean true on success, otherwise false
  * @uses   $CFG
  * @uses   $DB
  */
 function class_enrolment_update_instructor($record, $filename, $idnumber)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php';
     require_once elispm::lib('data/instructor.class.php');
     require_once elispm::lib('data/pmclass.class.php');
     if (!($crsid = $DB->get_field(pmclass::TABLE, 'id', array('idnumber' => $idnumber)))) {
         $this->fslogger->log_failure("instance value of \"{$idnumber}\" does not refer to a valid instance of a class context.", 0, $filename, $this->linenumber, $record, "enrolment");
         return false;
     }
     if (!$this->validate_class_enrolment_data('update', $record, $filename)) {
         return false;
     }
     // obtain the class id
     $classid = $DB->get_field(pmclass::TABLE, 'id', array('idnumber' => $idnumber));
     // obtain the user id
     $userid = $this->get_userid_from_record($record, $filename);
     // update the record
     $id = $DB->get_field(instructor::TABLE, 'id', array('classid' => $classid, 'userid' => $userid));
     $instructor = new instructor($id);
     $instructor->load();
     // enrolment and completion times
     if (isset($record->assigntime)) {
         $instructor->assigntime = $this->parse_date($record->assigntime);
     }
     if (isset($record->completetime)) {
         $instructor->completetime = $this->parse_date($record->completetime);
     }
     if (!empty($record->role)) {
         $instructor->roleshortname = $record->role;
     }
     $instructor->save();
     // string to describe the user
     $user_descriptor = $this->get_user_descriptor($record, false, 'user_');
     // log success
     $success_message = "Instructor enrolment for user with {$user_descriptor} in class instance \"{$idnumber}\" successfully updated.";
     $this->fslogger->log_success($success_message, 0, $filename, $this->linenumber);
     return true;
 }
 /**
  * Validate that create actions are converted to updates for instructor enrolments when the
  * "createorupdate" flag is enabled
  */
 public function test_elis_createorupdate_updates_instructor_enrolment()
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/local/elisprogram/lib/data/course.class.php';
     require_once $CFG->dirroot . '/local/elisprogram/lib/data/pmclass.class.php';
     require_once $CFG->dirroot . '/local/elisprogram/lib/data/instructor.class.php';
     require_once $CFG->dirroot . '/local/elisprogram/lib/data/user.class.php';
     // Set up initial conditions.
     set_config('createorupdate', 1, 'dhimport_version1elis');
     // Create the test course.
     $course = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => ''));
     $course->save();
     // Create the test class.
     $class = new pmclass(array('courseid' => $course->id, 'idnumber' => 'testclassidnumber'));
     $class->save();
     // Create the test user.
     $user = new user(array('username' => 'testuserusername', 'email' => '*****@*****.**', 'idnumber' => 'testuseridnumber', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'country' => 'CA'));
     $user->save();
     // Create the instructor enrolment user.
     $instructor = new instructor(array('classid' => $class->id, 'userid' => $user->id, 'completetime' => rlip_timestamp(0, 0, 0, 1, 1, 2012)));
     $instructor->save();
     // Run the instructor enrolment create action.
     $record = new stdClass();
     $record->action = 'create';
     $record->context = 'class_testclassidnumber';
     $record->user_username = '******';
     $record->role = 'instructor';
     $record->completetime = 'Jan/02/2012';
     $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis');
     $importplugin->fslogger = new silent_fslogger(null);
     $importplugin->process_record('enrolment', $record, 'bogus');
     // Validation.
     $this->assertTrue($DB->record_exists(instructor::TABLE, array('classid' => $class->id, 'userid' => $user->id, 'completetime' => rlip_timestamp(0, 0, 0, 1, 2, 2012))));
 }
Beispiel #8
0
 /**
  * Test removing a user having one of the course contact roles.
  */
 public function test_unenrol_user_with_coursecontact_role()
 {
     global $DB, $CFG;
     $this->resetAfterTest(true);
     $this->load_csv_data();
     $this->set_config_instructor_role();
     // Enroll and assign user teacher role in course
     $instructor = new instructor(array('userid' => 104, 'classid' => 100));
     $instructor->save();
     // Define user and course objects
     $user = new stdClass();
     $user->id = 101;
     $context = context_course::instance(100);
     // Manually assign another non-editing teacher role to prevent the user from being automatically unenrolled due to having no role in a course
     $role = $DB->get_record('role', array('shortname' => 'teacher'));
     role_assign($role->id, $user->id, $context->id);
     // Add role as a course contact
     $CFG->coursecontact .= ',' . $role->id;
     // Create an instance of the elis enrolment
     $course = new stdClass();
     $course->id = $context->instanceid;
     $plugin = enrol_get_plugin('elis');
     $enrolinstance = $plugin->get_or_create_instance($course);
     // Get the configured teache role id
     $teacherroleid = get_config('local_elisprogram', 'default_instructor_role');
     // Unassign the teacher role from the user
     role_unassign($teacherroleid, $user->id, $context->id, 'enrol_elis', $enrolinstance->id);
     // Initiate the delete method
     $instructor->delete();
     $result = is_enrolled($context, $user, '', true);
     $this->assertFalse($result);
 }
 /**
  * Test table doesn't show assigned students or instructors
  * @dataProvider dataprovider_available_doesnt_show_students_instructors
  * @param array $associations An array of arrays of parameters to construct student associations.
  * @param int $tableclassid The ID of the class we're managing.
  * @param array $expectedresults The expected page of results.
  */
 public function test_available_doesnt_show_students_instructors($students, $instructors, $tableclassid, $expectedresults)
 {
     global $USER, $DB, $CFG;
     $userbackup = $USER;
     // Set up permissions.
     $USER = $this->setup_permissions_test();
     $this->give_permission_for_context($USER->id, 'local/elisprogram:assign_class_instructor', context_system::instance());
     // Create associations.
     foreach ($students as $student) {
         $student = new student($student);
         $student->save();
     }
     foreach ($instructors as $instructor) {
         $instructor = new instructor($instructor);
         $instructor->save();
     }
     $table = new deepsight_datatable_instructor_available_mock($DB, 'test', 'http://localhost', 'testuniqid');
     $table->set_classid($tableclassid);
     $actualresults = $table->get_search_results(array(), array(), 0, 20);
     $this->assert_search_results($expectedresults, count($expectedresults), $actualresults);
     // Restore user.
     $USER = $userbackup;
 }