Esempio n. 1
0
 /**
  * Assign the user to the program.
  * @param array $elements An array of elements to perform the action on.
  * @param bool $bulkaction Whether this is a bulk-action or not.
  * @return array An array to format as JSON and return to the Javascript.
  */
 protected function _respond_to_js(array $elements, $bulkaction)
 {
     global $DB;
     $pgmid = required_param('id', PARAM_INT);
     // Permissions.
     if (curriculumpage::can_enrol_into_curriculum($pgmid) !== true) {
         return array('result' => 'fail', 'msg' => get_string('not_permitted', 'local_elisprogram'));
     }
     foreach ($elements as $userid => $label) {
         if ($this->can_assign($pgmid, $userid) === true) {
             $stucur = new curriculumstudent(array('userid' => $userid, 'curriculumid' => $pgmid));
             $stucur->save();
         }
     }
     return array('result' => 'success', 'msg' => 'Success');
 }
 /**
  * Test successful program enrolment creation.
  */
 public function test_success()
 {
     global $DB;
     $this->give_permissions(array('local/elisprogram:program_enrol'));
     // Initialize version1elis importplugin for utility functions.
     $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis');
     // Create test program.
     $datagen = new elis_program_datagenerator($DB);
     $program = $datagen->create_program(array('idnumber' => 'TestProgramEnrolmentCreate'));
     $userid = $DB->get_field(user::TABLE, 'id', array('username' => 'assigninguser'));
     // Create the program enrolment record to delete.
     $stucur = new curriculumstudent(array('curriculumid' => $program->id, 'userid' => $userid));
     $stucur->save();
     // Perform enrolment delete.
     $data = array('program_idnumber' => $program->idnumber, 'user_username' => 'assigninguser', 'user_email' => '*****@*****.**');
     $response = local_datahub_elis_program_enrolment_delete::program_enrolment_delete($data);
     $this->assertNotEmpty($response);
     $this->assertInternalType('array', $response);
     $this->assertArrayHasKey('messagecode', $response);
     $this->assertArrayHasKey('message', $response);
     $this->assertEquals(get_string('ws_program_enrolment_delete_success_code', 'local_datahub'), $response['messagecode']);
     $this->assertEquals(get_string('ws_program_enrolment_delete_success_msg', 'local_datahub'), $response['message']);
     $this->assertFalse($DB->record_exists(curriculumstudent::TABLE, array('curriculumid' => $program->id, 'userid' => $userid)));
 }
 /**
  * Test available table doesn't show assigned programs.
  * @dataProvider dataprovider_available_doesnt_show_assigned_programs
  * @param array $associations An array of arrays of parameters to construct curriculumstudent associations.
  * @param int $tableuserid The user ID of the user we're going to manage.
  * @param array $expectedresults The expected page of results.
  * @param int $expectedtotal The expected number of total results.
  */
 public function test_available_doesnt_show_assigned_programs($associations, $tableuserid, $expectedresults, $expectedtotal)
 {
     global $USER, $DB, $CFG;
     $userbackup = $USER;
     // Set up permissions.
     $USER = $this->setup_permissions_test();
     $this->give_permission_for_context($USER->id, 'local/elisprogram:program_enrol', context_system::instance());
     foreach ($associations as $association) {
         $curriculumstudent = new curriculumstudent($association);
         $curriculumstudent->save();
     }
     // Construct test table.
     $table = new deepsight_datatable_userprogram_available_mock($DB, 'test', 'http://localhost', 'testuniqid');
     $table->set_userid($tableuserid);
     // Perform test.
     $actualresults = $table->get_search_results(array(), array(), 0, 20);
     // Verify result.
     $this->assert_search_results($expectedresults, $expectedtotal, $actualresults);
     // Restore user.
     $USER = $userbackup;
 }
 /**
  * Validate that mappings are applied during the program enrolment delete action
  */
 public function test_mapping_applied_during_program_enrolment_delete()
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/local/elisprogram/lib/data/curriculum.class.php';
     require_once $CFG->dirroot . '/local/elisprogram/lib/data/curriculumstudent.class.php';
     $this->init_mapping();
     $userid = $this->create_test_user();
     $program = new curriculum(array('idnumber' => 'testprogramidnumber'));
     $program->save();
     $curriculumstudent = new curriculumstudent(array('curriculumid' => $program->id, 'userid' => $userid));
     $curriculumstudent->save();
     // Run the program enrolment delete action.
     $record = new stdClass();
     $record->customaction = 'delete';
     $record->customcontext = 'curriculum_testprogramidnumber';
     $record->customuser_username = '******';
     $record->customuser_email = '*****@*****.**';
     $record->customuser_idnumber = 'testuseridnumber';
     $this->run_enrolment_import((array) $record);
     // Validation.
     $this->assertEquals(0, $DB->count_records(curriculumstudent::TABLE));
 }
