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()) { | |
50 | throw new moodle_exception('target_defined', 'core_grading'); | |
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 DM |
58 | // user's capability in the templates bank |
59 | $canshare = has_capability('moodle/grade:sharegradingforms', get_system_context()); | |
60 | $canmanage = has_capability('moodle/grade:managesharedforms', get_system_context()); | |
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); | |
74 | if (!$sourcecontroller->is_form_defined()) { | |
75 | throw new moodle_exception('form_definition_mismatch', 'core_grading'); | |
76 | } | |
77 | $definition = $sourcecontroller->get_definition(); | |
78 | if (!$confirmed) { | |
79 | echo $output->header(); | |
80 | echo $output->confirm(get_string('templatepickconfirm', 'core_grading',array( | |
81 | 'formname' => s($definition->name), | |
82 | 'component' => $targetmanager->get_component_title(), | |
83 | 'area' => $targetmanager->get_area_title())), | |
84 | new moodle_url($PAGE->url, array('pick' => $pick, 'confirmed' => 1)), | |
85 | $PAGE->url); | |
86 | echo $output->box($sourcecontroller->render_preview($PAGE), 'template-preview-confirm'); | |
87 | echo $output->footer(); | |
88 | die(); | |
89 | } else { | |
90 | require_sesskey(); | |
3599b113 | 91 | $targetcontroller->update_definition($sourcecontroller->get_definition_copy($targetcontroller)); |
3f3ee711 | 92 | $DB->set_field('grading_definitions', 'timecopied', time(), array('id' => $definition->id)); |
20836db9 DM |
93 | redirect(new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid))); |
94 | } | |
95 | } | |
96 | ||
86e9ccfd DM |
97 | // process removing a template |
98 | if ($remove) { | |
99 | $sourceid = $DB->get_field('grading_definitions', 'areaid', array('id' => $remove), MUST_EXIST); | |
100 | $sourcemanager = get_grading_manager($sourceid); | |
101 | $sourcecontroller = $sourcemanager->get_controller($method); | |
102 | if (!$sourcecontroller->is_form_defined()) { | |
103 | throw new moodle_exception('form_definition_mismatch', 'core_grading'); | |
104 | } | |
105 | $definition = $sourcecontroller->get_definition(); | |
106 | if ($canmanage or ($canshare and ($definition->usercreated == $USER->id))) { | |
107 | // ok, this user can drop the template | |
108 | } else { | |
109 | throw new moodle_exception('no_permission_to_remove_template', 'core_grading'); | |
110 | } | |
111 | if (!$confirmed) { | |
112 | echo $output->header(); | |
113 | echo $output->confirm(get_string('templatedeleteconfirm', 'core_grading', s($definition->name)), | |
114 | new moodle_url($PAGE->url, array('remove' => $remove, 'confirmed' => 1)), | |
115 | $PAGE->url); | |
116 | echo $output->box($sourcecontroller->render_preview($PAGE), 'template-preview-confirm'); | |
117 | echo $output->footer(); | |
118 | die(); | |
119 | } else { | |
120 | require_sesskey(); | |
121 | $sourcecontroller->delete_definition(); | |
122 | redirect($PAGE->url); | |
123 | } | |
124 | } | |
125 | ||
20836db9 DM |
126 | $searchform = new grading_search_template_form($PAGE->url, null, 'GET', '', array('class' => 'templatesearchform')); |
127 | ||
128 | if ($searchdata = $searchform->get_data()) { | |
129 | $needle = $searchdata->needle; | |
130 | $searchform->set_data(array( | |
131 | 'needle' => $needle, | |
132 | )); | |
133 | } else { | |
134 | $needle = ''; | |
135 | } | |
136 | ||
137 | // construct the SQL to find all matching templates | |
86e9ccfd DM |
138 | $sql = "SELECT DISTINCT gd.id, gd.areaid, gd.name, gd.description, gd.descriptionformat, gd.timecreated, |
139 | gd.usercreated, gd.timemodified, gd.usermodified | |
20836db9 DM |
140 | FROM {grading_definitions} gd |
141 | JOIN {grading_areas} ga ON (gd.areaid = ga.id)"; | |
142 | // join method-specific tables from the plugin scope | |
143 | $sql .= $targetcontrollerclass::sql_search_from_tables('gd.id'); | |
144 | ||
145 | $sql .= " WHERE gd.method = ? | |
146 | AND ga.contextid = ? | |
147 | AND ga.component = 'core_grading'"; | |
148 | ||
149 | $params = array($method, get_system_context()->id); | |
150 | ||
151 | $tokens = grading_manager::tokenize($needle); | |
152 | if ($tokens) { | |
153 | $subsql = array(); | |
154 | ||
155 | // search for any of the tokens in the definition name | |
156 | foreach ($tokens as $token) { | |
157 | $subsql[] = $DB->sql_like('gd.name', '?', false, false); | |
158 | $params[] = '%'.$DB->sql_like_escape($token).'%'; | |
159 | } | |
160 | ||
161 | // search for any of the tokens in the definition description | |
162 | foreach ($tokens as $token) { | |
163 | $subsql[] = $DB->sql_like('gd.description', '?', false, false); | |
164 | $params[] = '%'.$DB->sql_like_escape($token).'%'; | |
165 | } | |
166 | ||
167 | // search for the needle in method-specific tables | |
168 | foreach ($tokens as $token) { | |
169 | list($methodsql, $methodparams) = $targetcontrollerclass::sql_search_where($token); | |
170 | $subsql = array_merge($subsql, $methodsql); | |
171 | $params = array_merge($params, $methodparams); | |
172 | } | |
173 | ||
174 | $sql .= " AND ((" . join(")\n OR (", $subsql) . "))"; | |
175 | } | |
176 | ||
177 | $sql .= " ORDER BY gd.name"; | |
178 | ||
179 | $rs = $DB->get_recordset_sql($sql, $params); | |
180 | ||
181 | echo $output->header(); | |
20836db9 DM |
182 | $searchform->display(); |
183 | ||
184 | $found = 0; | |
185 | foreach ($rs as $template) { | |
186 | $found++; | |
187 | $out = ''; | |
188 | $out .= $output->heading(s($template->name), 2, 'template-name'); | |
189 | $manager = get_grading_manager($template->areaid); | |
190 | $controller = $manager->get_controller($method); | |
191 | $out .= $output->box($controller->render_preview($PAGE), 'template-preview'); | |
86e9ccfd DM |
192 | $actions = array($output->pick_action_icon(new moodle_url($PAGE->url, array('pick' => $template->id)), |
193 | get_string('templatepick', 'core_grading'), 'i/tick_green_big', 'pick')); | |
194 | if ($canmanage or ($canshare and ($template->usercreated == $USER->id))) { | |
195 | //$actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('edit' => $template->id)), | |
196 | // get_string('templateedit', 'core_grading'), 'i/edit', 'edit'); | |
197 | $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('remove' => $template->id)), | |
198 | get_string('templatedelete', 'core_grading'), 't/delete', 'remove'); | |
199 | } | |
200 | $out .= $output->box(join(' ', $actions), 'template-actions'); | |
3599b113 | 201 | $out .= $output->box($controller->get_formatted_description(), 'template-description'); |
20836db9 DM |
202 | |
203 | // ideally we should highlight just the name, description and the fields | |
204 | // in the preview that were actually searched. to make our life easier, we | |
205 | // simply highlight the tokens everywhere they appear, even if that exact | |
206 | // piece was not searched. | |
207 | echo highlight(join(' ', $tokens), $out); | |
208 | } | |
209 | $rs->close(); | |
210 | ||
211 | if (!$found) { | |
3f3ee711 | 212 | echo $output->heading(get_string('nosharedformfound', 'core_grading')); |
20836db9 DM |
213 | } |
214 | ||
4d064218 DM |
215 | echo $output->single_button( |
216 | new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid)), | |
217 | get_string('back'), 'get'); | |
218 | ||
20836db9 DM |
219 | echo $output->footer(); |
220 | ||
221 | //////////////////////////////////////////////////////////////////////////////// | |
222 | ||
223 |