mirror of
https://github.com/moodle/moodle.git
synced 2025-03-14 04:30:15 +01:00
MDL-57818 mod_feedback: Refactor form processing
This commit is contained in:
parent
74975948e5
commit
96ae31bddc
@ -42,6 +42,12 @@ class mod_feedback_completion extends mod_feedback_structure {
|
||||
protected $values = null;
|
||||
/** @var bool */
|
||||
protected $iscompleted = false;
|
||||
/** @var mod_feedback_complete_form the form used for completing the feedback */
|
||||
protected $form = null;
|
||||
/** @var bool true when the feedback has been completed during the request */
|
||||
protected $justcompleted = false;
|
||||
/** @var int the next page the user should jump after processing the form */
|
||||
protected $jumpto = null;
|
||||
|
||||
|
||||
/**
|
||||
@ -96,6 +102,26 @@ class mod_feedback_completion extends mod_feedback_structure {
|
||||
return $this->completed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the feedback was just completed.
|
||||
*
|
||||
* @return bool true if the feedback was just completed.
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
public function just_completed() {
|
||||
return $this->justcompleted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the jumpto property.
|
||||
*
|
||||
* @return int the next page to jump.
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
public function get_jumpto() {
|
||||
return $this->jumpto;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the temporary completion record for the current user or guest session
|
||||
*
|
||||
@ -610,4 +636,72 @@ class mod_feedback_completion extends mod_feedback_structure {
|
||||
$completion = new completion_info($course);
|
||||
$completion->set_module_viewed($this->cm);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a page jump via the mod_feedback_complete_form.
|
||||
*
|
||||
* This function initializes the form and process the submission.
|
||||
*
|
||||
* @param int $gopage the current page
|
||||
* @param int $gopreviouspage if the user chose to go to the previous page
|
||||
* @return string the url to redirect the user (if any)
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
public function process_page($gopage, $gopreviouspage = false) {
|
||||
global $CFG, $PAGE, $SESSION;
|
||||
|
||||
$urltogo = null;
|
||||
|
||||
// Save the form for later during the request.
|
||||
$this->form = new mod_feedback_complete_form(mod_feedback_complete_form::MODE_COMPLETE,
|
||||
$this, 'feedback_complete_form', array('gopage' => $gopage));
|
||||
|
||||
if ($this->form->is_cancelled()) {
|
||||
// Form was cancelled - return to the course page.
|
||||
$urltogo = course_get_url($this->courseid);
|
||||
} else if ($this->form->is_submitted() &&
|
||||
($this->form->is_validated() || $gopreviouspage)) {
|
||||
// Form was submitted (skip validation for "Previous page" button).
|
||||
$data = $this->form->get_submitted_data();
|
||||
if (!isset($SESSION->feedback->is_started) OR !$SESSION->feedback->is_started == true) {
|
||||
print_error('error', '', $CFG->wwwroot.'/course/view.php?id='.$this->courseid);
|
||||
}
|
||||
$this->save_response_tmp($data);
|
||||
if (!empty($data->savevalues) || !empty($data->gonextpage)) {
|
||||
if (($nextpage = $this->get_next_page($gopage)) !== null) {
|
||||
if ($PAGE->has_set_url()) {
|
||||
$urltogo = new moodle_url($PAGE->url, array('gopage' => $nextpage));
|
||||
}
|
||||
$this->jumpto = $nextpage;
|
||||
} else {
|
||||
$this->save_response();
|
||||
if (!$this->feedback->page_after_submit) {
|
||||
\core\notification::success(get_string('entries_saved', 'feedback'));
|
||||
}
|
||||
$this->justcompleted = true;
|
||||
}
|
||||
} else if (!empty($gopreviouspage)) {
|
||||
$prevpage = intval($this->get_previous_page($gopage));
|
||||
if ($PAGE->has_set_url()) {
|
||||
$urltogo = new moodle_url($PAGE->url, array('gopage' => $prevpage));
|
||||
}
|
||||
$this->jumpto = $prevpage;
|
||||
}
|
||||
}
|
||||
return $urltogo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the form with the questions.
|
||||
*
|
||||
* @return string the form rendered
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
public function render_items() {
|
||||
global $SESSION;
|
||||
|
||||
// Print the items.
|
||||
$SESSION->feedback->is_started = true;
|
||||
return $this->form->render();
|
||||
}
|
||||
}
|
@ -30,6 +30,7 @@ feedback_init_feedback_session();
|
||||
$id = required_param('id', PARAM_INT);
|
||||
$courseid = optional_param('courseid', null, PARAM_INT);
|
||||
$gopage = optional_param('gopage', 0, PARAM_INT);
|
||||
$gopreviouspage = optional_param('gopreviouspage', null, PARAM_RAW);
|
||||
|
||||
list($course, $cm) = get_course_and_cm_from_cmid($id, 'feedback');
|
||||
$feedback = $DB->get_record("feedback", array("id" => $cm->instance), '*', MUST_EXIST);
|
||||
@ -90,32 +91,11 @@ $cansubmit = $feedbackcompletion->can_submit();
|
||||
|
||||
// Initialise the form processing feedback completion.
|
||||
if (!$feedbackcompletion->is_empty() && $cansubmit) {
|
||||
$form = new mod_feedback_complete_form(mod_feedback_complete_form::MODE_COMPLETE,
|
||||
$feedbackcompletion, 'feedback_complete_form', array('gopage' => $gopage));
|
||||
if ($form->is_cancelled()) {
|
||||
// Form was cancelled - return to the course page.
|
||||
redirect(course_get_url($courseid ?: $course));
|
||||
} else if ($form->is_submitted() &&
|
||||
($form->is_validated() || optional_param('gopreviouspage', null, PARAM_RAW))) {
|
||||
// Form was submitted (skip validation for "Previous page" button).
|
||||
$data = $form->get_submitted_data();
|
||||
if (!isset($SESSION->feedback->is_started) OR !$SESSION->feedback->is_started == true) {
|
||||
print_error('error', '', $CFG->wwwroot.'/course/view.php?id='.$course->id);
|
||||
}
|
||||
$feedbackcompletion->save_response_tmp($data);
|
||||
if (!empty($data->savevalues) || !empty($data->gonextpage)) {
|
||||
if (($nextpage = $feedbackcompletion->get_next_page($gopage)) !== null) {
|
||||
redirect(new moodle_url($PAGE->url, array('gopage' => $nextpage)));
|
||||
} else {
|
||||
$feedbackcompletion->save_response();
|
||||
if (!$feedback->page_after_submit) {
|
||||
\core\notification::success(get_string('entries_saved', 'feedback'));
|
||||
}
|
||||
}
|
||||
} else if (!empty($data->gopreviouspage)) {
|
||||
$prevpage = $feedbackcompletion->get_previous_page($gopage);
|
||||
redirect(new moodle_url($PAGE->url, array('gopage' => intval($prevpage))));
|
||||
}
|
||||
// Process the page via the form.
|
||||
$urltogo = $feedbackcompletion->process_page($gopage, $gopreviouspage);
|
||||
|
||||
if ($urltogo !== null) {
|
||||
redirect($urltogo);
|
||||
}
|
||||
}
|
||||
|
||||
@ -129,7 +109,7 @@ echo $OUTPUT->heading(format_string($feedback->name));
|
||||
if ($feedbackcompletion->is_empty()) {
|
||||
\core\notification::error(get_string('no_items_available_yet', 'feedback'));
|
||||
} else if ($cansubmit) {
|
||||
if (!empty($data->savevalues) || !empty($data->gonextpage)) {
|
||||
if ($feedbackcompletion->just_completed()) {
|
||||
// Display information after the submit.
|
||||
if ($feedback->page_after_submit) {
|
||||
echo $OUTPUT->box($feedbackcompletion->page_after_submit(),
|
||||
@ -149,9 +129,8 @@ if ($feedbackcompletion->is_empty()) {
|
||||
}
|
||||
echo $OUTPUT->continue_button($url);
|
||||
} else {
|
||||
// Print the items.
|
||||
$SESSION->feedback->is_started = true;
|
||||
$form->display();
|
||||
// Display the form with the questions.
|
||||
echo $feedbackcompletion->render_items();
|
||||
}
|
||||
} else {
|
||||
echo $OUTPUT->box_start('generalbox boxaligncenter');
|
||||
|
Loading…
x
Reference in New Issue
Block a user