MDL-37964 very basic support for enrolments in data generator

This commit is contained in:
Petr Škoda
2013-02-11 17:29:00 +08:00
committed by Mark Nelson
parent c1921325d4
commit 9ba2682e38
2 changed files with 117 additions and 0 deletions
+35
View File
@@ -543,4 +543,39 @@ EOD;
return $DB->get_record('scale', array('id'=>$id), '*', MUST_EXIST);
}
/**
* Simplified enrolment of user to course using default options.
*
* It is strongly recommended to use only this method for 'manual' and 'self' plugins only!!!
*
* @param int $userid
* @param int $courseid
* @param int $roleid optional role id, use only with manual plugin
* @param string $enrol name of enrol plugin,
* there must be exactly one instance in course,
* it must support enrol_user() method.
* @return bool success
*/
public function enrol_user($userid, $courseid, $roleid = null, $enrol = 'manual') {
global $DB;
if (!$plugin = enrol_get_plugin($enrol)) {
return false;
}
$instances = $DB->get_records('enrol', array('courseid'=>$courseid, 'enrol'=>$enrol));
if (count($instances) != 1) {
return false;
}
$instance = reset($instances);
if (is_null($roleid) and $instance->roleid) {
$roleid = $instance->roleid;
}
$plugin->enrol_user($instance, $userid, $roleid);
return true;
}
}