Esempio n. 5
0
 /**
  * Enrols a user in a track.
  *
  * @param int $userid The user id
  * @param int $trackid The track id
  */
 public static function enrol($userid, $trackid)
 {
     global $DB;
     // make sure we don't double-enrol
     if ($DB->record_exists(self::TABLE, array('userid' => $userid, 'trackid' => $trackid))) {
         return false;
     }
     $record = new usertrack();
     $record->userid = $userid;
     $record->trackid = $trackid;
     $record->save();
     $user = new user($userid);
     $track = new track($trackid);
     if (!$DB->record_exists(curriculumstudent::TABLE, array('userid' => $userid, 'curriculumid' => $track->curid))) {
         $curstu = new curriculumstudent();
         $curstu->userid = $userid;
         $curstu->curriculumid = $track->curid;
         $curstu->completed = 0;
         $curstu->credits = 0;
         $curstu->locked = 0;
         $curstu->save();
     }
     events_trigger('track_assigned', $record);
     /**
      * Get autoenrollable classes in the track.  Classes are autoenrollable
      * if:
      * - the autoenrol flag is set
      * - it is the only class in that course slot for the track
      */
     $sql = 'SELECT classid, courseid ' . 'FROM {' . trackassignment::TABLE . '} ' . 'WHERE trackid = ? ' . 'GROUP BY courseid ' . 'HAVING COUNT(*) = 1 AND MAX(autoenrol) = 1';
     $params = array($trackid);
     $classes = $DB->get_recordset_sql($sql, $params);
     foreach ($classes as $class) {
         // ELIS-3460: check pre-requisites ...
         $curcrs = new curriculumcourse(array('courseid' => $class->courseid, 'curriculumid' => $track->curid));
         if (!$curcrs->prerequisites_satisfied($userid)) {
             //error_log("/local/elisprogram/lib/data/usertrack.class.php::enrol({$userid}); pre-requisites NOT satisfied for course: {$class->courseid}, curriculum: {$track->curid}");
             continue;
         }
         $now = time();
         // enrol user in each autoenrolable class
         $stu_record = new object();
         $stu_record->userid = $userid;
         $stu_record->classid = $class->classid;
         $stu_record->enrolmenttime = $now;
         $enrolment = new student($stu_record);
         // catch enrolment limits
         try {
             $status = $enrolment->save();
         } catch (pmclass_enrolment_limit_validation_exception $e) {
             // autoenrol into waitlist
             $wait_record = new object();
             $wait_record->userid = $userid;
             $wait_record->classid = $class->classid;
             $wait_record->enrolmenttime = $now;
             $wait_record->timecreated = $now;
             $wait_record->position = 0;
             $wait_list = new waitlist($wait_record);
             $wait_list->save();
             $status = true;
         } catch (Exception $e) {
             $param = array('message' => $e->getMessage());
             echo cm_error(get_string('record_not_created_reason', 'local_elisprogram', $param));
         }
     }
     unset($classes);
     return true;
 }
 /**
  * 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;
     }
 }
Esempio n. 7
0
 /**
  * Test validation of duplicates.
  * @expectedException data_object_validation_exception
  */
 public function test_curriculumstudent_validationpreventsduplicates()
 {
     $this->load_csv_data();
     $curriculumstudent = new curriculumstudent(array('curriculumid' => 1, 'userid' => 1));
     $curriculumstudent->save();
 }
 /**
  * Performs program_enrolment creation
  * @throws moodle_exception If there was an error in passed parameters.
  * @throws data_object_exception If there was an error creating the entity.
  * @param array $data The incoming data parameter.
  * @return array An array of parameters, if successful.
  */
 public static function program_enrolment_create(array $data)
 {
     global $USER, $DB;
     if (static::require_elis_dependencies() !== true) {
         throw new moodle_exception('ws_function_requires_elis', 'local_datahub');
     }
     // Parameter validation.
     $params = self::validate_parameters(self::program_enrolment_create_parameters(), array('data' => $data));
     // Context validation.
     $context = context_user::instance($USER->id);
     self::validate_context($context);
     $data = (object) $data;
     // Parse program
     if (empty($data->program_idnumber) || !($curid = $DB->get_field(curriculum::TABLE, 'id', array('idnumber' => $data->program_idnumber)))) {
         throw new data_object_exception('ws_program_enrolment_create_fail_invalid_program', 'local_datahub', '', $data);
     }
     // Capability checking.
     require_capability('local/elisprogram:program_enrol', \local_elisprogram\context\program::instance($curid));
     // Initialize version1elis importplugin for utility functions.
     $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis');
     $userparams = array();
     $userid = $importplugin->get_userid_from_record($data, '', $userparams);
     if ($userid == false) {
         $a = new stdClass();
         if (empty($userparams)) {
             $a->userparams = '{empty}';
         } else {
             $a->userparams = '';
             foreach ($userparams as $userfield => $uservalue) {
                 $subfield = strpos($userfield, '_');
                 $userfield = substr($userfield, $subfield === false ? 0 : $subfield + 1);
                 if (!empty($a->userparams)) {
                     $a->userparams .= ', ';
                 }
                 $a->userparams .= "{$userfield}: '{$uservalue}'";
             }
         }
         throw new data_object_exception('ws_program_enrolment_create_fail_invalid_user', 'local_datahub', '', $a);
     }
     $record = new stdClass();
     $record->userid = $userid;
     $record->curriculumid = $curid;
     if (isset($data->credits)) {
         $record->credits = $data->credits;
     }
     if (isset($data->locked)) {
         $record->locked = $data->locked ? 1 : 0;
     }
     if (isset($data->timecompleted)) {
         $record->timecompleted = $importplugin->parse_date($data->timecompleted);
     }
     if (isset($data->timeexpired)) {
         $record->timeexpired = $importplugin->parse_date($data->timeexpired);
     }
     $stucur = new curriculumstudent($record);
     $stucur->save();
     // Respond.
     if (!empty($stucur->id)) {
         return array('messagecode' => get_string('ws_program_enrolment_create_success_code', 'local_datahub'), 'message' => get_string('ws_program_enrolment_create_success_msg', 'local_datahub'), 'record' => $stucur->to_array());
     } else {
         throw new data_object_exception('ws_program_enrolment_create_fail', 'local_datahub');
     }
 }
