MDL-66148 mod_assign: update override page for relative dates mode

This commit is contained in:
Ryan Wyllie 2019-08-06 16:13:19 +08:00
parent 9ce55a8668
commit c872ec0cbd
9 changed files with 167 additions and 23 deletions

View File

@ -0,0 +1,2 @@
define ("mod_assign/override_form",["exports","jquery"],function(a,b){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.init=void 0;b=function(a){return a&&a.__esModule?a:{default:a}}(b);var c=function(a,c){var d=document.getElementById(a),e=d.querySelector("[name=\"".concat(c,"\"]"));(0,b.default)(e).on("change",function(){var a=document.createElement("input");a.setAttribute("type","hidden");a.setAttribute("name","userchange");a.setAttribute("value",!0);d.appendChild(a);if("undefined"!=typeof M.core_formchangechecker){M.core_formchangechecker.reset_form_dirty_state()}d.submit()})};a.init=c});
//# sourceMappingURL=override_form.min.js.map

View File

@ -0,0 +1 @@
{"version":3,"sources":["../src/override_form.js"],"names":["init","formId","selectElementName","form","document","getElementById","selectElement","querySelector","on","inputElement","createElement","setAttribute","appendChild","M","core_formchangechecker","reset_form_dirty_state","submit"],"mappings":"kJAsBA,uDAEO,GAAMA,CAAAA,CAAI,CAAG,SAACC,CAAD,CAASC,CAAT,CAA+B,IACzCC,CAAAA,CAAI,CAAGC,QAAQ,CAACC,cAAT,CAAwBJ,CAAxB,CADkC,CAEzCK,CAAa,CAAGH,CAAI,CAACI,aAAL,mBAA6BL,CAA7B,QAFyB,CAI/C,cAAEI,CAAF,EAAiBE,EAAjB,CAAoB,QAApB,CAA8B,UAAM,CAChC,GAAMC,CAAAA,CAAY,CAAGL,QAAQ,CAACM,aAAT,CAAuB,OAAvB,CAArB,CACAD,CAAY,CAACE,YAAb,CAA0B,MAA1B,CAAkC,QAAlC,EACAF,CAAY,CAACE,YAAb,CAA0B,MAA1B,CAAkC,YAAlC,EACAF,CAAY,CAACE,YAAb,CAA0B,OAA1B,KAEAR,CAAI,CAACS,WAAL,CAAiBH,CAAjB,EAEA,GAAwC,WAApC,QAAOI,CAAAA,CAAC,CAACC,sBAAb,CAAqD,CACjDD,CAAC,CAACC,sBAAF,CAAyBC,sBAAzB,EACH,CAEDZ,CAAI,CAACa,MAAL,EACH,CAbD,CAcH,CAlBM,C","sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * A javascript module to enhance the override form.\n *\n * @copyright 2019 Ryan Wyllie <ryan@moodle.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport $ from 'jquery';\n\nexport const init = (formId, selectElementName) => {\n const form = document.getElementById(formId);\n const selectElement = form.querySelector(`[name=\"${selectElementName}\"]`);\n\n $(selectElement).on('change', () => {\n const inputElement = document.createElement('input');\n inputElement.setAttribute('type', 'hidden');\n inputElement.setAttribute('name', 'userchange');\n inputElement.setAttribute('value', true);\n\n form.appendChild(inputElement);\n\n if (typeof M.core_formchangechecker !== 'undefined') {\n M.core_formchangechecker.reset_form_dirty_state();\n }\n\n form.submit();\n });\n};"],"file":"override_form.min.js"}

View File

@ -0,0 +1,43 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* A javascript module to enhance the override form.
*
* @copyright 2019 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
import $ from 'jquery';
export const init = (formId, selectElementName) => {
const form = document.getElementById(formId);
const selectElement = form.querySelector(`[name="${selectElementName}"]`);
$(selectElement).on('change', () => {
const inputElement = document.createElement('input');
inputElement.setAttribute('type', 'hidden');
inputElement.setAttribute('name', 'userchange');
inputElement.setAttribute('value', true);
form.appendChild(inputElement);
if (typeof M.core_formchangechecker !== 'undefined') {
M.core_formchangechecker.reset_form_dirty_state();
}
form.submit();
});
};

View File

