Commit | Line | Data |
---|---|---|
20836db9 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 | * Allows to choose a form from the list of available templates | |
20 | * | |
21 | * @package core | |
22 | * @subpackage grading | |
23 | * @copyright 2011 David Mudrak <david@moodle.com> | |
24 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
25 | */ | |
26 | ||
27 | require_once(dirname(dirname(dirname(__FILE__))).'/config.php'); | |
28 | require_once($CFG->dirroot.'/grade/grading/lib.php'); | |
4d064218 | 29 | require_once($CFG->dirroot.'/grade/grading/pick_form.php'); |
20836db9 DM |
30 | |
31 | $targetid = required_param('targetid', PARAM_INT); // area we are coming from | |
86e9ccfd DM |
32 | $pick = optional_param('pick', null, PARAM_INT); // create new form from this template |
33 | $remove = optional_param('remove', null, PARAM_INT); // remove this template | |
20836db9 DM |
34 | $confirmed = optional_param('confirmed', false, PARAM_BOOL); // is the action confirmed |
35 | ||
36 | // the manager of the target area | |
37 | $targetmanager = get_grading_manager($targetid); | |
38 | ||
39 | if ($targetmanager->get_context()->contextlevel < CONTEXT_COURSE) { | |
40 | throw new coding_exception('Unsupported gradable area context level'); | |
41 | } | |
42 | ||
43 | // currently active method in the target area | |
44 | $method = $targetmanager->get_active_method(); | |
45 | $targetcontroller = $targetmanager->get_controller($method); | |
46 | $targetcontrollerclass = get_class($targetcontroller); | |
47 | ||
48 | // make sure there is no such form defined in the target area | |
49 | if ($targetcontroller->is_form_defined()) { | |
9e3d352d | 50 | redirect(new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid))); |
20836db9 DM |
51 | } |
52 | ||
53 | list($context, $course, $cm) = get_context_info_array($targetmanager->get_context()->id); | |
54 | ||
55 | require_login($course, true, $cm); | |
56 | require_capability('moodle/grade:managegradingforms', $context); | |
57 | ||
86e9ccfd | 58 | // user's capability in the templates bank |
dd736a87 DM |
59 | $canshare = has_capability('moodle/grade:sharegradingforms', context_system::instance()); |
60 | $canmanage = has_capability('moodle/grade:managesharedforms', context_system::instance()); | |
86e9ccfd DM |
61 | |
62 | // setup the page | |
4d064218 | 63 | $PAGE->set_url(new moodle_url('/grade/grading/pick.php', array('targetid' => $targetid))); |
20836db9 DM |
64 | navigation_node::override_active_url($targetmanager->get_management_url()); |
65 | $PAGE->set_title(get_string('gradingmanagement', 'core_grading')); | |
66 | $PAGE->set_heading(get_string('gradingmanagement', 'core_grading')); | |
67 | $output = $PAGE->get_renderer('core_grading'); | |
68 | ||
86e9ccfd | 69 | // process picking a template |
20836db9 DM |
70 | if ($pick) { |
71 | $sourceid = $DB->get_field('grading_definitions', 'areaid', array('id' => $pick), MUST_EXIST); | |
72 | $sourcemanager = get_grading_manager($sourceid); | |
73 | $sourcecontroller = $sourcemanager->get_controller($method); | |
967d346f DM |
74 | if (!$sourcecontroller->is_shared_template() and !$sourcecontroller->is_own_form()) { |
75 | // note that we don't actually check whether the user has still the capability | |
76 | // moodle/grade:managegradingforms in the source area. so when users loose | |
77 | // their teacher role in a course, they can't access the course but they can | |
78 | // still copy the forms they have created there. | |
79 | throw new moodle_exception('attempt_to_pick_others_form', 'core_grading'); | |
80 | } | |
20836db9 DM |
81 | if (!$sourcecontroller->is_form_defined()) { |
82 | throw new moodle_exception('form_definition_mismatch', 'core_grading'); | |
83 | } | |
84 | $definition = $sourcecontroller->get_definition(); | |
85 | if (!$confirmed) { | |
86 | echo $output->header(); | |
87 | echo $output->confirm(get_string('templatepickconfirm', 'core_grading',array( | |
88 | 'formname' => s($definition->name), | |
89 | 'component' => $targetmanager->get_component_title(), | |
90 | 'area' => $targetmanager->get_area_title())), | |
91 | new moodle_url($PAGE->url, array('pick' => $pick, 'confirmed' => 1)), | |
92 | $PAGE->url); | |
93 | echo $output->box($sourcecontroller->render_preview($PAGE), 'template-preview-confirm'); | |
94 | echo $output->footer(); | |
95 | die(); | |
96 | } else { | |
97 | require_sesskey(); | |
3599b113 | 98 | $targetcontroller->update_definition($sourcecontroller->get_definition_copy($targetcontroller)); |
3f3ee711 | 99 | $DB->set_field('grading_definitions', 'timecopied', time(), array('id' => $definition->id)); |
20836db9 DM |
100 | redirect(new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid))); |
101 | } | |
102 | } | |
103 | ||
86e9ccfd DM |
104 | // process removing a template |
105 | if ($remove) { | |
106 | $sourceid = $DB->get_field('grading_definitions', 'areaid', array('id' => $remove), MUST_EXIST); | |
107 | $sourcemanager = get_grading_manager($sourceid); | |
108 | $sourcecontroller = $sourcemanager->get_controller($method); | |
967d346f DM |
109 | if (!$sourcecontroller->is_shared_template()) { |
110 | throw new moodle_exception('attempt_to_delete_nontemplate', 'core_grading'); | |
111 | } | |
86e9ccfd DM |
112 | if (!$sourcecontroller->is_form_defined()) { |
113 | throw new moodle_exception('form_definition_mismatch', 'core_grading'); | |
114 | } | |
115 | $definition = $sourcecontroller->get_definition(); | |
116 | if ($canmanage or ($canshare and ($definition->usercreated == $USER->id))) { | |
117 | // ok, this user can drop the template | |
118 | } else { | |
119 | throw new moodle_exception('no_permission_to_remove_template', 'core_grading'); | |
120 | } | |
121 | if (!$confirmed) { | |
122 | echo $output->header(); | |
123 | echo $output->confirm(get_string('templatedeleteconfirm', 'core_grading', s($definition->name)), | |
124 | new moodle_url($PAGE->url, array('remove' => $remove, 'confirmed' => 1)), | |
125 | $PAGE->url); | |
126 | echo $output->box($sourcecontroller->render_preview($PAGE), 'template-preview-confirm'); | |
127 | echo $output->footer(); | |
128 | die(); | |
129 | } else { | |
130 | require_sesskey(); | |
131 | $sourcecontroller->delete_definition(); | |
132 | redirect($PAGE->url); | |
133 | } | |
134 | } | |
135 | ||
20836db9 DM |
136 | $searchform = new grading_search_template_form($PAGE->url, null, 'GET', '', array('class' => 'templatesearchform')); |
137 | ||
138 | if ($searchdata = $searchform->get_data()) { | |
967d346f DM |
139 | $tokens = grading_manager::tokenize($searchdata->needle); |
140 | $includeownforms = (!empty($searchdata->mode)); | |
20836db9 | 141 | } else { |
967d346f DM |
142 | $tokens = array(); |
143 | $includeownforms = false; | |
20836db9 DM |
144 | } |
145 | ||
146 | // construct the SQL to find all matching templates | |
86e9ccfd DM |
147 | $sql = "SELECT DISTINCT gd.id, gd.areaid, gd.name, gd.description, gd.descriptionformat, gd.timecreated, |
148 | gd.usercreated, gd.timemodified, gd.usermodified | |
20836db9 | 149 | FROM {grading_definitions} gd |
c15b933c DM |
150 | JOIN {grading_areas} ga ON (gd.areaid = ga.id) |
151 | JOIN {context} cx ON (ga.contextid = cx.id)"; | |
20836db9 DM |
152 | // join method-specific tables from the plugin scope |
153 | $sql .= $targetcontrollerclass::sql_search_from_tables('gd.id'); | |
154 | ||
967d346f DM |
155 | $sql .= " WHERE gd.method = ?"; |
156 | ||
157 | $params = array($method); | |
158 | ||
159 | if (!$includeownforms) { | |
160 | // search for public templates only | |
161 | $sql .= " AND ga.contextid = ? AND ga.component = 'core_grading'"; | |
dd736a87 | 162 | $params[] = context_system::instance()->id; |
20836db9 | 163 | |
967d346f DM |
164 | } else { |
165 | // search both templates and own forms in other areas | |
166 | $sql .= " AND ((ga.contextid = ? AND ga.component = 'core_grading') | |
167 | OR (gd.usercreated = ? AND gd.status = ?))"; | |
dd736a87 | 168 | $params = array_merge($params, array(context_system::instance()->id, $USER->id, |
967d346f DM |
169 | gradingform_controller::DEFINITION_STATUS_READY)); |
170 | } | |
20836db9 | 171 | |
20836db9 DM |
172 | if ($tokens) { |
173 | $subsql = array(); | |
174 | ||
175 | // search for any of the tokens in the definition name | |
176 | foreach ($tokens as $token) { | |
177 | $subsql[] = $DB->sql_like('gd.name', '?', false, false); | |
178 | $params[] = '%'.$DB->sql_like_escape($token).'%'; | |
179 | } | |
180 | ||
181 | // search for any of the tokens in the definition description | |
182 | foreach ($tokens as $token) { | |
183 | $subsql[] = $DB->sql_like('gd.description', '?', false, false); | |
184 | $params[] = '%'.$DB->sql_like_escape($token).'%'; | |
185 | } | |
186 | ||
187 | // search for the needle in method-specific tables | |
188 | foreach ($tokens as $token) { | |
189 | list($methodsql, $methodparams) = $targetcontrollerclass::sql_search_where($token); | |
190 | $subsql = array_merge($subsql, $methodsql); | |
191 | $params = array_merge($params, $methodparams); | |
192 | } | |
193 | ||
194 | $sql .= " AND ((" . join(")\n OR (", $subsql) . "))"; | |
195 | } | |
196 | ||
197 | $sql .= " ORDER BY gd.name"; | |
198 | ||
199 | $rs = $DB->get_recordset_sql($sql, $params); | |
200 | ||
201 | echo $output->header(); | |
20836db9 DM |
202 | $searchform->display(); |
203 | ||
204 | $found = 0; | |
205 | foreach ($rs as $template) { | |
206 | $found++; | |
207 | $out = ''; | |
20836db9 DM |
208 | $manager = get_grading_manager($template->areaid); |
209 | $controller = $manager->get_controller($method); | |
967d346f DM |
210 | if ($controller->is_shared_template()) { |
211 | $templatetag = html_writer::tag('span', get_string('templatetypeshared', 'core_grading'), | |
212 | array('class' => 'type shared')); | |
213 | $templatesrc = ''; | |
214 | } else if ($controller->is_own_form()) { | |
215 | $templatetag = html_writer::tag('span', get_string('templatetypeown', 'core_grading'), | |
216 | array('class' => 'type ownform')); | |
217 | $templatesrc = get_string('templatesource', 'core_grading', array( | |
218 | 'component' => $manager->get_component_title(), | |
219 | 'area' => $manager->get_area_title())); | |
220 | } else { | |
221 | throw new coding_exception('Something is wrong, the displayed form must be either template or own form'); | |
222 | } | |
223 | $out .= $output->heading(s($template->name).' '.$templatetag, 2, 'template-name'); | |
224 | $out .= $output->container($templatesrc, 'template-source'); | |
20836db9 | 225 | $out .= $output->box($controller->render_preview($PAGE), 'template-preview'); |
967d346f DM |
226 | $actions = array(); |
227 | if ($controller->is_shared_template()) { | |
228 | $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('pick' => $template->id)), | |
229 | get_string('templatepick', 'core_grading'), 'i/tick_green_big', 'pick template'); | |
230 | if ($canmanage or ($canshare and ($template->usercreated == $USER->id))) { | |
231 | //$actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('edit' => $template->id)), | |
232 | // get_string('templateedit', 'core_grading'), 'i/edit', 'edit'); | |
233 | $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('remove' => $template->id)), | |
234 | get_string('templatedelete', 'core_grading'), 't/delete', 'remove'); | |
235 | } | |
236 | } else if ($controller->is_own_form()) { | |
237 | $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('pick' => $template->id)), | |
238 | get_string('templatepickownform', 'core_grading'), 'i/tick_green_big', 'pick ownform'); | |
86e9ccfd DM |
239 | } |
240 | $out .= $output->box(join(' ', $actions), 'template-actions'); | |
3599b113 | 241 | $out .= $output->box($controller->get_formatted_description(), 'template-description'); |
20836db9 DM |
242 | |
243 | // ideally we should highlight just the name, description and the fields | |
244 | // in the preview that were actually searched. to make our life easier, we | |
245 | // simply highlight the tokens everywhere they appear, even if that exact | |
246 | // piece was not searched. | |
247 | echo highlight(join(' ', $tokens), $out); | |
248 | } | |
249 | $rs->close(); | |
250 | ||
251 | if (!$found) { | |
3f3ee711 | 252 | echo $output->heading(get_string('nosharedformfound', 'core_grading')); |
20836db9 DM |
253 | } |
254 | ||
4d064218 DM |
255 | echo $output->single_button( |
256 | new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid)), | |
257 | get_string('back'), 'get'); | |
258 | ||
20836db9 DM |
259 | echo $output->footer(); |
260 | ||
261 | //////////////////////////////////////////////////////////////////////////////// | |
262 | ||
263 |