Skip to content

Commit

Permalink
MDL-68944 mod_workshop: Workshop skips scheduled allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
juancs committed Nov 25, 2021
1 parent 16a5169 commit 42fa699
Show file tree
Hide file tree
Showing 8 changed files with 481 additions and 50 deletions.
37 changes: 37 additions & 0 deletions mod/workshop/allocation/scheduled/classes/observer.php
Expand Up @@ -81,4 +81,41 @@ public static function workshop_viewed($event) {
}
return true;
}

/**
* Called when the '\mod_workshop\event\phase_automatically_switched' event is triggered.
*
* This observer handles the phase_automatically_switched event triggered when phaseswithassesment is active
* and the phase is automatically switched.
*
* When this happens, this situation can occur:
*
* * cron_task transition the workshop to PHASE_ASESSMENT.
* * scheduled_allocator task executes.
* * scheduled_allocator task cannot allocate parcipants because workshop is not
* in PHASE_SUBMISSION state (it's in PHASE_ASSESMENT).
*
* @param \mod_workshop\event\phase_automatically_switched $event
*/
public static function phase_automatically_switched(\mod_workshop\event\phase_automatically_switched $event) {
if ($event->other['previousworkshopphase'] != \workshop::PHASE_SUBMISSION) {
return;
}
if ($event->other['targetworkshopphase'] != \workshop::PHASE_ASSESSMENT) {
return;
}

$workshop = $event->get_record_snapshot('workshop', $event->objectid);
$course = $event->get_record_snapshot('course', $event->courseid);
$cm = $event->get_record_snapshot('course_modules', $event->contextinstanceid);

$workshop = new \workshop($workshop, $cm, $course);
if ($workshop->phase != \workshop::PHASE_ASSESSMENT) {
return;
}

$allocator = $workshop->allocator_instance('scheduled');
// We know that we come from PHASE_SUBMISSION so we tell the allocator not to test for the PHASE_SUBMISSION state.
$allocator->execute(false);
}
}
5 changes: 5 additions & 0 deletions mod/workshop/allocation/scheduled/db/events.php
Expand Up @@ -31,5 +31,10 @@
array(
'eventname' => '\mod_workshop\event\course_module_viewed',
'callback' => '\workshopallocation_scheduled\observer::workshop_viewed',
),