Esempio n. 9
0
 function curriculum_enrolment_create($record, $filename, $idnumber)
 {
     global $DB, $CFG;
     if (!($curid = $DB->get_field(curriculum::TABLE, 'id', array('idnumber' => $idnumber)))) {
         $this->fslogger->log_failure("instance value of \"{$idnumber}\" does not refer to a valid instance of a program context.", 0, $filename, $this->linenumber, $record, "enrolment");
         return false;
     }
     $userid = $this->get_userid_from_record($record, $filename);
     //string to describe the user
     $user_descriptor = $this->get_user_descriptor($record, false, 'user_');
     if ($DB->record_exists(curriculumstudent::TABLE, array('curriculumid' => $curid, 'userid' => $userid))) {
         $this->fslogger->log_failure("User with {$user_descriptor} is already enrolled in program \"{$idnumber}\".", 0, $filename, $this->linenumber, $record, "enrolment");
         return false;
     }
     if (!$this->validate_program_enrolment_data('create', $record, $filename)) {
         return false;
     }
     $record->userid = $userid;
     $record->curriculumid = $curid;
     $stucur = new curriculumstudent($record);
     $stucur->save();
     //log success
     $success_message = "User with {$user_descriptor} successfully enrolled in program \"{$idnumber}\".";
     $this->fslogger->log_success($success_message, 0, $filename, $this->linenumber);
     return true;
 }
Esempio n. 10
0
 static function cluster_assigned_handler($eventdata)
 {
     require_once elispm::lib('data/clusterassignment.class.php');
     require_once elispm::lib('data/clustercurriculum.class.php');
     require_once elispm::lib('data/curriculumstudent.class.php');
     require_once elispm::lib('data/clustertrack.class.php');
     require_once elispm::lib('data/usertrack.class.php');
     $assignment = new clusterassignment($eventdata);
     $userset = $assignment->cluster;
     // assign user to the curricula associated with the cluster
     /**
      * @todo we may need to change this if associating a user with a
      * curriculum does anything more complicated
      */
     // enrol user in associated curricula
     $prog_assocs = $userset->clustercurriculum;
     foreach ($prog_assocs as $prog_assoc) {
         if ($prog_assoc->autoenrol && !curriculumstudent::exists(array(new field_filter('userid', $eventdata->userid), new field_filter('curriculumid', $prog_assoc->curriculumid)))) {
             $progass = new curriculumstudent();
             $progass->userid = $eventdata->userid;
             $progass->curriculumid = $prog_assoc->curriculumid;
             $progass->timecreated = $progass->timemodified = time();
             $progass->save();
         }
     }
     // enrol user in associated tracks if autoenrol flag is set on the cluster-track associations
     $track_assocs = $userset->clustertrack;
     foreach ($track_assocs as $track_assoc) {
         if ($track_assoc->autoenrol && !usertrack::exists(array(new field_filter('userid', $eventdata->userid), new field_filter('trackid', $track_assoc->trackid)))) {
             usertrack::enrol($eventdata->userid, $track_assoc->trackid);
         }
     }
     return true;
 }