MDL-29108 Improved support for form definition status
[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();
7622ae95
DM
85 $targetcontroller->update_definition($sourcecontroller->get_definition_copy($targetcontroller),
86 gradingform_controller::DEFINITION_STATUS_READY);
20836db9
DM
87 redirect(new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid)));
88 }
89}
90
91$searchform = new grading_search_template_form($PAGE->url, null, 'GET', '', array('class' => 'templatesearchform'));
92
93if ($searchdata = $searchform->get_data()) {
94 $needle = $searchdata->needle;
95 $searchform->set_data(array(
96 'needle' => $needle,
97 ));
98} else {
99 $needle = '';
100}
101
102// construct the SQL to find all matching templates
103$sql = "SELECT DISTINCT gd.id, gd.areaid, gd.name, gd.description, gd.descriptionformat, gd.timecreated
104 FROM {grading_definitions} gd
105 JOIN {grading_areas} ga ON (gd.areaid = ga.id)";
106// join method-specific tables from the plugin scope
107$sql .= $targetcontrollerclass::sql_search_from_tables('gd.id');
108
109$sql .= " WHERE gd.method = ?
110 AND ga.contextid = ?
111 AND ga.component = 'core_grading'";
112
113$params = array($method, get_system_context()->id);
114
115$tokens = grading_manager::tokenize($needle);
116if ($tokens) {
117 $subsql = array();
118
119 // search for any of the tokens in the definition name
120 foreach ($tokens as $token) {
121 $subsql[] = $DB->sql_like('gd.name', '?', false, false);
122 $params[] = '%'.$DB->sql_like_escape($token).'%';
123 }
124
125 // search for any of the tokens in the definition description
126 foreach ($tokens as $token) {
127 $subsql[] = $DB->sql_like('gd.description', '?', false, false);
128 $params[] = '%'.$DB->sql_like_escape($token).'%';
129 }
130
131 // search for the needle in method-specific tables
132 foreach ($tokens as $token) {
133 list($methodsql, $methodparams) = $targetcontrollerclass::sql_search_where($token);
134 $subsql = array_merge($subsql, $methodsql);
135 $params = array_merge($params, $methodparams);
136 }
137
138 $sql .= " AND ((" . join(")\n OR (", $subsql) . "))";
139}
140
141$sql .= " ORDER BY gd.name";
142
143$rs = $DB->get_recordset_sql($sql, $params);
144
145echo $output->header();
146
147$searchform->display();
148
149$found = 0;
150foreach ($rs as $template) {
151 $found++;
152 $out = '';
153 $out .= $output->heading(s($template->name), 2, 'template-name');
154 $manager = get_grading_manager($template->areaid);
155 $controller = $manager->get_controller($method);
156 $out .= $output->box($controller->render_preview($PAGE), 'template-preview');
157 $out .= $output->box(join(' ', array(
158 $output->pick_action_icon(new moodle_url($PAGE->url, array('pick' => $template->id)),
159 get_string('templatepick', 'core_grading'), 'i/tick_green_big', 'pick'),
160 //$output->pick_action_icon(new moodle_url($PAGE->url, array('edit' => $template->id)),
161 // get_string('templateedit', 'core_grading'), 'i/edit', 'edit'),
162 //$output->pick_action_icon(new moodle_url($PAGE->url, array('remove' => $template->id)),
163 // get_string('templatedelete', 'core_grading'), 't/delete', 'edit'),
164 )), 'template-actions');
165 $out .= $output->box(format_text($template->description, $template->descriptionformat), 'template-description');
166
167 // ideally we should highlight just the name, description and the fields
168 // in the preview that were actually searched. to make our life easier, we
169 // simply highlight the tokens everywhere they appear, even if that exact
170 // piece was not searched.
171 echo highlight(join(' ', $tokens), $out);
172}
173$rs->close();
174
175if (!$found) {
176 echo $output->heading(get_string('nothingtodisplay'));
177}
178
179echo $output->footer();
180
181////////////////////////////////////////////////////////////////////////////////
182
183