MDL-23532 enrol - moved UI enrolment components from core enrol to their respective enrolment plugins

This commit is contained in:
Sam Hemelryk
2011-04-21 10:02:54 +08:00
parent 4452ed6f17
commit b69ca6bee4
11 changed files with 371 additions and 182 deletions
+60 -10
View File
@@ -64,32 +64,40 @@ class enrol_cohort_plugin extends enrol_plugin {
* @return moodle_url page url
*/
public function get_newinstance_link($courseid) {
if (!$this->can_add_new_instances($courseid)) {
return NULL;
}
// multiple instances supported - multiple parent courses linked
return new moodle_url('/enrol/cohort/addinstance.php', array('id'=>$courseid));
}
/**
* Given a courseid this function returns true if the user is able to enrol or configure cohorts
* AND there are cohorts that the user can view.
*
* @param int $courseid
* @return bool
*/
protected function can_add_new_instances($courseid) {
global $DB;
$coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
if (!has_capability('moodle/course:enrolconfig', $coursecontext) or !has_capability('enrol/cohort:config', $coursecontext)) {
return NULL;
return false;
}
list($sqlparents, $params) = $DB->get_in_or_equal(get_parent_contexts($coursecontext));
$sql = "SELECT id, contextid
FROM {cohort}
WHERE contextid $sqlparents
ORDER BY name ASC";
$cohorts = $DB->get_records_sql($sql, $params);
$found = false;
foreach ($cohorts as $c) {
$context = get_context_instance_by_id($c->contextid);
if (has_capability('moodle/cohort:view', $context)) {
$found = true;
break;
return true;
}
}
if (!$found) {
return NULL;
}
// multiple instances supported - multiple parent courses linked
return new moodle_url('/enrol/cohort/addinstance.php', array('id'=>$courseid));
return false;
}
/**
@@ -129,6 +137,48 @@ class enrol_cohort_plugin extends enrol_plugin {
}
}
/**
* Returns a button to enrol a cohort or its users through the manual enrolment plugin.
*
* This function also adds a quickenrolment JS ui to the page so that users can be enrolled
* via AJAX.
*
* @global moodle_page $PAGE
* @param course_enrolment_manager $manager
* @return enrol_user_button
*/
public function get_manual_enrol_button(course_enrolment_manager $manager) {
global $PAGE;
$course = $manager->get_course();
if (!$this->can_add_new_instances($course->id)) {
return false;
}
$cohorturl = new moodle_url('/enrol/cohort/addinstance.php', array('id' => $course->id));
$button = new enrol_user_button($cohorturl, get_string('enrolcohort', 'enrol'), 'get');
$button->class .= ' enrol_cohort_plugin';
$button->strings_for_js(array('enrol','synced','enrolcohort','enrolcohortusers'), 'enrol');
$button->strings_for_js('assignroles', 'role');
$button->strings_for_js('cohort', 'cohort');
$button->strings_for_js('users', 'moodle');
// No point showing this at all if the user cant manually enrol users
$hasmanualinstance = has_capability('enrol/manual:manage', $manager->get_context()) && $manager->has_instance('manual');
$modules = array('moodle-enrol_cohort-quickenrolment', 'moodle-enrol_cohort-quickenrolment-skin');
$function = 'M.enrol_cohort.quickenrolment.init';
$arguments = array(
'courseid' => $course->id,
'ajaxurl' => '/enrol/ajax.php',
'url' => $PAGE->url->out(false),
'manualEnrolment' => $hasmanualinstance);
$button->require_yui_module($modules, $function, array($arguments));
return $button;
}
}

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB

