3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Allows to choose a form from the list of available templates
23 * @copyright 2011 David Mudrak <david@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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/pick_form.php');
31 $targetid = required_param('targetid', PARAM_INT); // area we are coming from
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
34 $confirmed = optional_param('confirmed', false, PARAM_BOOL); // is the action confirmed
36 // the manager of the target area
37 $targetmanager = get_grading_manager($targetid);
39 if ($targetmanager->get_context()->contextlevel < CONTEXT_COURSE) {
40 throw new coding_exception('Unsupported gradable area context level');
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);
48 // make sure there is no such form defined in the target area
49 if ($targetcontroller->is_form_defined()) {
50 redirect(new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid)));
53 list($context, $course, $cm) = get_context_info_array($targetmanager->get_context()->id);
55 require_login($course, true, $cm);
56 require_capability('moodle/grade:managegradingforms', $context);
58 // user's capability in the templates bank
59 $canshare = has_capability('moodle/grade:sharegradingforms', context_system::instance());
60 $canmanage = has_capability('moodle/grade:managesharedforms', context_system::instance());
63 $PAGE->set_url(new moodle_url('/grade/grading/pick.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');
69 // process picking a template
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_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');
81 if (!$sourcecontroller->is_form_defined()) {
82 throw new moodle_exception('form_definition_mismatch', 'core_grading');
84 $definition = $sourcecontroller->get_definition();
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)),
93 echo $output->box($sourcecontroller->render_preview($PAGE), 'template-preview-confirm');
94 echo $output->footer();
98 $targetcontroller->update_definition($sourcecontroller->get_definition_copy($targetcontroller));
99 $DB->set_field('grading_definitions', 'timecopied', time(), array('id' => $definition->id));
100 redirect(new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid)));
104 // process removing a template
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);
109 if (!$sourcecontroller->is_shared_template()) {
110 throw new moodle_exception('attempt_to_delete_nontemplate', 'core_grading');
112 if (!$sourcecontroller->is_form_defined()) {
113 throw new moodle_exception('form_definition_mismatch', 'core_grading');
115 $definition = $sourcecontroller->get_definition();
116 if ($canmanage or ($canshare and ($definition->usercreated == $USER->id))) {
117 // ok, this user can drop the template
119 throw new moodle_exception('no_permission_to_remove_template', 'core_grading');
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)),
126 echo $output->box($sourcecontroller->render_preview($PAGE), 'template-preview-confirm');
127 echo $output->footer();
131 $sourcecontroller->delete_definition();
132 redirect($PAGE->url);
136 $searchform = new grading_search_template_form($PAGE->url, null, 'GET', '', array('class' => 'templatesearchform'));
138 if ($searchdata = $searchform->get_data()) {
139 $tokens = grading_manager::tokenize($searchdata->needle);
140 $includeownforms = (!empty($searchdata->mode));
143 $includeownforms = false;
146 // construct the SQL to find all matching templates
147 $sql = "SELECT DISTINCT gd.id, gd.areaid, gd.name, gd.description, gd.descriptionformat, gd.timecreated,
148 gd.usercreated, gd.timemodified, gd.usermodified
149 FROM {grading_definitions} gd
150 JOIN {grading_areas} ga ON (gd.areaid = ga.id)";
151 // join method-specific tables from the plugin scope
152 $sql .= $targetcontrollerclass::sql_search_from_tables('gd.id');
154 $sql .= " WHERE gd.method = ?";
156 $params = array($method);
158 if (!$includeownforms) {
159 // search for public templates only
160 $sql .= " AND ga.contextid = ? AND ga.component = 'core_grading'";
161 $params[] = context_system::instance()->id;
164 // search both templates and own forms in other areas
165 $sql .= " AND ((ga.contextid = ? AND ga.component = 'core_grading')
166 OR (gd.usercreated = ? AND gd.status = ?))";
167 $params = array_merge($params, array(context_system::instance()->id, $USER->id,
168 gradingform_controller::DEFINITION_STATUS_READY));
174 // search for any of the tokens in the definition name
175 foreach ($tokens as $token) {
176 $subsql[] = $DB->sql_like('gd.name', '?', false, false);
177 $params[] = '%'.$DB->sql_like_escape($token).'%';
180 // search for any of the tokens in the definition description
181 foreach ($tokens as $token) {
182 $subsql[] = $DB->sql_like('gd.description', '?', false, false);
183 $params[] = '%'.$DB->sql_like_escape($token).'%';
186 // search for the needle in method-specific tables
187 foreach ($tokens as $token) {
188 list($methodsql, $methodparams) = $targetcontrollerclass::sql_search_where($token);
189 $subsql = array_merge($subsql, $methodsql);
190 $params = array_merge($params, $methodparams);
193 $sql .= " AND ((" . join(")\n OR (", $subsql) . "))";
196 $sql .= " ORDER BY gd.name";
198 $rs = $DB->get_recordset_sql($sql, $params);
200 echo $output->header();
201 $searchform->display();
204 foreach ($rs as $template) {
207 $manager = get_grading_manager($template->areaid);
208 $controller = $manager->get_controller($method);
209 if ($controller->is_shared_template()) {
210 $templatetag = html_writer::tag('span', get_string('templatetypeshared', 'core_grading'),
211 array('class' => 'type shared'));
213 } else if ($controller->is_own_form()) {
214 $templatetag = html_writer::tag('span', get_string('templatetypeown', 'core_grading'),
215 array('class' => 'type ownform'));
216 $templatesrc = get_string('templatesource', 'core_grading', array(
217 'component' => $manager->get_component_title(),
218 'area' => $manager->get_area_title()));
220 throw new coding_exception('Something is wrong, the displayed form must be either template or own form');
222 $out .= $output->heading(s($template->name).' '.$templatetag, 2, 'template-name');
223 $out .= $output->container($templatesrc, 'template-source');
224 $out .= $output->box($controller->render_preview($PAGE), 'template-preview');
226 if ($controller->is_shared_template()) {
227 $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('pick' => $template->id)),
228 get_string('templatepick', 'core_grading'), 'i/tick_green_big', 'pick template');
229 if ($canmanage or ($canshare and ($template->usercreated == $USER->id))) {
230 //$actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('edit' => $template->id)),
231 // get_string('templateedit', 'core_grading'), 'i/edit', 'edit');
232 $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('remove' => $template->id)),
233 get_string('templatedelete', 'core_grading'), 't/delete', 'remove');
235 } else if ($controller->is_own_form()) {
236 $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('pick' => $template->id)),
237 get_string('templatepickownform', 'core_grading'), 'i/tick_green_big', 'pick ownform');
239 $out .= $output->box(join(' ', $actions), 'template-actions');
240 $out .= $output->box($controller->get_formatted_description(), 'template-description');
242 // ideally we should highlight just the name, description and the fields
243 // in the preview that were actually searched. to make our life easier, we
244 // simply highlight the tokens everywhere they appear, even if that exact
245 // piece was not searched.
246 echo highlight(join(' ', $tokens), $out);
251 echo $output->heading(get_string('nosharedformfound', 'core_grading'));
254 echo $output->single_button(
255 new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid)),
256 get_string('back'), 'get');
258 echo $output->footer();
260 ////////////////////////////////////////////////////////////////////////////////