MDL-32235 execute scheduled allocation by cron
[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() {
9f8f3808 56 global $PAGE, $DB;
98621280
DM
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);
9f8f3808 62
98621280
DM
63 if ($this->mform->is_cancelled()) {
64 redirect($PAGE->url->out(false));
65 } else if ($settings = $this->mform->get_data()) {
66 if (empty($settings->enablescheduled)) {
67 $enabled = false;
68 } else {
69 $enabled = true;
70 }
71 $settings = workshop_random_allocator_setting::instance_from_object($settings);
72 $this->store_settings($enabled, $settings, $result);
73 if ($enabled) {
74 $msg = get_string('resultenabled', 'workshopallocation_scheduled');
75 } else {
76 $msg = get_string('resultdisabled', 'workshopallocation_scheduled');
77 }
78 $result->set_status(workshop_allocation_result::STATUS_CONFIGURED, $msg);
79 return $result;
80 } else {
81 // this branch is executed if the form is submitted but the data
82 // doesn't validate and the form should be redisplayed
83 // or on the first display of the form.
9f8f3808
DM
84
85 $current = $DB->get_record('workshopallocation_scheduled',
86 array('workshopid' => $this->workshop->id), '*', IGNORE_MISSING);
87
88 if ($current !== false) {
89 $data = workshop_random_allocator_setting::instance_from_text($current->settings);
90 $data->enablescheduled = $current->enabled;
91 $this->mform->set_data($data);
92 }
93
98621280
DM
94 $result->set_status(workshop_allocation_result::STATUS_VOID);
95 return $result;
96 }
97 }
98
99 /**
100 * Returns the HTML code to print the user interface
101 */
102 public function ui() {
103 global $PAGE;
104
105 $output = $PAGE->get_renderer('mod_workshop');
106
107 // the submissions deadline must be defined
108 if (empty($this->workshop->submissionend)) {
109 $out = $output->box(get_string('nosubmissionend', 'workshopallocation_scheduled'), 'generalbox', 'notice');
110 $out .= $output->continue_button($this->workshop->updatemod_url());
111 return $out;
112 }
113
114 $out = $output->container_start('scheduled-allocator');
115 // the nasty hack follows to bypass the sad fact that moodle quickforms do not allow to actually
116 // return the HTML content, just to display it
117 ob_start();
118 $this->mform->display();
119 $out .= ob_get_contents();
120 ob_end_clean();
121 $out .= $output->container_end();
122
123 return $out;
124 }
125
2a3aac40
DM
126 /**
127 * Executes the allocation
128 *
129 * @return workshop_allocation_result
130 */
131 public function execute() {
132 global $DB;
133
134 $result = new workshop_allocation_result($this);
135
136 // make sure the workshop itself is at the expected state
137
138 if ($this->workshop->phase != workshop::PHASE_SUBMISSION) {
139 $result->set_status(workshop_allocation_result::STATUS_FAILED,
140 get_string('resultfailedphase', 'workshopallocation_scheduled'));
141 return $result;
142 }
143
144 if (empty($this->workshop->submissionend)) {
145 $result->set_status(workshop_allocation_result::STATUS_FAILED,
146 get_string('resultfaileddeadline', 'workshopallocation_scheduled'));
147 return $result;
148 }
149
150 if ($this->workshop->submissionend > time()) {
151 $result->set_status(workshop_allocation_result::STATUS_VOID,
152 get_string('resultvoiddeadline', 'workshopallocation_scheduled'));
153 return $result;
154 }
155
156 $current = $DB->get_record('workshopallocation_scheduled',
157 array('workshopid' => $this->workshop->id, 'enabled' => 1), '*', IGNORE_MISSING);
158
159 if ($current === false) {
160 $result->set_status(workshop_allocation_result::STATUS_FAILED,
161 get_string('resultfailedconfig', 'workshopallocation_scheduled'));
162 return $result;
163 }
164
165 if (!$current->enabled) {
166 $result->set_status(workshop_allocation_result::STATUS_VOID,
167 get_string('resultdisabled', 'workshopallocation_scheduled'));
168 return $result;
169 }
170
171 if (!is_null($current->timeallocated) and $current->timeallocated >= $this->workshop->submissionend) {
172 $result->set_status(workshop_allocation_result::STATUS_VOID,
173 get_string('resultvoidexecuted', 'workshopallocation_scheduled'));
174 return $result;
175 }
176
177 // so now we know that we are after the submissions deadline and either the scheduled allocation was not
178 // executed yet or it was but the submissions deadline has been prolonged (and hence we should repeat the
179 // allocations)
180
181 $settings = workshop_random_allocator_setting::instance_from_text($current->settings);
182 $randomallocator = $this->workshop->allocator_instance('random');
183 $randomallocator->execute($settings, $result);
184
185 // store the result in the instance's table
186 $update = new stdClass();
187 $update->id = $current->id;
188 $update->timeallocated = $result->get_timeend();
189 $update->resultstatus = $result->get_status();
190 $update->resultmessage = $result->get_message();
191 $update->resultlog = json_encode($result->get_logs());
192
193 $DB->update_record('workshopallocation_scheduled', $update);
194
195 return $result;
196 }
197
98621280
DM
198 /**
199 * Delete all data related to a given workshop module instance
200 *
201 * @see workshop_delete_instance()
202 * @param int $workshopid id of the workshop module instance being deleted
203 * @return void
204 */
205 public static function delete_instance($workshopid) {
206 // TODO
207 return;
208 }
209
210 /**
211 * Stores the pre-defined random allocation settings for later usage
212 *
213 * @param bool $enabled is the scheduled allocation enabled
214 * @param workshop_random_allocator_setting $settings settings form data
215 * @param workshop_allocation_result $result logger
216 */
217 protected function store_settings($enabled, workshop_random_allocator_setting $settings, workshop_allocation_result $result) {
9f8f3808
DM
218 global $DB;
219
220
221 $data = new stdClass();
222 $data->workshopid = $this->workshop->id;
223 $data->enabled = $enabled;
224 $data->submissionend = $this->workshop->submissionend;
225 $data->timeallocated = null;
226 $data->settings = $settings->export_text();
227
228 $result->log($data->settings, 'debug');
229
230 $current = $DB->get_record('workshopallocation_scheduled', array('workshopid' => $data->workshopid), '*', IGNORE_MISSING);
231
232 if ($current === false) {
233 $DB->insert_record('workshopallocation_scheduled', $data);
234
235 } else {
236 $data->id = $current->id;
237 $DB->update_record('workshopallocation_scheduled', $data);
238 }
98621280
DM
239 }
240}
2a3aac40
DM
241
242/**
243 * Regular jobs to execute via cron
244 */
245function workshopallocation_scheduled_cron() {
246 global $CFG, $DB;
247
248 $sql = "SELECT w.*
249 FROM {workshopallocation_scheduled} a
250 JOIN {workshop} w ON a.workshopid = w.id
251 WHERE a.enabled = 1
252 AND w.phase = 20
253 AND w.submissionend > 0
254 AND w.submissionend < ?
255 AND (a.timeallocated IS NULL OR a.timeallocated < w.submissionend)";
256
257 $workshops = $DB->get_records_sql($sql, array(time()));
258
259 if (empty($workshops)) {
260 mtrace('No workshops ready for scheduled allocation');
261 return;
262 }
263
264 // let's have some fun!
265 require_once($CFG->dirroot.'/mod/workshop/locallib.php');
266
267 foreach ($workshops as $workshop) {
268 $cm = get_coursemodule_from_instance('workshop', $workshop->id, $workshop->course, false, MUST_EXIST);
269 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
270 $workshop = new workshop($workshop, $cm, $course);
271 $allocator = $workshop->allocator_instance('scheduled');
272 $result = $allocator->execute();
273
274 // todo inform the teachers about the results
275 }
276}