MDL-32235 introducing new workshop scheduled allocation plugin
[moodle.git] / mod / workshop / allocation / scheduled / lib.php
CommitLineData
98621280
DM
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Scheduled allocator that internally executes the random allocation later
20 *
21 * @package workshopallocation_scheduled
22 * @subpackage mod_workshop
23 * @copyright 2012 David Mudrak <david@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29require_once(dirname(dirname(__FILE__)) . '/lib.php'); // interface definition
30require_once(dirname(dirname(dirname(__FILE__))) . '/locallib.php'); // workshop internal API
31require_once(dirname(dirname(__FILE__)) . '/random/lib.php'); // random allocator
32require_once(dirname(__FILE__) . '/settings_form.php'); // our settings form
33
34/**
35 * Allocates the submissions randomly in a cronjob task
36 */
37class workshop_scheduled_allocator implements workshop_allocator {
38
39 /** workshop instance */
40 protected $workshop;
41
42 /** workshop_scheduled_allocator_form with settings for the random allocator */
43 protected $mform;
44
45 /**
46 * @param workshop $workshop Workshop API object
47 */
48 public function __construct(workshop $workshop) {
49 $this->workshop = $workshop;
50 }
51
52 /**
53 * Save the settings for the random allocator to execute it later
54 */
55 public function init() {
56 global $PAGE;
57
58 $result = new workshop_allocation_result($this);
59 $customdata = array();
60 $customdata['workshop'] = $this->workshop;
61 $this->mform = new workshop_scheduled_allocator_form($PAGE->url, $customdata);
62 if ($this->mform->is_cancelled()) {
63 redirect($PAGE->url->out(false));
64 } else if ($settings = $this->mform->get_data()) {
65 if (empty($settings->enablescheduled)) {
66 $enabled = false;
67 } else {
68 $enabled = true;
69 }
70 $settings = workshop_random_allocator_setting::instance_from_object($settings);
71 $this->store_settings($enabled, $settings, $result);
72 if ($enabled) {
73 $msg = get_string('resultenabled', 'workshopallocation_scheduled');
74 } else {
75 $msg = get_string('resultdisabled', 'workshopallocation_scheduled');
76 }
77 $result->set_status(workshop_allocation_result::STATUS_CONFIGURED, $msg);
78 return $result;
79 } else {
80 // this branch is executed if the form is submitted but the data
81 // doesn't validate and the form should be redisplayed
82 // or on the first display of the form.
83 $result->set_status(workshop_allocation_result::STATUS_VOID);
84 return $result;
85 }
86 }
87
88 /**
89 * Returns the HTML code to print the user interface
90 */
91 public function ui() {
92 global $PAGE;
93
94 $output = $PAGE->get_renderer('mod_workshop');
95
96 // the submissions deadline must be defined
97 if (empty($this->workshop->submissionend)) {
98 $out = $output->box(get_string('nosubmissionend', 'workshopallocation_scheduled'), 'generalbox', 'notice');
99 $out .= $output->continue_button($this->workshop->updatemod_url());
100 return $out;
101 }
102
103 $out = $output->container_start('scheduled-allocator');
104 // the nasty hack follows to bypass the sad fact that moodle quickforms do not allow to actually
105 // return the HTML content, just to display it
106 ob_start();
107 $this->mform->display();
108 $out .= ob_get_contents();
109 ob_end_clean();
110 $out .= $output->container_end();
111
112 return $out;
113 }
114
115 /**
116 * Delete all data related to a given workshop module instance
117 *
118 * @see workshop_delete_instance()
119 * @param int $workshopid id of the workshop module instance being deleted
120 * @return void
121 */
122 public static function delete_instance($workshopid) {
123 // TODO
124 return;
125 }
126
127 /**
128 * Stores the pre-defined random allocation settings for later usage
129 *
130 * @param bool $enabled is the scheduled allocation enabled
131 * @param workshop_random_allocator_setting $settings settings form data
132 * @param workshop_allocation_result $result logger
133 */
134 protected function store_settings($enabled, workshop_random_allocator_setting $settings, workshop_allocation_result $result) {
135 $result->log(print_r($settings, true), 'debug');
136 }
137}