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