MDL-32235 fixing DB schema
[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
126 /**
127 * Delete all data related to a given workshop module instance
128 *
129 * @see workshop_delete_instance()
130 * @param int $workshopid id of the workshop module instance being deleted
131 * @return void
132 */
133 public static function delete_instance($workshopid) {
134 // TODO
135 return;
136 }
137
138 /**
139 * Stores the pre-defined random allocation settings for later usage
140 *
141 * @param bool $enabled is the scheduled allocation enabled
142 * @param workshop_random_allocator_setting $settings settings form data
143 * @param workshop_allocation_result $result logger
144 */
145 protected function store_settings($enabled, workshop_random_allocator_setting $settings, workshop_allocation_result $result) {
9f8f3808
DM
146 global $DB;
147
148
149 $data = new stdClass();
150 $data->workshopid = $this->workshop->id;
151 $data->enabled = $enabled;
152 $data->submissionend = $this->workshop->submissionend;
153 $data->timeallocated = null;
154 $data->settings = $settings->export_text();
155
156 $result->log($data->settings, 'debug');
157
158 $current = $DB->get_record('workshopallocation_scheduled', array('workshopid' => $data->workshopid), '*', IGNORE_MISSING);
159
160 if ($current === false) {
161 $DB->insert_record('workshopallocation_scheduled', $data);
162
163 } else {
164 $data->id = $current->id;
165 $DB->update_record('workshopallocation_scheduled', $data);
166 }
98621280
DM
167 }
168}