MDL-29794 Initial support for re-using a shared grading form
[moodle.git] / grade / grading / templates.php
CommitLineData
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
27require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
28require_once($CFG->dirroot.'/grade/grading/lib.php');
29require_once($CFG->dirroot.'/grade/grading/templates_form.php');
30
31$targetid = required_param('targetid', PARAM_INT); // area we are coming from
32$pick = optional_param('pick', null, PARAM_INT); // use this form
33$confirmed = optional_param('confirmed', false, PARAM_BOOL); // is the action confirmed
34
35// the manager of the target area
36$targetmanager = get_grading_manager($targetid);
37
38if ($targetmanager->get_context()->contextlevel < CONTEXT_COURSE) {
39 throw new coding_exception('Unsupported gradable area context level');
40}
41
42// currently active method in the target area
43$method = $targetmanager->get_active_method();
44$targetcontroller = $targetmanager->get_controller($method);
45$targetcontrollerclass = get_class($targetcontroller);
46
47// make sure there is no such form defined in the target area
48if ($targetcontroller->is_form_defined()) {
49 throw new moodle_exception('target_defined', 'core_grading');
50}
51
52list($context, $course, $cm) = get_context_info_array($targetmanager->get_context()->id);
53
54require_login($course, true, $cm);
55require_capability('moodle/grade:managegradingforms', $context);
56
57$PAGE->set_url(new moodle_url('/grade/grading/templates.php', array('targetid' => $targetid)));
58navigation_node::override_active_url($targetmanager->get_management_url());
59$PAGE->set_title(get_string('gradingmanagement', 'core_grading'));
60$PAGE->set_heading(get_string('gradingmanagement', 'core_grading'));
61$output = $PAGE->get_renderer('core_grading');
62
63// process template actions
64if ($pick) {
65 $sourceid = $DB->get_field('grading_definitions', 'areaid', array('id' => $pick), MUST_EXIST);
66 $sourcemanager = get_grading_manager($sourceid);
67 $sourcecontroller = $sourcemanager->get_controller($method);
68 if (!$sourcecontroller->is_form_defined()) {
69 throw new moodle_exception('form_definition_mismatch', 'core_grading');
70 }
71 $definition = $sourcecontroller->get_definition();
72 if (!$confirmed) {
73 echo $output->header();
74 echo $output->confirm(get_string('templatepickconfirm', 'core_grading',array(
75 'formname' => s($definition->name),
76 'component' => $targetmanager->get_component_title(),
77 'area' => $targetmanager->get_area_title())),
78 new moodle_url($PAGE->url, array('pick' => $pick, 'confirmed' => 1)),
79 $PAGE->url);
80 echo $output->box($sourcecontroller->render_preview($PAGE), 'template-preview-confirm');
81 echo $output->footer();
82 die();
83 } else {
84 require_sesskey();
85 $targetcontroller->update_definition($sourcecontroller->get_definition_copy($targetcontroller));
86 redirect(new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid)));
87 }
88}
89
90$searchform = new grading_search_template_form($PAGE->url, null, 'GET', '', array('class' => 'templatesearchform'));
91
92if ($searchdata = $searchform->get_data()) {
93 $needle = $searchdata->needle;
94 $searchform->set_data(array(
95 'needle' => $needle,
96 ));
97} else {
98 $needle = '';
99}
100
101// construct the SQL to find all matching templates
102$sql = "SELECT DISTINCT gd.id, gd.areaid, gd.name, gd.description, gd.descriptionformat, gd.timecreated
103 FROM {grading_definitions} gd
104 JOIN {grading_areas} ga ON (gd.areaid = ga.id)";
105// join method-specific tables from the plugin scope
106$sql .= $targetcontrollerclass::sql_search_from_tables('gd.id');
107
108$sql .= " WHERE gd.method = ?
109 AND ga.contextid = ?
110 AND ga.component = 'core_grading'";
111
112$params = array($method, get_system_context()->id);
113
114$tokens = grading_manager::tokenize($needle);
115if ($tokens) {
116 $subsql = array();
117
118 // search for any of the tokens in the definition name
119 foreach ($tokens as $token) {
120 $subsql[] = $DB->sql_like('gd.name', '?', false, false);
121 $params[] = '%'.$DB->sql_like_escape($token).'%';
122 }
123
124 // search for any of the tokens in the definition description
125 foreach ($tokens as $token) {
126 $subsql[] = $DB->sql_like('gd.description', '?', false, false);
127 $params[] = '%'.$DB->sql_like_escape($token).'%';
128 }
129
130 // search for the needle in method-specific tables
131 foreach ($tokens as $token) {
132 list($methodsql, $methodparams) = $targetcontrollerclass::sql_search_where($token);
133 $subsql = array_merge($subsql, $methodsql);
134 $params = array_merge($params, $methodparams);
135 }
136
137 $sql .= " AND ((" . join(")\n OR (", $subsql) . "))";
138}
139
140$sql .= " ORDER BY gd.name";
141
142$rs = $DB->get_recordset_sql($sql, $params);
143
144echo $output->header();
145
146$searchform->display();
147
148$found = 0;
149foreach ($rs as $template) {
150 $found++;
151 $out = '';
152 $out .= $output->heading(s($template->name), 2, 'template-name');
153 $manager = get_grading_manager($template->areaid);
154 $controller = $manager->get_controller($method);
155 $out .= $output->box($controller->render_preview($PAGE), 'template-preview');
156 $out .= $output->box(join(' ', array(
157 $output->pick_action_icon(new moodle_url($PAGE->url, array('pick' => $template->id)),
158 get_string('templatepick', 'core_grading'), 'i/tick_green_big', 'pick'),
159 //$output->pick_action_icon(new moodle_url($PAGE->url, array('edit' => $template->id)),
160 // get_string('templateedit', 'core_grading'), 'i/edit', 'edit'),
161 //$output->pick_action_icon(new moodle_url($PAGE->url, array('remove' => $template->id)),
162 // get_string('templatedelete', 'core_grading'), 't/delete', 'edit'),
163 )), 'template-actions');
164 $out .= $output->box(format_text($template->description, $template->descriptionformat), 'template-description');
165
166 // ideally we should highlight just the name, description and the fields
167 // in the preview that were actually searched. to make our life easier, we
168 // simply highlight the tokens everywhere they appear, even if that exact
169 // piece was not searched.
170 echo highlight(join(' ', $tokens), $out);
171}
172$rs->close();
173
174if (!$found) {
175 echo $output->heading(get_string('nothingtodisplay'));
176}
177
178echo $output->footer();
179
180////////////////////////////////////////////////////////////////////////////////
181
182