MDL-46907 gradereport_history: Refactor user_button class to remove js methods

Part of MDL-46191
This commit is contained in:
Ankit Agarwal 2014-08-21 10:25:29 +08:00
parent 9e7a84e5b2
commit 5bd36407af
4 changed files with 26 additions and 110 deletions

View File

@ -37,29 +37,18 @@ defined('MOODLE_INTERNAL') || die;
class helper {
/**
* Get an instance of the user select button {@link gradereport_history_user_button}.
* Initialise the js to handle the user selection {@link gradereport_history_user_button}.
*
* @param int $courseid course id.
* @param array $currentusers List of currently selected users.
*
* @return output\user_button the user select button.
*/
public static function get_user_select_button($courseid, array $currentusers = null) {
public static function init_js($courseid, array $currentusers = null) {
global $PAGE;
$button = new output\user_button($PAGE->url, get_string('selectusers', 'gradereport_history'), 'get');
$button->class .= ' gradereport_history_plugin';
$module = array('moodle-gradereport_history-userselector');
$arguments = array(
'courseid' => $courseid,
'ajaxurl' => '/grade/report/history/users_ajax.php',
'url' => $PAGE->url->out(false),
'selectedUsers' => $currentusers,
);
$jsfunction = 'Y.M.gradereport_history.UserSelector.init';
$button->require_yui_module($module, $jsfunction, array($arguments));
$button->strings_for_js(array(
// Load the strings for js.
$PAGE->requires->strings_for_js(array(
'errajaxsearch',
'finishselectingusers',
'foundoneuser',
@ -67,15 +56,27 @@ class helper {
'loadmoreusers',
'selectusers',
), 'gradereport_history');
$button->strings_for_js(array(
$PAGE->requires->strings_for_js(array(
'loading'
), 'admin');
$button->strings_for_js(array(
$PAGE->requires->strings_for_js(array(
'noresults',
'search'
));
), 'moodle');
return $button;
$arguments = array(
'courseid' => $courseid,
'ajaxurl' => '/grade/report/history/users_ajax.php',
'url' => $PAGE->url->out(false),
'selectedUsers' => $currentusers,
);
// Load the yui module.
$PAGE->requires->yui_module(
'moodle-gradereport_history-userselector',
'Y.M.gradereport_history.UserSelector.init',
array($arguments)
);
}
/**

View File

@ -59,8 +59,6 @@ class renderer extends \plugin_renderer_base {
$this->add_action_handler($action, $id);
}
}
$button->initialise_js($this->page);
// First the input element.
$output = \html_writer::empty_tag('input', $attributes);

View File

@ -37,25 +37,6 @@ defined('MOODLE_INTERNAL') || die;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_button extends \single_button implements \renderable {
/**
* 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 select_user_button.
*
@ -65,75 +46,7 @@ class user_button extends \single_button implements \renderable {
*/
public function __construct(\moodle_url $url, $label, $method = 'post') {
parent::__construct($url, $label, $method);
$this->class = 'singlebutton selectusersbutton';
$this->class = 'singlebutton selectusersbutton gradereport_history_plugin';
$this->formid = \html_writer::random_id('selectusersbutton');
}
/**
* 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 $jsfunction 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, $jsfunction, array $arguments = null, $galleryversion = null, $ondomready = false) {
$js = new \stdClass;
$js->modules = (array)$modules;
$js->function = $jsfunction;
$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 $jsfunction 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($jsfunction, array $extraarguments = null, $ondomready = false, array $module = null) {
$js = new \stdClass;
$js->function = $jsfunction;
$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 array|\stdClass $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);
}
}
}

View File

@ -83,8 +83,12 @@ foreach ($table->get_selected_users() as $key => $user) {
}
$filters['userfullnames'] = implode(',', $names);
// Set up js.
\gradereport_history\helper::init_js($course->id, $names);
// Now that we have the names, reinitialise the button so its able to control them.
$button = \gradereport_history\helper::get_user_select_button($course->id, $names);
$button = new \gradereport_history\output\user_button($PAGE->url, get_string('selectusers', 'gradereport_history'), 'get');
$userbutton = $output->render($button);
$params = array('course' => $course, 'itemids' => $itemids, 'graders' => $graders, 'userbutton' => $userbutton);
$mform = new \gradereport_history\filter_form(null, $params);