@@ -1,4 +1,4 @@
YUI.add('moodle-enrol-quickcohortenrolment', function(Y) {
YUI.add('moodle-enrol_cohort-quickenrolment', function(Y) {
var CONTROLLERNAME = 'Quick cohort enrolment controller',
COHORTNAME = 'Cohort',
@@ -71,7 +71,7 @@ YUI.add('moodle-enrol-quickcohortenrolment', function(Y) {
this.on('defaultcohortroleloaded', this.updateContent, this, panel);
close.on('click', this.hide, this);
Y.all('.enrolcohortbutton input').each(function(node){
Y.all('.enrol_cohort_plugin input').each(function(node){
if (node.getAttribute('type', 'submit')) {
node.on('click', this.show, this);
}
@@ -332,8 +332,8 @@ YUI.add('moodle-enrol-quickcohortenrolment', function(Y) {
});
Y.augment(COHORT, Y.EventTarget);
M.enrol = M.enrol || {};
M.enrol.quickcohortenrolment = {
M.enrol_cohort = M.enrol || {};
M.enrol_cohort.quickenrolment = {
init : function(cfg) {
new CONTROLLER(cfg);
}
+135
View File
@@ -986,6 +986,141 @@ class course_enrolment_manager {
}
return $userdetails;
}
public function get_manual_enrol_buttons() {
$plugins = $this->get_enrolment_plugins();
$buttons = array();
foreach ($plugins as $plugin) {
$newbutton = $plugin->get_manual_enrol_button($this);
if (is_array($newbutton)) {
$buttons += $newbutton;
} else if ($newbutton instanceof enrol_user_button) {
$buttons[] = $newbutton;
}
}
return $buttons;
}
public function has_instance($enrolpluginname) {
// Make sure manual enrolments instance exists
foreach ($this->get_enrolment_instances() as $instance) {
if ($instance->enrol == $enrolpluginname) {
return true;
}
}
return false;
}
}
/**
* A button that is used to enrol users in a course
*
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_user_button extends single_button {
/**
* An array containing JS YUI modules required by this button
* @var array
*/
protected $jsyuimodules = array();
/**
* An array containing JS initialisation calls required by this button
* @var array
*/
protected $jsinitcalls = array();
/**
* An array strings required by JS for this button
* @var array
*/
protected $jsstrings = array();
/**
* Initialises the new enrol_user_button
*
* @staticvar int $count The number of enrol user buttons already created
* @param moodle_url $url
* @param string $label The text to display in the button
* @param string $method Either post or get
*/
public function __construct(moodle_url $url, $label, $method = 'post') {
static $count = 0;
$count ++;
parent::__construct($url, $label, $method);
$this->class = 'singlebutton enrolusersbutton';
$this->formid = 'enrolusersbutton-'.$count;
}
/**
* Adds a YUI module call that will be added to the page when the button is used.
*
* @param string|array $modules One or more modules to require
* @param string $function The JS function to call
* @param array $arguments An array of arguments to pass to the function
* @param string $galleryversion The YUI gallery version of any modules required
* @param bool $ondomready If true the call is postponed until the DOM is finished loading
*/
public function require_yui_module($modules, $function, array $arguments = null, $galleryversion = '2010.04.08-12-35', $ondomready = false) {
$js = new stdClass;
$js->modules = (array)$modules;
$js->function = $function;
$js->arguments = $arguments;
$js->galleryversion = $galleryversion;
$js->ondomready = $ondomready;
$this->jsyuimodules[] = $js;
}
/**
* Adds a JS initialisation call to the page when the button is used.
*
* @param string $function The function to call
* @param array $extraarguments An array of arguments to pass to the function
* @param bool $ondomready If true the call is postponed until the DOM is finished loading
* @param array $module A module definition
*/
public function require_js_init_call($function, array $extraarguments = null, $ondomready = false, array $module = null) {
$js = new stdClass;
$js->function = $function;
$js->extraarguments = $extraarguments;
$js->ondomready = $ondomready;
$js->module = $module;
$this->jsinitcalls[] = $js;
}
/**
* Requires strings for JS that will be loaded when the button is used.
*
* @param type $identifiers
* @param string $component
* @param mixed $a
*/
public function strings_for_js($identifiers, $component = 'moodle', $a = null) {
$string = new stdClass;
$string->identifiers = (array)$identifiers;
$string->component = $component;
$string->a = $a;
$this->jsstrings[] = $string;
}
/**
* Initialises the JS that is required by this button
*
* @param moodle_page $page
*/
public function initialise_js(moodle_page $page) {
foreach ($this->jsyuimodules as $js) {
$page->requires->yui_module($js->modules, $js->function, $js->arguments, $js->galleryversion, $js->ondomready);
}
foreach ($this->jsinitcalls as $js) {
$page->requires->js_init_call($js->function, $js->extraarguments, $js->ondomready, $js->module);
}
foreach ($this->jsstrings as $string) {
$page->requires->strings_for_js($string->identifiers, $string->component, $string->a);
}
}
}
class enrol_ajax_exception extends moodle_exception {
+75
View File
@@ -168,6 +168,81 @@ class enrol_manual_plugin extends enrol_plugin {
return parent::add_instance($course, $fields);
}
/**
* Returns a button to manually enrol users through the manual enrolment plugin.
*
* By default the first manual enrolment plugin instance available in the course is used.
* If no manual enrolment instances exist within the course then false is returned.
*
* This function also adds a quickenrolment JS ui to the page so that users can be enrolled
* via AJAX.
*
* @global moodle_page $PAGE
* @param course_enrolment_manager $manager
* @return enrol_user_button
*/
public function get_manual_enrol_button(course_enrolment_manager $manager) {
global $PAGE;
$instance = null;
$instances = array();
foreach ($manager->get_enrolment_instances() as $tempinstance) {
if ($tempinstance->enrol == 'manual') {
if ($instance === null) {
$instance = $tempinstance;
}
$instances[] = array('id' => $tempinstance->id, 'name' => $this->get_instance_name($tempinstance));
}
}
if (empty($instance)) {
return false;
}
$button = new enrol_user_button($this->get_manual_enrol_link($instance), get_string('enrolusers', 'enrol_manual'), 'get');
$button->class .= ' enrol_manual_plugin';
$startdate = $manager->get_course()->startdate;
$startdateoptions = array();
$timeformat = get_string('strftimedatefullshort');
if ($startdate > 0) {
$today = time();
$today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
$startdateoptions[2] = get_string('coursestart') . ' (' . userdate($startdate, $timeformat) . ')';
}
$startdateoptions[3] = get_string('today') . ' (' . userdate($today, $timeformat) . ')' ;
$modules = array('moodle-enrol_manual-quickenrolment', 'moodle-enrol_manual-quickenrolment-skin');
$arguments = array(
'instances' => $instances,
'courseid' => $instance->courseid,
'ajaxurl' => '/enrol/ajax.php',
'url' => $PAGE->url->out(false),
'optionsStartDate' => $startdateoptions,
'defaultRole' => $instance->roleid
);
$function = 'M.enrol_manual.quickenrolment.init';
$button->require_yui_module($modules, $function, array($arguments));
$button->strings_for_js(array(
'ajaxoneuserfound',
'ajaxxusersfound',
'ajaxnext25',
'enrol',
'enrolmentoptions',
'enrolusers',
'errajaxfailedenrol',
'errajaxsearch',
'none',
'usersearch',
'unlimitedduration',
'startdatetoday',
'durationdays',
'enrolperiod'), 'enrol');
$button->strings_for_js('assignroles', 'role');
$button->strings_for_js('startingfrom', 'moodle');
return $button;
}
}
/**

Before

Width:  |  Height:  |  Size: 1002 B

After

Width:  |  Height:  |  Size: 1002 B

@@ -1,4 +1,4 @@
YUI.add('moodle-enrol-enrolmentmanager', function(Y) {
YUI.add('moodle-enrol_manual-quickenrolment', function(Y) {
var UEP = {
NAME : 'Enrolment Manager',
@@ -116,7 +116,7 @@ YUI.add('moodle-enrol-enrolmentmanager', function(Y) {
);
this.set(UEP.SEARCH, this.get(UEP.BASE).one('#enrolusersearch'));
Y.all('.enrolusersbutton input').each(function(node){
Y.all('.enrol_manual_plugin input').each(function(node){
if (node.getAttribute('type', 'submit')) {
node.on('click', this.show, this);
}
@@ -506,9 +506,8 @@ YUI.add('moodle-enrol-enrolmentmanager', function(Y) {
});
Y.augment(USERENROLLER, Y.EventTarget);
M.enrol = M.enrol || {};
M.enrol.enrolmentmanager = {
M.enrol_manual = M.enrol_manual || {};
M.enrol_manual.quickenrolment = {
init : function(cfg) {
new USERENROLLER(cfg);
}
+79 -162
View File
@@ -42,30 +42,84 @@ class core_enrol_renderer extends plugin_renderer_base {
$table->initialise_javascript();
$buttons = $table->get_manual_enrol_buttons();
$buttonhtml = '';
if (count($buttons) > 0) {
$buttonhtml .= html_writer::start_tag('div', array('class' => 'enrol_user_buttons'));
foreach ($buttons as $button) {
$buttonhtml .= $this->render($button);
}
$buttonhtml .= html_writer::end_tag('div');
}
$content = '';
$enrolmentselector = $table->get_enrolment_selector();
if ($enrolmentselector) {
$content .= $this->output->render($enrolmentselector);
if (!empty($buttonhtml)) {
$content .= $buttonhtml;
}
$cohortenroller = $table->get_cohort_enrolment_control();
if ($cohortenroller) {
$content .= $this->output->render($cohortenroller);
}
$content .= $this->output->render($table->get_enrolment_type_filter());
$content .= $this->output->render($table->get_enrolment_type_filter());
$content .= $this->output->render($table->get_paging_bar());
$content .= html_writer::table($table);
$content .= $this->output->render($table->get_paging_bar());
$enrolmentselector = $table->get_enrolment_selector();
if ($enrolmentselector) {
$content .= $this->output->render($enrolmentselector);
}
$cohortenroller = $table->get_cohort_enrolment_control();
if ($cohortenroller) {
$content .= $this->output->render($cohortenroller);
if (!empty($buttonhtml)) {
$content .= $buttonhtml;
}
return $content;
}
/**
* Renderers the enrol_user_button.
*
* @param enrol_user_button $button
* @return string XHTML
*/
protected function render_enrol_user_button(enrol_user_button $button) {
$attributes = array('type' => 'submit',
'value' => $button->label,
'disabled' => $button->disabled ? 'disabled' : null,
'title' => $button->tooltip);
if ($button->actions) {
$id = html_writer::random_id('single_button');
$attributes['id'] = $id;
foreach ($button->actions as $action) {
$this->add_action_handler($action, $id);
}
}
$button->initialise_js($this->page);
// first the input element
$output = html_writer::empty_tag('input', $attributes);
// then hidden fields
$params = $button->url->params();
if ($button->method === 'post') {
$params['sesskey'] = sesskey();
}
foreach ($params as $var => $val) {
$output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $var, 'value' => $val));
}
// then div wrapper for xhtml strictness
$output = html_writer::tag('div', $output);
// now the form itself around it
if ($button->method === 'get') {
$url = $button->url->out_omit_querystring(true); // url without params, the anchor part allowed
} else {
$url = $button->url->out_omit_querystring(); // url without params, the anchor part not allowed
}
if ($url === '') {
$url = '#'; // there has to be always some action
}
$attributes = array('method' => $button->method,
'action' => $url,
'id' => $button->formid);
$output = html_writer::tag('form', $output, $attributes);
// and finally one more wrapper with class
return html_writer::tag('div', $output, array('class' => $button->class));
}
/**
* Renders a course enrolment table
*
@@ -369,6 +423,16 @@ class course_enrolment_table extends html_table implements renderable {
$this->id = html_writer::random_id();
}
/**
* Returns an array of enrol_user_buttons that are created by the different
* enrolment plugins available.
*
* @return array
*/
public function get_manual_enrol_buttons() {
return $this->manager->get_manual_enrol_buttons();
}
/**
* Gets the sort direction for a given field
*
@@ -443,9 +507,6 @@ class course_enrolment_table extends html_table implements renderable {
$this->page = $this->pages;
}
}
/**
*/
/**
* Sets the users for this table
*
@@ -564,150 +625,6 @@ class course_enrolment_users_table extends course_enrolment_table {
*/
protected static $sortablefields = array('firstname', 'lastname', 'email', 'lastaccess');
/**
* Returns a button to enrol cohorts or thier users
*
* @staticvar int $count
* @return single_button|false
*/
public function get_cohort_enrolment_control() {
static $count = 0;
// First make sure that cohorts is enabled
$plugins = $this->manager->get_enrolment_plugins();
if (!array_key_exists('cohort', $plugins)) {
return false;
}
$course = $this->manager->get_course();
if (!$plugins['cohort']->get_newinstance_link($course->id)) {
// user can not see any cohort === can not use this
return false;
}
$count ++;
$cohorturl = new moodle_url('/enrol/cohort/addinstance.php', array('id'=>$course->id));
$control = new single_button($cohorturl, get_string('enrolcohort', 'enrol'), 'get');
$control->class = 'singlebutton enrolcohortbutton instance'.$count;
$control->formid = 'manuallyenrol_single_'+$count;
if ($count == 1) {
$this->moodlepage->requires->strings_for_js(array('enrol','synced','enrolcohort','enrolcohortusers'), 'enrol');
$this->moodlepage->requires->string_for_js('assignroles', 'role');
$this->moodlepage->requires->string_for_js('cohort', 'cohort');
$this->moodlepage->requires->string_for_js('users', 'moodle');
$hasmanualinstance = false;
// No point showing this at all if the user cant manually enrol users
if (has_capability('enrol/manual:manage', $this->manager->get_context())) {
// Make sure manual enrolments instance exists
$instances = $this->manager->get_enrolment_instances();
foreach ($instances as $instance) {
if ($instance->enrol == 'manual') {
$hasmanualinstance = true;
break;
}
}
}
$modules = array('moodle-enrol-quickcohortenrolment', 'moodle-enrol-quickcohortenrolment-skin');
$function = 'M.enrol.quickcohortenrolment.init';
$arguments = array(
'courseid'=>$course->id,
'ajaxurl'=>'/enrol/ajax.php',
'url'=>$this->moodlepage->url->out(false),
'manualEnrolment'=>$hasmanualinstance);
$this->moodlepage->requires->yui_module($modules, $function, array($arguments));
}
return $control;
}
/**
* Gets the enrolment selector control for this table and initialises its
* JavaScript
*
* @return single_button|url_select
*/
public function get_enrolment_selector() {
global $CFG;
static $count = 0;
$instances = $this->manager->get_enrolment_instances();
$plugins = $this->manager->get_enrolment_plugins();
$manuals = array();
// print enrol link or selection
$links = array();
foreach($instances as $instance) {
$plugin = $plugins[$instance->enrol];
if ($link = $plugin->get_manual_enrol_link($instance)) {
$links[$instance->id] = $link;
$manuals[$instance->id] = $instance;
}
}
if (!empty($links)) {
$arguments = array();
$count ++;
if (count($links) == 1) {
$control = new single_button(reset($links), get_string('enrolusers', 'enrol_manual'), 'get');
$control->class = 'singlebutton enrolusersbutton instance'.$count;
$control->formid = 'manuallyenrol_single_'+$count;
$arguments[] = array('id'=>key($links), 'name'=>$plugins[$instances[key($links)]->enrol]->get_instance_name($instances[key($links)]));
} else if (count($links) > 1) {
$inames = $this->manager->get_enrolment_instance_names();
$options = array();
foreach ($links as $i=>$link) {
$options[$link->out(false)] = $inames[$i];
$arguments[] = array('id'=>$i, 'name'=>$plugins[$instances[$i]->enrol]->get_instance_name($instances[$i]));
}
$control = new url_select($options, '', array(''=>get_string('enrolusers', 'enrol_manual').'...'));
$control->class = 'singlebutton enrolusersbutton instance'.$count;
$control->formid = 'manuallyenrol_select_'+$count;
}
$course = $this->manager->get_course();
$timeformat = get_string('strftimedatefullshort');
$today = time();
$today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
$startdateoptions = array();
if ($course->startdate > 0) {
$startdateoptions[2] = get_string('coursestart') . ' (' . userdate($course->startdate, $timeformat) . ')';
}
$startdateoptions[3] = get_string('today') . ' (' . userdate($today, $timeformat) . ')' ;
if ($count == 1) {
$instance = reset($manuals);
$this->moodlepage->requires->strings_for_js(array(
'ajaxoneuserfound',
'ajaxxusersfound',
'ajaxnext25',
'enrol',
'enrolmentoptions',
'enrolusers',
'errajaxfailedenrol',
'errajaxsearch',
'none',
'usersearch',
'unlimitedduration',
'startdatetoday',
'durationdays',
'enrolperiod',
'finishenrollingusers',
'recovergrades'), 'enrol');
$this->moodlepage->requires->string_for_js('assignroles', 'role');
$this->moodlepage->requires->string_for_js('startingfrom', 'moodle');
$modules = array('moodle-enrol-enrolmentmanager', 'moodle-enrol-enrolmentmanager-skin');
$function = 'M.enrol.enrolmentmanager.init';
$arguments = array(
'instances'=>$arguments,
'courseid'=>$course->id,
'ajaxurl'=>'/enrol/ajax.php',
'url'=>$this->moodlepage->url->out(false),
'optionsStartDate'=>$startdateoptions,
'defaultRole'=>$instance->roleid,
'disableGradeHistory'=>$CFG->disablegradehistory);
$this->moodlepage->requires->yui_module($modules, $function, array($arguments));
}
return $control;
}
return null;
}
/**
* Gets the enrolment type filter control for this table
*
+14 -1
View File
@@ -1517,5 +1517,18 @@ abstract class enrol_plugin {
}
$rs->close();
}
}
/**
* Returns an enrol_user_button that takes the user to a page where they are able to
* enrol users into the managers course through this plugin.
*
* Optional: If the plugin supports manual enrolments it can choose to override this
* otherwise it shouldn't
*
* @param course_enrolment_manager $manager
* @return enrol_user_button|false
*/
public function get_manual_enrol_button(course_enrolment_manager $manager) {
return false;
}
}