if (!data_submitted() or empty($reallysure)) {
$optionsyes = array('sure'=>'yes', 'sesskey'=>sesskey(), 'reallysure'=>'yes');
$formcontinue = new single_button(new moodle_url('delete.php', $optionsyes), get_string('yes'));
- $formcancel = new signle_button('index.php', get_string('no'), 'get');
+ $formcancel = new single_button('index.php', get_string('no'), 'get');
echo $OUTPUT->confirm('Are you REALLY REALLY completely sure you want to delete everything inside the directory '.
$deletedir .' (this includes all user images, and any other course files that have been created) ?',
$formcontinue, $formcancel);
$optionsyes = array('eid'=>$eid, 'confirm'=>1, 'sesskey'=>sesskey(), 'id'=>$course->id, 'action'=>'delete');
$optionsno = array('id'=>$course->id);
$formcontinue = new single_button(new moodle_url('index.php', $optionsyes), get_string('yes'));
- $formcancel = new signle_button(new moodle_url('index.php', $optionsno), get_string('no'), 'get');
+ $formcancel = new single_button(new moodle_url('index.php', $optionsno), get_string('no'), 'get');
echo $OUTPUT->confirm($strdeletecheckfull, $formcontinue, $formcancel);
echo $OUTPUT->footer();
die;
}
+/**
+ * Data structure representing a simple form with only one button.
+ *
+ * @copyright 2009 Petr Skoda
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since Moodle 2.0
+ */
+class single_button implements renderable {
+ /** Target url */
+ var $url;
+ /** Button label */
+ var $label;
+ /** Form submit method */
+ var $method = 'post';
+ /** Form class */
+ var $class = 'singlebutton';
+ /** True if button disabled, false if normal */
+ var $disabled = false;
+ /** Button tooltip */
+ var $tooltip = '';
+ /** Form id */
+ var $formid;
+ /** List of attached actions */
+ var $actions = array();
+
+ /**
+ * Constructor
+ * @param string|moodle_url
+ * @param string $label button text
+ * @param string $method get or post submit method
+ * @param array $options associative array form attributes + {disabled, title}
+ */
+ public function __construct(moodle_url $url, $label, $method='post') {
+ $this->url = clone($url);
+ $this->label = $label;
+ $this->method = $method;
+ }
+
+ /**
+ * Shortcut for adding a JS confirm dialog when the component is clicked.
+ * The message must be a yes/no question.
+ * @param string $message The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
+ * @return void
+ */
+ public function add_confirm_action($confirmmessage) {
+ $this->add_action(new component_action('click', 'confirm_dialog', array('message' => $confirmmessage)));
+ }
+
+ public function add_action(component_action $action) {
+ $this->actions[] = $action;
+ }
+}
+
+
+
// ==== HTML writer and helper classes, will be probably moved elsewhere ======
parent::prepare($output, $page, $target);
}
-
- public static function make_button($url, array $params=null, $label=null, $method='post', array $formoptions=null) {
- //TODO: to be removed soon, repalced by ew single_button()
- // in any case the $params argument is not appropriate here, we use moodle_urls now!
- $form = new html_form($formoptions);
- $form->url = new moodle_url($url, $params);
- if ($label !== null) {
- $form->button->text = $label;
- }
- $form->method = $method;
-
- return $form;
- }
-}
-
-
-/**
- * A component representing a simple form with only one button.
- *
- * @copyright 2009 Petr Skoda
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- * @since Moodle 2.0
- */
-class single_button extends html_form {
- /**
- * Constructor
- * @param string|moodle_url
- * @param string $label button text
- * @param string $method get or post submit method
- * @param array $options associative array form attributes + {disabled, title}
- */
- public function __construct($url, $label, $method='post', array $options=null) {
- parent::__construct($options);
- $this->url = $url;
- $form->method = $method;
- $this->button->text = $label;
- if (!empty($options['disabled'])) {
- $this->button->disabled = true;
- }
- if (!empty($options['title'])) {
- $this->button->title = $options['title'];
- }
- }
}
* If a string or moodle_url is given instead of a html_button, method defaults to post.
*
* @param string $message The question to ask the user
- * @param html_form|moodle_url|string $continue The html_form component representing the Continue answer. Can also be a moodle_url or string URL
- * @param html_form|moodle_url|string $cancel The html_form component representing the Cancel answer. Can also be a moodle_url or string URL
+ * @param single_button|moodle_url|string $continue The single_button component representing the Continue answer. Can also be a moodle_url or string URL
+ * @param single_button|moodle_url|string $cancel The single_button component representing the Cancel answer. Can also be a moodle_url or string URL
* @return string HTML fragment
*/
public function confirm($message, $continue, $cancel) {
- if ($continue instanceof html_form) { //TODO: change to single_button
- $continue = clone($continue);
- } else if (is_string($continue) or $continue instanceof moodle_url) {
+ if (is_string($continue) or $continue instanceof moodle_url) {
$continue = new single_button($continue, get_string('continue'), 'post');
} else {
throw new coding_exception('The continue param to $OUTPUT->confirm() must be either a URL (string/moodle_url) or a html_form instance.');
}
- if ($cancel instanceof html_form) { //TODO: change to single_button
- $cancel = clone($cancel);
- } else if (is_string($cancel) or $cancel instanceof moodle_url) {
+ if (is_string($cancel) or $cancel instanceof moodle_url) {
$cancel = new single_button($cancel, get_string('cancel'), 'get');
} else {
throw new coding_exception('The cancel param to $OUTPUT->confirm() must be either a URL (string/moodle_url) or a html_form instance.');
$output = $this->box_start('generalbox', 'notice');
$output .= html_writer::tag('p', array(), $message);
- $output .= html_writer::tag('div', array('class' => 'buttons'), $this->button($continue) . $this->button($cancel));
+ $output .= html_writer::tag('div', array('class' => 'buttons'), $this->render($continue) . $this->render($cancel));
$output .= $this->box_end();
return $output;
}
/**
- * Returns a form with single button.
- * If first parameter is html_form instance all other parameters are ignored.
+ * Returns a form with a single button.
*
- * @param string|moodle_url|single_button $url_or_singlebutton
+ * @param string|moodle_url $url
* @param string $label button text
* @param string $method get or post submit method
- * @param array $options associative array {disabled, title}
+ * @param array $options associative array {disabled, title, etc.}
* @return string HTML fragment
*/
- public function single_button($url_or_singlebutton, $label=null, $method='post', array $options=null) {
- if ($url_or_singlebutton instanceof single_button) {
- $button = $url_or_singlebutton;
- if (func_num_args() > 1) {
- debugging('html_form instance used as first parameter of $OUTPUT->single_button(), all other parameters are ignored.');
- }
- } else if ($url_or_singlebutton instanceof moodle_url or is_string($url_or_singlebutton)) {
- $button = new single_button($url_or_singlebutton, $label, $method, $options);
+ public function single_button($url, $label, $method='post', array $options=null) {
+ if ($url instanceof moodle_url) {
+ $button = new single_button($url, $label, $method);
+ } else if (is_string($url_or_singlebutton)) {
+ $button = new single_button(new moodle_url($url), $label, $method);
} else {
- throw new coding_exception('The $$url_or_singlebutton param to $OUTPUT->single_button() must be either a URL (string/moodle_url) or a single_button instance.');
+ throw new coding_exception('The $url param to $OUTPUT->single_button() must be either a string or moodle_url.');
+ }
+ foreach ((array)$options as $key=>$value) {
+ if (array_key_exists($key, $button)) {
+ $button->$key = $value;
+ }
}
- return $this->button($button);
+ return $this->render($button);
}
/**
- * Given a html_form object, outputs an <input> tag within a form that uses the object's attributes.
- *
- * @param html_form $form A html_form object
+ * Internal implementation of single_button rendering
+ * @param single_button $button
* @return string HTML fragment
*/
- public function button(html_form $form) {
- if (empty($form->button) or !($form->button instanceof html_button)) {
- throw new coding_exception('$OUTPUT->button($form) requires $form to have a button (html_button) value');
+ protected function render_single_button(single_button $button) {
+ $attributes = array('type' => 'submit',
+ 'value' => $button->label,
+ 'disabled' => $button->disabled,
+ '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($id, $action);
+ }
}
- $form = clone($form);
- $form->button->prepare($this, $this->page, $this->target);
- $this->prepare_event_handlers($form->button);
+ // first the input element
+ $output = html_writer::empty_tag('input', $attributes);
- $buttonattributes = array('class' => $form->button->get_classes_string(),
- 'type' => 'submit',
- 'value' => $form->button->text,
- 'disabled' => $form->button->disabled,
- 'id' => $form->button->id);
+ // 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));
+ }
- $buttonoutput = html_writer::empty_tag('input', $buttonattributes);
+ // then div wrapper for xhtml strictness
+ $output = html_writer::tag('div', array(), $output);
- // Removing the button so it doesn't get output again
- unset($form->button);
+ // now the form itself around it
+ $attributes = array('method' => $button->method,
+ 'action' => $button->url->out(true), // url without params
+ 'id' => $button->formid);
+ $output = html_writer::tag('form', $attributes, $output);
- return html_writer::tag('div', array('class' => 'singlebutton'), $this->form($form, $buttonoutput));
+ // and finally one more wrapper with class
+ return html_writer::tag('div', array('class' => $button->class), $output);
}
/**
if (has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_MODULE, $cmid))) {
$modulename = get_string('modulename', $modulename);
$string = get_string('updatethis', '', $modulename);
-
- $form = new html_form();
- $form->url = new moodle_url("$CFG->wwwroot/course/mod.php", array('update' => $cmid, 'return' => true, 'sesskey' => sesskey()));
- $form->button->text = $string;
- return $this->button($form);
+ $url = new moodle_url("$CFG->wwwroot/course/mod.php", array('update' => $cmid, 'return' => true, 'sesskey' => sesskey()));
+ return $this->single_button($url, $string);
} else {
return '';
}
$edit = '1';
}
- $form = new html_form();
- $form->url = $url;
- $form->url->param('edit', $edit);
- $form->button->text = $string;
+ $url = new moodle_url($url, array('edit'=>$edit));
- return $this->button($form);
+ return $this->single_button($url, $string);
}
/**
/**
* Prints a simple button to close a window
*
- * @global objec)t
* @param string $text The lang string for the button's label (already output from get_string())
- * @return string|void if $return is true, void otherwise
+ * @return string html fragment
*/
public function close_window_button($text='') {
if (empty($text)) {
$text = get_string('closewindow');
}
- $closeform = new html_form();
- $closeform->url = '#';
- $closeform->method = 'get';
- $closeform->button->text = $text;
- $closeform->button->add_action('click', 'close_window');
- $closeform->button->prepare($this, $this->page, $this->target);
- return $this->container($this->button($closeform), 'closewindow');
+ $button = new single_button($this->page->url.'#', $text, 'get');
+ $button->add_action('click', 'close_window');
+
+ return $this->container($this->render($button), 'closewindow');
}
/**
/**
* Print a continue button that goes to a particular URL.
*
- * @param string|moodle_url $link The url the button goes to.
+ * @param string|moodle_url $url The url the button goes to.
* @return string the HTML to output.
*/
- public function continue_button($link) {
- if (!is_a($link, 'moodle_url')) {
- $link = new moodle_url($link);
+ public function continue_button($url) {
+ if (!($url instanceof moodle_url)) {
+ $url = new moodle_url($url);
}
- $form = new html_form();
- $form->url = $link;
- $form->values = $link->params();
- $form->button->text = get_string('continue');
- $form->method = 'get';
+ $button = new single_button($url, get_string('continue'), 'get');
+ $button->class = 'continuebutton';
- return html_writer::tag('div', array('class' => 'continuebutton') , $this->button($form));
+ return $this->render($button);
}
/**
$params['pageid'] = $this->properties->id;
$params['sesskey'] = sesskey();
$params['jumpto'] = $answer->jumpto;
- $buttons[] = $renderer->button(new single_button(new moodle_url($CFG->wwwroot.'/mod/lesson/continue.php', $params), strip_tags(format_text($answer->answer, FORMAT_MOODLE, $options))));
+ $url = new moodle_url($CFG->wwwroot.'/mod/lesson/continue.php', $params);
+ $buttons[] = $renderer->single_button($url, strip_tags(format_text($answer->answer, FORMAT_MOODLE, $options)));
$i++;
}
// Set the orientation
* @param html_form $button
* @return string
*/
- public function message($message, html_form $button = null) {
+ public function message($message, single_button $button = null) {
$output = $this->output->box_start('generalbox boxaligncenter');
$output .= $message;
if ($button !== null) {
- $output .= $this->output->box($this->output->button($button),'lessonbutton standardbutton');
+ $output .= $this->output->box($this->output->render($button), 'lessonbutton standardbutton');
}
$output .= $this->output->box_end();
return $output;
if ($this->securewindow_required($canpreview)) {
$this->_securewindowrule->print_start_attempt_button($buttontext, $strconfirmstartattempt);
} else {
- $form = new single_button(new moodle_url($this->_quizobj->start_attempt_url(), array('cmid' => $this->_quizobj->get_cmid())), $buttontext);
+ $button = new single_button(new moodle_url($this->_quizobj->start_attempt_url(), array('cmid' => $this->_quizobj->get_cmid())), $buttontext);
if ($strconfirmstartattempt) {
- $form->button->add_confirm_action($strconfirmstartattempt);
+ $button->add_confirm_action($strconfirmstartattempt);
}
- echo $OUTPUT->single_button($form);
+ echo $OUTPUT->render($button);
}
echo "</div>\n";
}
*/
public function make_review_link($linktext, $attemptid) {
global $OUTPUT;
- $form = new single_button($this->_quizobj->review_url($attemptid), $linktext);
- $form->button->add_action(new popup_action('click', $form->url, 'quizpopup', $this->windowoptions));
- return $OUTPUT->single_button($form);
+ $button = new single_button($this->_quizobj->review_url($attemptid), $linktext);
+ $button->add_action(new popup_action('click', $form->url, 'quizpopup', $this->windowoptions));
+ return $OUTPUT->render($button);
}
/**
'sesskey' => sesskey(),
);
-$form = new single_button(new moodle_url($attemptobj->processattempt_url(), $options), get_string('finishattempt', 'quiz'));
-$form->id = 'responseform';
-$form->button->add_confirm_action(get_string('confirmclose', 'quiz'));
+$button = new single_button(new moodle_url($attemptobj->processattempt_url(), $options), get_string('finishattempt', 'quiz'));
+$button->id = 'responseform';
+$button->add_confirm_action(get_string('confirmclose', 'quiz'));
-echo $OUTPUT->single_button($form);
+echo $OUTPUT->render($button);
echo $OUTPUT->container_end();
/// Finish the page
break;
}
if ($m[0] == self::MSG_CONFIRM_DEL) {
- $form = new html_form();
- $form->url = new moodle_url($PAGE->url, array('mode' => 'del', 'what' => $m[1], 'confirm' => 1));
- $form->button = new html_button();
- $form->button->text = get_string('iamsure', 'workshop');
- $form->method = 'post';
- $msg->extra = $OUTPUT->button($form);
+ $aurl = new moodle_url($PAGE->url, array('mode' => 'del', 'what' => $m[1], 'confirm' => 1));
+ $msg->extra = $OUTPUT->single_button($url, get_string('iamsure', 'workshop'), 'post');
}
}
$mformassessment->display();
echo $OUTPUT->container_start('buttonsbar');
if ($isreviewer and $workshop->assessing_examples_allowed()) {
- $button = new html_form();
- $button->method = 'get';
- $button->button->text = get_string('reassess', 'workshop');
- $button->url = new moodle_url($workshop->exsubmission_url($example->id), array('assess' => 'on', 'sesskey' => sesskey()));
- echo $OUTPUT->button($button);
+ $aurl = new moodle_url($workshop->exsubmission_url($example->id), array('assess' => 'on', 'sesskey' => sesskey()));
+ echo $OUTPUT->single_button($aurl, get_string('reassess', 'workshop'), 'get');
}
echo $OUTPUT->container_end(); // buttonsbar
echo $OUTPUT->container_start('buttonsbar');
if ($canmanage) {
if (empty($edit) and empty($delete)) {
- $button = new html_form();
- $button->method = 'get';
- $button->button->text = get_string('exampleedit', 'workshop');
- $button->url = new moodle_url($workshop->exsubmission_url($example->id), array('edit' => 'on'));
- echo $OUTPUT->button($button);
-
- $button = new html_form();
- $button->method = 'get';
- $button->button->text = get_string('exampledelete', 'workshop');
- $button->url = new moodle_url($workshop->exsubmission_url($example->id), array('delete' => 'on'));
- echo $OUTPUT->button($button);
+ $aurl = new moodle_url($workshop->exsubmission_url($example->id), array('edit' => 'on'));
+ echo $OUTPUT->single_button($aurl, get_string('exampleedit', 'workshop'), 'get');
+
+ $aurl = new moodle_url($workshop->exsubmission_url($example->id), array('delete' => 'on'));
+ echo $OUTPUT->single_button($aurl, get_string('exampledelete', 'workshop'), 'get');
}
}
// ...and optionally assess it
if ($canassess or ($canmanage and empty($edit) and empty($delete))) {
- $button = new html_form();
- $button->method = 'get';
- $button->button->text = get_string('exampleassess', 'workshop');
- $button->url = new moodle_url($workshop->exsubmission_url($example->id), array('assess' => 'on', 'sesskey' => sesskey()));
- echo $OUTPUT->button($button);
+ $aurl = new moodle_url($workshop->exsubmission_url($example->id), array('assess' => 'on', 'sesskey' => sesskey()));
+ echo $OUTPUT->single_button($aurl, get_string('exampleassess', 'workshop'), 'get');
}
echo $OUTPUT->container_end(); // buttonsbar
// and possibly display the example's review(s) - todo
$summary->gradeinfo->received = $this->real_grade($example->grade);
$summary->gradeinfo->max = $this->real_grade(100);
- $summary->btnform = new html_form();
- $summary->btnform->method = 'get';
- $summary->btnform->button->text = $buttontext;
- $summary->btnform->url = new moodle_url($this->exsubmission_url($example->id), array('assess' => 'on', 'sesskey' => sesskey()));
+ $aurl = new moodle_url($this->exsubmission_url($example->id), array('assess' => 'on', 'sesskey' => sesskey()));
+ $summary->btnform = new single_button($aurl, $buttontext, 'get');
return $summary;
}
}
// button to assess
- $o .= $this->output->container($this->output->button($summary->btnform), 'example-actions');
+ $o .= $this->output->container($this->output->render($summary->btnform), 'example-actions');
// end of wrapping box
$o .= $this->output->box_end();
}
if ($ownsubmission and $editable) {
- $editbutton = new html_form();
- $editbutton->method = 'get';
- $editbutton->button->text = get_string('editsubmission', 'workshop');
- $editbutton->url = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
- echo $OUTPUT->button($editbutton);
+ $url = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
+ echo $OUTPUT->sinle_button($url, get_string('editsubmission', 'workshop'), 'get');
}
// and possibly display the submission's review(s)
$summary = $workshop->prepare_example_summary($example);
echo $wsoutput->example_summary($summary);
}
- $editbutton = new html_form();
- $editbutton->method = 'get';
- $editbutton->button->text = get_string('exampleadd', 'workshop');
- $editbutton->url = new moodle_url($workshop->exsubmission_url(0), array('edit' => 'on'));
- echo $OUTPUT->button($editbutton);
+ $aurl = new moodle_url($workshop->exsubmission_url(0), array('edit' => 'on'));
+ echo $OUTPUT->single_button($aurl, get_string('exampleadd', 'workshop'), 'get');
} else {
echo $OUTPUT->container(get_string('noexamplesformready', 'workshop'));
}
echo $OUTPUT->container(get_string('noyoursubmission', 'workshop'));
}
if ($workshop->submitting_allowed()) {
- $editbutton = new html_form();
- $editbutton->method = 'get';
- $editbutton->button->text = get_string('editsubmission', 'workshop');
- $editbutton->url = new moodle_url($workshop->submission_url(), array('edit' => 'on'));
- echo $OUTPUT->button($editbutton);
+ $aurl = new moodle_url($workshop->submission_url(), array('edit' => 'on'));
+ echo $OUTPUT->single_button($aurl, get_string('editsubmission', 'workshop'), 'get');
}
echo $OUTPUT->box_end();
print_collapsible_region_end();
}
echo $OUTPUT->box_start('generalbox assessment-summary' . $class);
echo $wsoutput->submission_summary($submission, $shownames);
- $button = new html_form();
- $button->method = 'get';
- $button->button->text = $buttontext;
- $button->url = $workshop->assess_url($assessment->id);
- echo $OUTPUT->button($button);
+ $aurl = $workshop->assess_url($assessment->id);
+ echo $OUTPUT->single_button($aurl, $buttontext, 'get');
echo $OUTPUT->box_end();
}
}
$exporter->print_header('confirmcancel');
echo $OUTPUT->box_start();
$yesbutton = new single_button(new moodle_url($CFG->wwwroot . '/portfolio/add.php', array('id' => $dataid, 'cancel' => 1, 'cancelsure' => 1, 'logreturn' => $logreturn)). get_string('yes'));
- $nobutton = new single_button(new moodle_url($CFG->wwwroot . '/portfolio/add.php', array('id' => $dataid)), get_string('no'));
if ($logreturn) {
- $nobutton->url = $CFG->wwwroot . '/user/portfoliologs.php';
+ $nobutton = new single_button(new moodle_url($CFG->wwwroot . '/user/portfoliologs.php', array('id' => $dataid)), get_string('no'));
+ } else {
+ $nobutton = new single_button(new moodle_url($CFG->wwwroot . '/portfolio/add.php', array('id' => $dataid)), get_string('no'));
}
echo $OUTPUT->confirm(get_string('confirmcancel', 'portfolio'), $yesbutton, $nobutton);
echo $OUTPUT->box_end();