MDL-42531 assign: refactor batch operations to use forms properly

Avoids a lot of ugly optional_param() use. Also fixes the
non-functional cancel buttons.
This commit is contained in:
Jonathon Fowler 2014-05-07 16:11:23 +10:00
parent f8fa440649
commit c462c35b95
5 changed files with 150 additions and 103 deletions

View File

@ -42,17 +42,17 @@ class mod_assign_batch_set_allocatedmarker_form extends moodleform {
$mform = $this->_form;
$params = $this->_customdata;
$mform->addElement('header', 'general', get_string('batchsetallocatedmarker', 'assign', count($params['users'])));
$mform->addElement('header', 'general', get_string('batchsetallocatedmarker', 'assign', $params['userscount']));
$mform->addElement('static', 'userslist', get_string('selectedusers', 'assign'), $params['usershtml']);
$options = $params['markers'];
$mform->addElement('select', 'allocatedmarker', get_string('allocatedmarker', 'assign'), $options);
$mform->addElement('hidden', 'id', $params['cm']);
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'action', 'setbatchmarkingallocation');
$mform->setType('action', PARAM_ALPHA);
$mform->addElement('hidden', 'selectedusers', implode(',', $params['users']));
$mform->addElement('hidden', 'selectedusers');
$mform->setType('selectedusers', PARAM_SEQUENCE);
$this->add_action_buttons(true, get_string('savechanges'));

View File

