测验时间设置限制:仅允许设置11:00-17:00时段
Hey there! Let's sort out this time restriction issue properly—your initial change affected all modules because you modified the global datetime selector component, which every part of the system uses. Here's how to target only the quiz module's time settings:
Step 1: Revert the global selector change
First, undo the edit you made to lib/form/datetimeselector.php. That file is a shared tool used by every module in the system, so altering it restricts time picks everywhere—not just quizzes. Restoring it will fix the unintended side effects for other modules.
Step 2: Target the quiz module's specific form
You need to modify the form that handles quiz creation/editing settings, which lives in mod/quiz/edit_form.php. This file defines all the fields you see when setting up a quiz, including the open and close time pickers.
Step 3: Add hour range restrictions to quiz time fields
In mod/quiz/edit_form.php, look for the lines where the timeopen (quiz start time) and timeclose (quiz end time) fields are added. They’ll look something like this (varies slightly by system version):
$mform->add('timeopen', 'datetime', get_string('quizopen', 'quiz'), ['optional' => true]); $mform->add('timeclose', 'datetime', get_string('quizclose', 'quiz'), ['optional' => true]);
Replace those lines with code that explicitly sets the allowed hour range (11 to 17, since 5 PM is 17:00 in 24-hour time):
// Define allowed hours (11 AM to 5 PM) $allowed_hours = range(11, 17); // Format hours as two-digit strings for the dropdown menu $hour_options = array_combine($allowed_hours, array_map(function($hour) { return sprintf('%02d', $hour); }, $allowed_hours)); // Add restricted quiz open time field $mform->add('timeopen', 'datetime', get_string('quizopen', 'quiz'), [ 'optional' => true, 'houroptions' => $hour_options ]); // Add restricted quiz close time field $mform->add('timeclose', 'datetime', get_string('quizclose', 'quiz'), [ 'optional' => true, 'houroptions' => $hour_options ]);
Optional: Restrict to whole hours only
If you want to force users to select only full hours (no 11:30 or 14:15), add a minuteoptions parameter to each field:
'minuteoptions' => ['00' => '00']
Why this works
By setting houroptions directly on the quiz's time fields, you’re only restricting time selections for quiz settings—all other modules will still use the full range of hours. Plus, both start and end times will respect your 11 AM to 5 PM window without interfering with each other.
内容的提问来源于stack exchange,提问作者Iyas