@ -184,6 +184,7 @@ class assign_grading_table extends table_sql implements renderable {
}
$hasoverrides = $this->assignment->has_overrides();
$inrelativedatesmode = $this->assignment->get_course()->relativedatesmode;
if ($hasoverrides) {
$params['assignmentid5'] = (int)$this->assignment->get_instance()->id;
@ -202,7 +203,17 @@ class assign_grading_table extends table_sql implements renderable {
$fields .= ', priority.priority, ';
$fields .= 'effective.allowsubmissionsfromdate, ';
$fields .= 'effective.duedate, ';
if ($inrelativedatesmode) {
// If the priority is less than the 9999999 constant value it means it's an override
// and we should use that value directly. Otherwise we need to apply the uesr's course
// start date offset.
$fields .= 'CASE WHEN priority.priority < 9999999 THEN effective.duedate ELSE' .
' effective.duedate + enroloffset.enrolstartoffset END as duedate, ';
} else {
$fields .= 'effective.duedate, ';
}
$fields .= 'effective.cutoffdate ';
$from .= ' LEFT JOIN (
@ -258,7 +269,7 @@ class assign_grading_table extends table_sql implements renderable {
)
) effective ON effective.priority = priority.priority AND effective.userid = priority.userid ';
} else if ($this->assignment->get_course()->relativedatesmode) {
} else if ($inrelativedatesmode) {
// In relative dates mode and when we don't have overrides, include the
// duedate, cutoffdate and allowsubmissionsfrom date anyway as this information is useful and can vary.
$params['assignmentid5'] = (int)$this->assignment->get_instance()->id;
@ -401,7 +412,7 @@ class assign_grading_table extends table_sql implements renderable {
$columns[] = 'status';
$headers[] = get_string('status', 'assign');
if ($hasoverrides || $this->assignment->get_course()->relativedatesmode) {
if ($hasoverrides || $inrelativedatesmode) {
// Allowsubmissionsfromdate.
$columns[] = 'allowsubmissionsfromdate';
$headers[] = get_string('allowsubmissionsfromdate', 'assign');

View File

@ -586,6 +586,7 @@ $string['updategrade'] = 'Update grade';
$string['updatetable'] = 'Save and update table';
$string['upgradenotimplemented'] = 'Upgrade not implemented in plugin ({$a->type} {$a->subtype})';
$string['userextensiondate'] = 'Extension granted until: {$a}';
$string['userassignmentdefaults'] = 'User assignment defaults';
$string['useridlistnotcached'] = 'The grade changes were NOT saved, as it was not possible to determine which submission they were for.';
$string['useroverrides'] = 'User overrides';
$string['useroverridesdeleted'] = 'User overrides deleted';

View File

@ -909,7 +909,7 @@ class assign {
$keys = array('duedate', 'cutoffdate', 'allowsubmissionsfromdate');
foreach ($keys as $key) {
if (isset($override->{$key})) {
$this->get_instance()->{$key} = $override->{$key};
$this->get_instance($userid)->{$key} = $override->{$key};
}
}

View File

@ -58,6 +58,9 @@ class assign_override_form extends moodleform {
/** @var int sortorder, if provided. */
protected $sortorder;
/** @var int selecteduserid, if provided. */
protected $selecteduserid;
/**
* Constructor.
* @param moodle_url $submiturl the form action URL.
@ -66,8 +69,9 @@ class assign_override_form extends moodleform {
* @param object $context the assign context.
* @param bool $groupmode editing group override (true) or user override (false).
* @param object $override the override being edited, if it already exists.
* @param int $selecteduserid the user selected in the form, if any.
*/
public function __construct($submiturl, $cm, $assign, $context, $groupmode, $override) {
public function __construct($submiturl, $cm, $assign, $context, $groupmode, $override, $selecteduserid = null) {
$this->cm = $cm;
$this->assign = $assign;
@ -76,6 +80,7 @@ class assign_override_form extends moodleform {
$this->groupid = empty($override->groupid) ? 0 : $override->groupid;
$this->userid = empty($override->userid) ? 0 : $override->userid;
$this->sortorder = empty($override->sortorder) ? null : $override->sortorder;
$this->selecteduserid = $selecteduserid;
parent::__construct($submiturl, null, 'post');
@ -85,10 +90,13 @@ class assign_override_form extends moodleform {
* Define this form - called by the parent constructor
*/
protected function definition() {
global $DB;
global $DB, $OUTPUT, $PAGE;
$cm = $this->cm;
$mform = $this->_form;
$userid = $this->selecteduserid ?? $this->userid ?: null;
$assigninstance = $this->assign->get_instance($userid);
$inrelativedatesmode = !empty($this->assign->get_course()->relativedatesmode);
$mform->addElement('header', 'override', get_string('override', 'assign'));
@ -196,6 +204,32 @@ class assign_override_form extends moodleform {
$mform->addElement('searchableselector', 'userid',
get_string('overrideuser', 'assign'), $userchoices);
$mform->addRule('userid', get_string('required'), 'required', null, 'client');
if ($inrelativedatesmode) {
// If in relative dates mode then add the JS to reload the page when the user
// selection is changed to ensure that the correct dates are displayed.
$PAGE->requires->js_call_amd('mod_assign/override_form', 'init', [
$mform->getAttribute('id'),
'userid'
]);
}
}
if ($inrelativedatesmode) {
if ($userid) {
$templatecontext = [
'allowsubmissionsfromdate' => $assigninstance->allowsubmissionsfromdate,
'duedate' => $assigninstance->duedate,
'cutoffdate' => $assigninstance->cutoffdate
];
$html = $OUTPUT->render_from_template('mod_assign/override_form_user_defaults', $templatecontext);
} else {
$html = get_string('noselection', 'form');
}
$groupelements = [];
$groupelements[] = $mform->createElement('html', $html);
$mform->addGroup($groupelements, null, get_string('userassignmentdefaults', 'mod_assign'), null, false);
}
}
@ -203,7 +237,7 @@ class assign_override_form extends moodleform {
array_push($users, $this->userid);
$extensionmax = 0;
foreach ($users as $value) {
$extension = $DB->get_record('assign_user_flags', array('assignment' => $this->assign->get_instance()->id,
$extension = $DB->get_record('assign_user_flags', array('assignment' => $assigninstance->id,
'userid' => $value));
if ($extension) {
if ($extensionmax < $extension->extensionduedate) {
@ -213,23 +247,23 @@ class assign_override_form extends moodleform {
}
if ($extensionmax) {
$this->assign->get_instance()->extensionduedate = $extensionmax;
$assigninstance->extensionduedate = $extensionmax;
}
// Open and close dates.
$mform->addElement('date_time_selector', 'allowsubmissionsfromdate',
get_string('allowsubmissionsfromdate', 'assign'), array('optional' => true));
$mform->setDefault('allowsubmissionsfromdate', $this->assign->get_instance()->allowsubmissionsfromdate);
$mform->setDefault('allowsubmissionsfromdate', $assigninstance->allowsubmissionsfromdate);
$mform->addElement('date_time_selector', 'duedate', get_string('duedate', 'assign'), array('optional' => true));
$mform->setDefault('duedate', $this->assign->get_instance()->duedate);
$mform->setDefault('duedate', $assigninstance->duedate);
$mform->addElement('date_time_selector', 'cutoffdate', get_string('cutoffdate', 'assign'), array('optional' => true));
$mform->setDefault('cutoffdate', $this->assign->get_instance()->cutoffdate);
$mform->setDefault('cutoffdate', $assigninstance->cutoffdate);
if (isset($this->assign->get_instance()->extensionduedate)) {
if (isset($assigninstance->extensionduedate)) {
$mform->addElement('static', 'extensionduedate', get_string('extensionduedate', 'assign'),
userdate($this->assign->get_instance()->extensionduedate));
userdate($assigninstance->extensionduedate));
}
// Submit buttons.
@ -259,7 +293,8 @@ class assign_override_form extends moodleform {
$errors = parent::validation($data, $files);
$mform =& $this->_form;
$assign = $this->assign;
$userid = $this->selecteduserid ?? $this->userid ?: null;
$assigninstance = $this->assign->get_instance($userid);
if ($mform->elementExists('userid')) {
if (empty($data['userid'])) {
@ -293,13 +328,13 @@ class assign_override_form extends moodleform {
}
// Ensure that override duedate/allowsubmissionsfromdate are before extension date if exist.
if (!empty($assign->get_instance()->extensionduedate) && !empty($data['duedate'])) {
if ($assign->get_instance()->extensionduedate < $data['duedate']) {
if (!empty($assigninstance->extensionduedate) && !empty($data['duedate'])) {
if ($assigninstance->extensionduedate < $data['duedate']) {
$errors['duedate'] = get_string('extensionnotafterduedate', 'assign');
}
}
if (!empty($assign->get_instance()->extensionduedate) && !empty($data['allowsubmissionsfromdate'])) {
if ($assign->get_instance()->extensionduedate < $data['allowsubmissionsfromdate']) {
if (!empty($assigninstance->extensionduedate) && !empty($data['allowsubmissionsfromdate'])) {
if ($assigninstance->extensionduedate < $data['allowsubmissionsfromdate']) {
$errors['allowsubmissionsfromdate'] = get_string('extensionnotafterfromdate', 'assign');
}
}
@ -308,7 +343,7 @@ class assign_override_form extends moodleform {
$changed = false;
$keys = array('duedate', 'cutoffdate', 'allowsubmissionsfromdate');
foreach ($keys as $key) {
if ($data[$key] != $assign->get_instance()->{$key}) {
if ($data[$key] != $assigninstance->{$key}) {
$changed = true;
break;
}

View File

@ -33,6 +33,8 @@ $cmid = optional_param('cmid', 0, PARAM_INT);
$overrideid = optional_param('id', 0, PARAM_INT);
$action = optional_param('action', null, PARAM_ALPHA);
$reset = optional_param('reset', false, PARAM_BOOL);
$userid = optional_param('userid', null, PARAM_INT);
$userchange = optional_param('userchange', false, PARAM_BOOL);
$pagetitle = get_string('editoverride', 'assign');
@ -68,7 +70,9 @@ require_login($course, false, $cm);
$context = context_module::instance($cm->id);
$assign = new assign($context, $cm, $course);
$assigninstance = $assign->get_instance();
$assigninstance = $assign->get_instance($userid);
$shouldadduserid = $userid && !empty($course->relativedatesmode);
$shouldresetform = optional_param('resetbutton', 0, PARAM_ALPHA) || ($userchange && $action !== 'duplicate');
// Add or edit an override.
require_capability('mod/assign:manageoverrides', $context);
@ -111,23 +115,30 @@ if ($action === 'duplicate') {
$pagetitle = get_string('duplicateoverride', 'assign');
}
if ($shouldadduserid) {
$data->userid = $userid;
}
$overridelisturl = new moodle_url('/mod/assign/overrides.php', array('cmid' => $cm->id));
if (!$groupmode) {
$overridelisturl->param('mode', 'user');
}
// Setup the form.
$mform = new assign_override_form($url, $cm, $assign, $context, $groupmode, $override);
$mform = new assign_override_form($url, $cm, $assign, $context, $groupmode, $override, $userid);
$mform->set_data($data);
if ($mform->is_cancelled()) {
redirect($overridelisturl);
} else if (optional_param('resetbutton', 0, PARAM_ALPHA)) {
} else if ($shouldresetform) {
$url->param('reset', true);
if ($shouldadduserid) {
$url->param('userid', $userid);
}
redirect($url);
} else if ($fromform = $mform->get_data()) {
} else if (!$userchange && $fromform = $mform->get_data()) {
// Process the data.
$fromform->assignid = $assigninstance->id;

View File

@ -0,0 +1,40 @@
{{!
This file is part of Moodle - http://moodle.org/
Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template mod_assign/override_form_user_defaults
HTML for showing user default dates in override form.
}}
<dl>
<dt>{{#str}} allowsubmissionsfromdate, mod_assign {{/str}}</dt>
<dl>
{{#allowsubmissionsfromdate}} {{#userdate}} {{.}}, {{#str}} strftimedatetime, core_langconfig {{/str}} {{/userdate}} {{/allowsubmissionsfromdate}}
{{^allowsubmissionsfromdate}} {{#str}} disabled, mod_assign {{/str}} {{/allowsubmissionsfromdate}}
</dl>
<dt>{{#str}} duedate, mod_assign {{/str}}</dt>
<dl>
{{#duedate}} {{#userdate}} {{.}}, {{#str}} strftimedatetime, core_langconfig {{/str}} {{/userdate}} {{/duedate}}
{{^duedate}} {{#str}} disabled, mod_assign {{/str}} {{/duedate}}
</dl>
<dt>{{#str}} cutoffdate, mod_assign {{/str}}</dt>
<dl>
{{#cutoffdate}} {{#userdate}} {{cutoffdate}}, {{#str}} strftimedatetime, core_langconfig {{/str}} {{/userdate}} {{/cutoffdate}}
{{^cutoffdate}} {{#str}} disabled, mod_assign {{/str}} {{/cutoffdate}}
</dl>
</dl>