array(
'eventname' => '\mod_workshop\event\phase_automatically_switched',
'callback' => '\workshopallocation_scheduled\observer::phase_automatically_switched'
)
);
120 changes: 73 additions & 47 deletions mod/workshop/allocation/scheduled/lib.php
Expand Up @@ -128,71 +128,97 @@ public function ui() {
/**
* Executes the allocation
*
* @param bool $checksubmissionphase Check that the workshop is in submission phase before doing anything else.
* @return workshop_allocation_result
*/
public function execute() {
public function execute(bool $checksubmissionphase = true) {
global $DB;

$result = new workshop_allocation_result($this);

// make sure the workshop itself is at the expected state

if ($this->workshop->phase != workshop::PHASE_SUBMISSION) {
// Execution can occur in multiple places. Ensure we only allocate one at a time.
$lockfactory = \core\lock\lock_config::get_lock_factory('mod_workshop_allocation_scheduled_execution');
$executionlock = $lockfactory->get_lock($this->workshop->id, 1, 30);
if (!$executionlock) {
$result->set_status(workshop_allocation_result::STATUS_FAILED,
get_string('resultfailedphase', 'workshopallocation_scheduled'));
return $result;
get_string('resultfailed', 'workshopallocation_scheduled'));
}

if (empty($this->workshop->submissionend)) {
$result->set_status(workshop_allocation_result::STATUS_FAILED,
get_string('resultfaileddeadline', 'workshopallocation_scheduled'));
return $result;
}
try {
// Make sure the workshop itself is at the expected state.

if ($this->workshop->submissionend > time()) {
$result->set_status(workshop_allocation_result::STATUS_VOID,
get_string('resultvoiddeadline', 'workshopallocation_scheduled'));
return $result;
}
if ($checksubmissionphase && $this->workshop->phase != workshop::PHASE_SUBMISSION) {
$executionlock->release();
$result->set_status(workshop_allocation_result::STATUS_FAILED,
get_string('resultfailedphase', 'workshopallocation_scheduled'));
return $result;
}

$current = $DB->get_record('workshopallocation_scheduled',
array('workshopid' => $this->workshop->id, 'enabled' => 1), '*', IGNORE_MISSING);
if (empty($this->workshop->submissionend)) {
$executionlock->release();
$result->set_status(workshop_allocation_result::STATUS_FAILED,
get_string('resultfaileddeadline', 'workshopallocation_scheduled'));
return $result;
}

if ($current === false) {
$result->set_status(workshop_allocation_result::STATUS_FAILED,
get_string('resultfailedconfig', 'workshopallocation_scheduled'));
return $result;
}
if ($this->workshop->submissionend > time()) {
$executionlock->release();
$result->set_status(workshop_allocation_result::STATUS_VOID,
get_string('resultvoiddeadline', 'workshopallocation_scheduled'));
return $result;
}

if (!$current->enabled) {
$result->set_status(workshop_allocation_result::STATUS_VOID,
get_string('resultdisabled', 'workshopallocation_scheduled'));
return $result;
}
$current = $DB->get_record('workshopallocation_scheduled',
array('workshopid' => $this->workshop->id, 'enabled' => 1), '*', IGNORE_MISSING);

if (!is_null($current->timeallocated) and $current->timeallocated >= $this->workshop->submissionend) {
$result->set_status(workshop_allocation_result::STATUS_VOID,
get_string('resultvoidexecuted', 'workshopallocation_scheduled'));
return $result;
}
if ($current === false) {
$executionlock->release();
$result->set_status(workshop_allocation_result::STATUS_FAILED,
get_string('resultfailedconfig', 'workshopallocation_scheduled'));
return $result;
}

if (!$current->enabled) {
$executionlock->release();
$result->set_status(workshop_allocation_result::STATUS_VOID,
get_string('resultdisabled', 'workshopallocation_scheduled'));
return $result;
}

// so now we know that we are after the submissions deadline and either the scheduled allocation was not
// executed yet or it was but the submissions deadline has been prolonged (and hence we should repeat the
// allocations)
if (!is_null($current->timeallocated) and $current->timeallocated >= $this->workshop->submissionend) {
$executionlock->release();
$result->set_status(workshop_allocation_result::STATUS_VOID,
get_string('resultvoidexecuted', 'workshopallocation_scheduled'));
return $result;
}

// So now we know that we are after the submissions deadline and either the scheduled allocation was not
// executed yet or it was but the submissions deadline has been prolonged (and hence we should repeat the
// allocations).

$settings = workshop_random_allocator_setting::instance_from_text($current->settings);
$randomallocator = $this->workshop->allocator_instance('random');
$randomallocator->execute($settings, $result);

// Store the result in the instance's table.
$update = new stdClass();
$update->id = $current->id;
$update->timeallocated = $result->get_timeend();
$update->resultstatus = $result->get_status();
$update->resultmessage = $result->get_message();
$update->resultlog = json_encode($result->get_logs());

$settings = workshop_random_allocator_setting::instance_from_text($current->settings);
$randomallocator = $this->workshop->allocator_instance('random');
$randomallocator->execute($settings, $result);
$DB->update_record('workshopallocation_scheduled', $update);

// store the result in the instance's table
$update = new stdClass();
$update->id = $current->id;
$update->timeallocated = $result->get_timeend();
$update->resultstatus = $result->get_status();
$update->resultmessage = $result->get_message();
$update->resultlog = json_encode($result->get_logs());
} catch (\Exception $e) {
$executionlock->release();
$result->set_status(workshop_allocation_result::STATUS_FAILED,
get_string('resultfailed', 'workshopallocation_scheduled'));

throw $e;
}

$DB->update_record('workshopallocation_scheduled', $update);
$executionlock->release();

return $result;
}
Expand Down
200 changes: 200 additions & 0 deletions mod/workshop/allocation/scheduled/tests/scheduled_allocator_test.php
@@ -0,0 +1,200 @@
<?php
// 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/>.

namespace workshopallocation_scheduled;

/**
* Test for the scheduled allocator.
*
* @package workshopallocation_scheduled
* @copyright 2020 Jaume I University <https://www.uji.es/>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class scheduled_allocator_test extends \advanced_testcase {

/** @var \stdClass $course The course where the tests will be run */
private $course;

/** @var \workshop $workshop The workshop where the tests will be run */
private $workshop;

/** @var \stdClass $workshopcm The workshop course module instance */
private $workshopcm;

/** @var \stdClass[] $students An array of student enrolled in $course */
private $students;

/**
* Tests that student submissions get automatically alocated after the submission deadline and when the workshop
* "Switch to the next phase after the submissions deadline" checkbox is active.
*/
public function test_that_allocator_in_executed_on_submission_end_when_phaseswitchassessment_is_active(): void {
global $DB;

$this->resetAfterTest();

$this->setup_test_course_and_workshop();

$this->activate_switch_to_the_next_phase_after_submission_deadline();
$this->set_the_submission_deadline_in_the_past();
$this->activate_the_scheduled_allocator();

$workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');

cron_setup_user();

// Let the students add submissions.
$this->workshop->switch_phase(\workshop::PHASE_SUBMISSION);

// Create some submissions.
foreach ($this->students as $student) {
$workshopgenerator->create_submission($this->workshop->id, $student->id);
}

// No allocations yet.
$this->assertEmpty($this->workshop->get_allocations());

/* Execute the tasks that will do the transition and allocation thing.
* We expect the workshop cron to do the whole work: change the phase and
* allocate the submissions.
*/
$this->execute_workshop_cron_task();

$workshopdb = $DB->get_record('workshop', ['id' => $this->workshop->id]);
$workshop = new \workshop($workshopdb, $this->workshopcm, $this->course);

$this->assertEquals(\workshop::PHASE_ASSESSMENT, $workshop->phase);
$this->assertNotEmpty($workshop->get_allocations());
}

/**
* No allocations are performed if the allocator is not enabled.
*/
public function test_that_allocator_is_not_executed_when_its_not_active(): void {
global $DB;

$this->resetAfterTest();

$this->setup_test_course_and_workshop();
$this->activate_switch_to_the_next_phase_after_submission_deadline();
$this->set_the_submission_deadline_in_the_past();

$workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');

cron_setup_user();

// Let the students add submissions.
$this->workshop->switch_phase(\workshop::PHASE_SUBMISSION);

// Create some submissions.
foreach ($this->students as $student) {
$workshopgenerator->create_submission($this->workshop->id, $student->id);
}

// No allocations yet.
$this->assertEmpty($this->workshop->get_allocations());

// Transition to the assessment phase.
$this->execute_workshop_cron_task();

$workshopdb = $DB->get_record('workshop', ['id' => $this->workshop->id]);
$workshop = new \workshop($workshopdb, $this->workshopcm, $this->course);

// No allocations too.
$this->assertEquals(\workshop::PHASE_ASSESSMENT, $workshop->phase);
$this->assertEmpty($workshop->get_allocations());
}

/**
* Activates and configures the scheduled allocator for the workshop.
*/
private function activate_the_scheduled_allocator(): void {

$settings = \workshop_random_allocator_setting::instance_from_object((object)[
'numofreviews' => count($this->students),
'numper' => 1,
'removecurrentuser' => true,
'excludesamegroup' => false,
'assesswosubmission' => true,
'addselfassessment' => false
]);

$allocator = new \workshop_scheduled_allocator($this->workshop);

$storesettingsmethod = new \ReflectionMethod('workshop_scheduled_allocator', 'store_settings');
$storesettingsmethod->setAccessible(true);
$storesettingsmethod->invoke($allocator, true, true, $settings, new \workshop_allocation_result($allocator));
}

/**
* Creates a minimum common setup to execute tests:
*/
protected function setup_test_course_and_workshop(): void {
$this->setAdminUser();

$datagenerator = $this->getDataGenerator();

$this->course = $datagenerator->create_course();

$this->students = [];
for ($i = 0; $i < 10; $i++) {
$this->students[] = $datagenerator->create_and_enrol($this->course);
}

$workshopdb = $datagenerator->create_module('workshop', [
'course' => $this->course,
'name' => 'Test Workshop',
]);
$this->workshopcm = get_coursemodule_from_instance('workshop', $workshopdb->id, $this->course->id, false, MUST_EXIST);
$this->workshop = new \workshop($workshopdb, $this->workshopcm, $this->course);
}

/**
* Executes the workshop cron task.
*/
protected function execute_workshop_cron_task(): void {
ob_start();
$cron = new \mod_workshop\task\cron_task();
$cron->execute();
ob_end_clean();
}

/**
* Executes the scheduled allocator cron task.
*/
protected function execute_allocator_cron_task(): void {
ob_start();
$cron = new \workshopallocation_scheduled\task\cron_task();
$cron->execute();
ob_end_clean();
}

/**
* Activates the "Switch to the next phase after the submissions deadline" flag in the workshop.
*/
protected function activate_switch_to_the_next_phase_after_submission_deadline(): void {
global $DB;
$DB->set_field('workshop', 'phaseswitchassessment', 1, ['id' => $this->workshop->id]);
}

/**
* Sets the submission deadline in a past time.
*/
protected function set_the_submission_deadline_in_the_past(): void {
global $DB;
$DB->set_field('workshop', 'submissionend', time() - 1, ['id' => $this->workshop->id]);
}
}

0 comments on commit 42fa699

Please sign in to comment.