@ -41,7 +41,7 @@ class mod_assign_batch_set_marking_workflow_state_form extends moodleform {
public function definition() {
$mform = $this->_form;
$params = $this->_customdata;
$formheader = get_string('batchsetmarkingworkflowstateforusers', 'assign', count($params['users']));
$formheader = get_string('batchsetmarkingworkflowstateforusers', 'assign', $params['userscount']);
$mform->addElement('header', 'general', $formheader);
$mform->addElement('static', 'userslist', get_string('selectedusers', 'assign'), $params['usershtml']);
@ -49,11 +49,11 @@ class mod_assign_batch_set_marking_workflow_state_form extends moodleform {
$options = $params['markingworkflowstates'];
$mform->addElement('select', 'markingworkflowstate', get_string('markingworkflowstate', 'assign'), $options);
$mform->addElement('hidden', 'id', $params['cm']);
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'action', 'setbatchmarkingworkflowstate');
$mform->setType('action', PARAM_ALPHA);
$mform->addElement('hidden', 'selectedusers', implode(',', $params['users']));
$mform->addElement('hidden', 'selectedusers');
$mform->setType('selectedusers', PARAM_SEQUENCE);
$this->add_action_buttons(true, get_string('savechanges'));

View File

@ -3493,9 +3493,8 @@ class assign {
$users = $submitteddata->selectedusers;
$userlist = explode(',', $users);
$formparams = array('cm'=>$this->get_course_module()->id,
'users'=>$userlist,
'context'=>$this->get_context());
$formdata = array('id' => $this->get_course_module()->id,
'selectedusers' => $users);
$usershtml = '';
@ -3519,10 +3518,14 @@ class assign {
$usercount += 1;
}
$formparams['usershtml'] = $usershtml;
$formparams['markingworkflowstates'] = $this->get_marking_workflow_states_for_current_user();
$formparams = array(
'userscount' => count($userlist),
'usershtml' => $usershtml,
'markingworkflowstates' => $this->get_marking_workflow_states_for_current_user()
);
$mform = new mod_assign_batch_set_marking_workflow_state_form(null, $formparams);
$mform->set_data($formdata); // Initialises the hidden elements.
$o .= $this->get_renderer()->header();
$o .= $this->get_renderer()->render(new assign_form('setworkflowstate', $mform));
$o .= $this->view_footer();
@ -3549,9 +3552,8 @@ class assign {
$users = $submitteddata->selectedusers;
$userlist = explode(',', $users);
$formparams = array('cm'=>$this->get_course_module()->id,
'users'=>$userlist,
'context'=>$this->get_context());
$formdata = array('id' => $this->get_course_module()->id,
'selectedusers' => $users);
$usershtml = '';
@ -3575,7 +3577,11 @@ class assign {
$usercount += 1;
}
$formparams['usershtml'] = $usershtml;
$formparams = array(
'userscount' => count($userlist),
'usershtml' => $usershtml,
);
$markers = get_users_by_capability($this->get_context(), 'mod/assign:grade');
$markerlist = array();
foreach ($markers as $marker) {
@ -3585,6 +3591,7 @@ class assign {
$formparams['markers'] = $markerlist;
$mform = new mod_assign_batch_set_allocatedmarker_form(null, $formparams);
$mform->set_data($formdata); // Initialises the hidden elements.
$o .= $this->get_renderer()->header();
$o .= $this->get_renderer()->render(new assign_form('setworkflowstate', $mform));
$o .= $this->view_footer();
@ -6114,34 +6121,48 @@ class assign {
* @return void
*/
protected function process_set_batch_marking_workflow_state() {
global $DB;
global $CFG, $DB;
require_sesskey();
// Include batch marking workflow form.
require_once($CFG->dirroot . '/mod/assign/batchsetmarkingworkflowstateform.php');
$batchusers = required_param('selectedusers', PARAM_TEXT);
$state = required_param('markingworkflowstate', PARAM_ALPHA);
$useridlist = explode(',', $batchusers);
$formparams = array(
'userscount' => 0, // This form is never re-displayed, so we don't need to
'usershtml' => '', // initialise these parameters with real information.
'markingworkflowstates' => $this->get_marking_workflow_states_for_current_user()
);
foreach ($useridlist as $userid) {
$flags = $this->get_user_flags($userid, true);
$mform = new mod_assign_batch_set_marking_workflow_state_form(null, $formparams);
$flags->workflowstate = $state;
if ($mform->is_cancelled()) {
return true;
}
$gradingdisabled = $this->grading_disabled($userid);
if ($formdata = $mform->get_data()) {
$useridlist = explode(',', $formdata->selectedusers);
$state = $formdata->markingworkflowstate;
// Will not apply update if user does not have permission to assign this workflow state.
if (!$gradingdisabled && $this->update_user_flags($flags)) {
if ($state == ASSIGN_MARKING_WORKFLOW_STATE_RELEASED) {
// Update Gradebook.
$assign = clone $this->get_instance();
$assign->cmidnumber = $this->get_course_module()->idnumber;
// Set assign gradebook feedback plugin status.
$assign->gradefeedbackenabled = $this->is_gradebook_feedback_enabled();
assign_update_grades($assign, $userid);
foreach ($useridlist as $userid) {
$flags = $this->get_user_flags($userid, true);
$flags->workflowstate = $state;
$gradingdisabled = $this->grading_disabled($userid);
// Will not apply update if user does not have permission to assign this workflow state.
if (!$gradingdisabled && $this->update_user_flags($flags)) {
if ($state == ASSIGN_MARKING_WORKFLOW_STATE_RELEASED) {
// Update Gradebook.
$assign = clone $this->get_instance();
$assign->cmidnumber = $this->get_course_module()->idnumber;
// Set assign gradebook feedback plugin status.
$assign->gradefeedbackenabled = $this->is_gradebook_feedback_enabled();
assign_update_grades($assign, $userid);
}
$user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
\mod_assign\event\workflow_state_updated::create_from_user($this, $user, $state)->trigger();
}
$user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
\mod_assign\event\workflow_state_updated::create_from_user($this, $user, $state)->trigger();
}
}
}
@ -6152,32 +6173,50 @@ class assign {
* @return void
*/
protected function process_set_batch_marking_allocation() {
global $DB;
global $CFG, $DB;
require_sesskey();
require_capability('mod/assign:manageallocations', $this->context);
// Include batch marking allocation form.
require_once($CFG->dirroot . '/mod/assign/batchsetallocatedmarkerform.php');
$batchusers = required_param('selectedusers', PARAM_TEXT);
$markerid = required_param('allocatedmarker', PARAM_INT);
$marker = $DB->get_record('user', array('id' => $markerid), '*', MUST_EXIST);
$formparams = array(
'userscount' => 0, // This form is never re-displayed, so we don't need to
'usershtml' => '' // initialise these parameters with real information.
);
$useridlist = explode(',', $batchusers);
$markers = get_users_by_capability($this->get_context(), 'mod/assign:grade');
$markerlist = array();
foreach ($markers as $marker) {
$markerlist[$marker->id] = fullname($marker);
}
foreach ($useridlist as $userid) {
$flags = $this->get_user_flags($userid, true);
if ($flags->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_READYFORREVIEW ||
$flags->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW ||
$flags->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_READYFORRELEASE ||
$flags->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_RELEASED) {
$formparams['markers'] = $markerlist;
continue; // Allocated marker can only be changed in certain workflow states.
}
$mform = new mod_assign_batch_set_allocatedmarker_form(null, $formparams);
$flags->allocatedmarker = $marker->id;
if ($mform->is_cancelled()) {
return true;
}
if ($this->update_user_flags($flags)) {
$user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
\mod_assign\event\marker_updated::create_from_marker($this, $user, $marker)->trigger();
if ($formdata = $mform->get_data()) {
$useridlist = explode(',', $formdata->selectedusers);
$marker = $DB->get_record('user', array('id' => $formdata->allocatedmarker), '*', MUST_EXIST);
foreach ($useridlist as $userid) {
$flags = $this->get_user_flags($userid, true);
if ($flags->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_READYFORREVIEW ||
$flags->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW ||
$flags->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_READYFORRELEASE ||
$flags->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_RELEASED) {
continue; // Allocated marker can only be changed in certain workflow states.
}
$flags->allocatedmarker = $marker->id;
if ($this->update_user_flags($flags)) {
$user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
\mod_assign\event\marker_updated::create_from_marker($this, $user, $marker)->trigger();
}
}
}
}

View File

@ -263,18 +263,32 @@ class testable_assign extends assign {
}
public function testable_process_set_batch_marking_allocation($selectedusers, $markerid) {
// Ugly hack to get something into the method.
global $_POST;
$_POST['selectedusers'] = $selectedusers;
$_POST['allocatedmarker'] = $markerid;
global $CFG;
require_once($CFG->dirroot . '/mod/assign/batchsetallocatedmarkerform.php');
// Simulate the form submission.
$data = array();
$data['id'] = $this->get_course_module()->id;
$data['selectedusers'] = $selectedusers;
$data['allocatedmarker'] = $markerid;
$data['action'] = 'setbatchmarkingallocation';
mod_assign_batch_set_allocatedmarker_form::mock_submit($data);
return parent::process_set_batch_marking_allocation();
}
public function testable_process_set_batch_marking_workflow_state($selectedusers, $state) {
// Ugly hack to get something into the method.
global $_POST;
$_POST['selectedusers'] = $selectedusers;
$_POST['markingworkflowstate'] = $state;
global $CFG;
require_once($CFG->dirroot . '/mod/assign/batchsetmarkingworkflowstateform.php');
// Simulate the form submission.
$data = array();
$data['id'] = $this->get_course_module()->id;
$data['selectedusers'] = $selectedusers;
$data['markingworkflowstate'] = $state;
$data['action'] = 'setbatchmarkingworkflowstate';
mod_assign_batch_set_marking_workflow_state_form::mock_submit($data);
return parent::process_set_batch_marking_workflow_state();
}
@ -291,47 +305,41 @@ class testable_assign extends assign {
return parent::get_graders($userid);
}
public function testable_view_batch_set_workflow_state() {
global $CFG;
require_once($CFG->dirroot . '/mod/assign/batchsetmarkingworkflowstateform.php');
// Mock submit data.
$data = array();
$data['selectedusers'] = '1';
mod_assign_batch_set_marking_workflow_state_form::mock_submit($data);
// Set required variables in the form - not valid just allows us to continue.
$formparams = array();
$formparams['users'] = array(1);
$formparams['usershtml'] = 1;
$formparams['cm'] = $this->get_course_module()->id;
$formparams['context'] = $this->get_context();
$formparams['markingworkflowstates'] = 1;
$mform = new mod_assign_batch_set_marking_workflow_state_form('', $formparams);
public function testable_view_batch_set_workflow_state($selectedusers) {
$mform = $this->testable_grading_batch_operations_form('setmarkingworkflowstate', $selectedusers);
return parent::view_batch_set_workflow_state($mform);
}
public function testable_view_batch_markingallocation() {
global $CFG;
require_once($CFG->dirroot . '/mod/assign/batchsetallocatedmarkerform.php');
// Mock submit data.
$data = array();
$data['selectedusers'] = '1';
mod_assign_batch_set_allocatedmarker_form::mock_submit($data);
// Set required variables in the form - not valid just allows us to continue.
$formparams = array();
$formparams['users'] = array(1);
$formparams['usershtml'] = 1;
$formparams['cm'] = $this->get_course_module()->id;
$formparams['context'] = $this->get_context();
$formparams['markers'] = 1;
$mform = new mod_assign_batch_set_allocatedmarker_form('', $formparams);
public function testable_view_batch_markingallocation($selectedusers) {
$mform = $this->testable_grading_batch_operations_form('setmarkingallocation', $selectedusers);
return parent::view_batch_markingallocation($mform);
}
public function testable_grading_batch_operations_form($operation, $selectedusers) {
global $CFG;
require_once($CFG->dirroot . '/mod/assign/gradingbatchoperationsform.php');
// Mock submit the grading operations form.
$data = array();
$data['id'] = $this->get_course_module()->id;
$data['selectedusers'] = $selectedusers;
$data['returnaction'] = 'grading';
$data['operation'] = $operation;
mod_assign_grading_batch_operations_form::mock_submit($data);
// Set required variables in the form.
$formparams = array();
$formparams['submissiondrafts'] = 1;
$formparams['duedate'] = 1;
$formparams['attemptreopenmethod'] = ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL;
$formparams['feedbackplugins'] = array();
$formparams['markingworkflow'] = 1;
$formparams['markingallocation'] = 1;
$formparams['cm'] = $this->get_course_module()->id;
$formparams['context'] = $this->get_context();
$mform = new mod_assign_grading_batch_operations_form(null, $formparams);
return $mform;
}
}

View File

@ -912,7 +912,7 @@ class assign_events_testcase extends mod_assign_base_testcase {
// Trigger and capture the event.
$sink = $this->redirectEvents();
$assign->testable_view_batch_set_workflow_state();
$assign->testable_view_batch_set_workflow_state($this->students[0]->id);
$events = $sink->get_events();
$event = reset($events);
@ -939,7 +939,7 @@ class assign_events_testcase extends mod_assign_base_testcase {
// Trigger and capture the event.
$sink = $this->redirectEvents();
$assign->testable_view_batch_markingallocation();
$assign->testable_view_batch_markingallocation($this->students[0]->id);
$events = $sink->get_events();
$event = reset($events);