MDL-31873 added mod_assign_get_grades web service function
[moodle.git] / grade / grading / pick.php
CommitLineData
20836db9 1<?php
20836db9
DM
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Allows to choose a form from the list of available templates
19 *
a153c9f2 20 * @package core_grades
20836db9
DM
21 * @subpackage grading
22 * @copyright 2011 David Mudrak <david@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
27require_once($CFG->dirroot.'/grade/grading/lib.php');
4d064218 28require_once($CFG->dirroot.'/grade/grading/pick_form.php');
20836db9
DM
29
30$targetid = required_param('targetid', PARAM_INT); // area we are coming from
86e9ccfd
DM
31$pick = optional_param('pick', null, PARAM_INT); // create new form from this template
32$remove = optional_param('remove', null, PARAM_INT); // remove this template
20836db9
DM
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()) {
9e3d352d 49 redirect(new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid)));
20836db9
DM
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
86e9ccfd 57// user's capability in the templates bank
dd736a87
DM
58$canshare = has_capability('moodle/grade:sharegradingforms', context_system::instance());
59$canmanage = has_capability('moodle/grade:managesharedforms', context_system::instance());
86e9ccfd
DM
60
61// setup the page
4d064218 62$PAGE->set_url(new moodle_url('/grade/grading/pick.php', array('targetid' => $targetid)));
20836db9
DM
63navigation_node::override_active_url($targetmanager->get_management_url());
64$PAGE->set_title(get_string('gradingmanagement', 'core_grading'));
65$PAGE->set_heading(get_string('gradingmanagement', 'core_grading'));
66$output = $PAGE->get_renderer('core_grading');
67
86e9ccfd 68// process picking a template
20836db9
DM
69if ($pick) {
70 $sourceid = $DB->get_field('grading_definitions', 'areaid', array('id' => $pick), MUST_EXIST);
71 $sourcemanager = get_grading_manager($sourceid);
72 $sourcecontroller = $sourcemanager->get_controller($method);
967d346f
DM
73 if (!$sourcecontroller->is_shared_template() and !$sourcecontroller->is_own_form()) {
74 // note that we don't actually check whether the user has still the capability
75 // moodle/grade:managegradingforms in the source area. so when users loose
76 // their teacher role in a course, they can't access the course but they can
77 // still copy the forms they have created there.
78 throw new moodle_exception('attempt_to_pick_others_form', 'core_grading');
79 }
20836db9
DM
80 if (!$sourcecontroller->is_form_defined()) {
81 throw new moodle_exception('form_definition_mismatch', 'core_grading');
82 }
83 $definition = $sourcecontroller->get_definition();
84 if (!$confirmed) {
85 echo $output->header();
86 echo $output->confirm(get_string('templatepickconfirm', 'core_grading',array(
87 'formname' => s($definition->name),
88 'component' => $targetmanager->get_component_title(),
89 'area' => $targetmanager->get_area_title())),
90 new moodle_url($PAGE->url, array('pick' => $pick, 'confirmed' => 1)),
91 $PAGE->url);
92 echo $output->box($sourcecontroller->render_preview($PAGE), 'template-preview-confirm');
93 echo $output->footer();
94 die();
95 } else {
96 require_sesskey();
3599b113 97 $targetcontroller->update_definition($sourcecontroller->get_definition_copy($targetcontroller));
3f3ee711 98 $DB->set_field('grading_definitions', 'timecopied', time(), array('id' => $definition->id));
20836db9
DM
99 redirect(new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid)));
100 }
101}
102
86e9ccfd
DM
103// process removing a template
104if ($remove) {
105 $sourceid = $DB->get_field('grading_definitions', 'areaid', array('id' => $remove), MUST_EXIST);
106 $sourcemanager = get_grading_manager($sourceid);
107 $sourcecontroller = $sourcemanager->get_controller($method);
967d346f
DM
108 if (!$sourcecontroller->is_shared_template()) {
109 throw new moodle_exception('attempt_to_delete_nontemplate', 'core_grading');
110 }
86e9ccfd
DM
111 if (!$sourcecontroller->is_form_defined()) {
112 throw new moodle_exception('form_definition_mismatch', 'core_grading');
113 }
114 $definition = $sourcecontroller->get_definition();
115 if ($canmanage or ($canshare and ($definition->usercreated == $USER->id))) {
116 // ok, this user can drop the template
117 } else {
118 throw new moodle_exception('no_permission_to_remove_template', 'core_grading');
119 }
120 if (!$confirmed) {
121 echo $output->header();
122 echo $output->confirm(get_string('templatedeleteconfirm', 'core_grading', s($definition->name)),
123 new moodle_url($PAGE->url, array('remove' => $remove, 'confirmed' => 1)),
124 $PAGE->url);
125 echo $output->box($sourcecontroller->render_preview($PAGE), 'template-preview-confirm');
126 echo $output->footer();
127 die();
128 } else {
129 require_sesskey();
130 $sourcecontroller->delete_definition();
131 redirect($PAGE->url);
132 }
133}
134
20836db9
DM
135$searchform = new grading_search_template_form($PAGE->url, null, 'GET', '', array('class' => 'templatesearchform'));
136
137if ($searchdata = $searchform->get_data()) {
967d346f
DM
138 $tokens = grading_manager::tokenize($searchdata->needle);
139 $includeownforms = (!empty($searchdata->mode));
20836db9 140} else {
967d346f
DM
141 $tokens = array();
142 $includeownforms = false;
20836db9
DM
143}
144
145// construct the SQL to find all matching templates
96ef951d 146$sql = "SELECT DISTINCT gd.id, gd.areaid, gd.name, gd.usercreated
20836db9 147 FROM {grading_definitions} gd
c15b933c
DM
148 JOIN {grading_areas} ga ON (gd.areaid = ga.id)
149 JOIN {context} cx ON (ga.contextid = cx.id)";
20836db9
DM
150// join method-specific tables from the plugin scope
151$sql .= $targetcontrollerclass::sql_search_from_tables('gd.id');
152
967d346f
DM
153$sql .= " WHERE gd.method = ?";
154
155$params = array($method);
156
157if (!$includeownforms) {
158 // search for public templates only
159 $sql .= " AND ga.contextid = ? AND ga.component = 'core_grading'";
dd736a87 160 $params[] = context_system::instance()->id;
20836db9 161
967d346f
DM
162} else {
163 // search both templates and own forms in other areas
164 $sql .= " AND ((ga.contextid = ? AND ga.component = 'core_grading')
165 OR (gd.usercreated = ? AND gd.status = ?))";
dd736a87 166 $params = array_merge($params, array(context_system::instance()->id, $USER->id,
967d346f
DM
167 gradingform_controller::DEFINITION_STATUS_READY));
168}
20836db9 169
20836db9
DM
170if ($tokens) {
171 $subsql = array();
172
173 // search for any of the tokens in the definition name
174 foreach ($tokens as $token) {
175 $subsql[] = $DB->sql_like('gd.name', '?', false, false);
176 $params[] = '%'.$DB->sql_like_escape($token).'%';
177 }
178
179 // search for any of the tokens in the definition description
180 foreach ($tokens as $token) {
181 $subsql[] = $DB->sql_like('gd.description', '?', false, false);
182 $params[] = '%'.$DB->sql_like_escape($token).'%';
183 }
184
185 // search for the needle in method-specific tables
186 foreach ($tokens as $token) {
187 list($methodsql, $methodparams) = $targetcontrollerclass::sql_search_where($token);
188 $subsql = array_merge($subsql, $methodsql);
189 $params = array_merge($params, $methodparams);
190 }
191
192 $sql .= " AND ((" . join(")\n OR (", $subsql) . "))";
193}
194
195$sql .= " ORDER BY gd.name";
196
197$rs = $DB->get_recordset_sql($sql, $params);
198
199echo $output->header();
20836db9
DM
200$searchform->display();
201
202$found = 0;
203foreach ($rs as $template) {
204 $found++;
205 $out = '';
20836db9
DM
206 $manager = get_grading_manager($template->areaid);
207 $controller = $manager->get_controller($method);
967d346f
DM
208 if ($controller->is_shared_template()) {
209 $templatetag = html_writer::tag('span', get_string('templatetypeshared', 'core_grading'),
210 array('class' => 'type shared'));
211 $templatesrc = '';
212 } else if ($controller->is_own_form()) {
213 $templatetag = html_writer::tag('span', get_string('templatetypeown', 'core_grading'),
214 array('class' => 'type ownform'));
215 $templatesrc = get_string('templatesource', 'core_grading', array(
216 'component' => $manager->get_component_title(),
217 'area' => $manager->get_area_title()));
218 } else {
219 throw new coding_exception('Something is wrong, the displayed form must be either template or own form');
220 }
221 $out .= $output->heading(s($template->name).' '.$templatetag, 2, 'template-name');
222 $out .= $output->container($templatesrc, 'template-source');
20836db9 223 $out .= $output->box($controller->render_preview($PAGE), 'template-preview');
967d346f
DM
224 $actions = array();
225 if ($controller->is_shared_template()) {
226 $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('pick' => $template->id)),
227 get_string('templatepick', 'core_grading'), 'i/tick_green_big', 'pick template');
228 if ($canmanage or ($canshare and ($template->usercreated == $USER->id))) {
229 //$actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('edit' => $template->id)),
230 // get_string('templateedit', 'core_grading'), 'i/edit', 'edit');
231 $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('remove' => $template->id)),
232 get_string('templatedelete', 'core_grading'), 't/delete', 'remove');
233 }
234 } else if ($controller->is_own_form()) {
235 $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('pick' => $template->id)),
236 get_string('templatepickownform', 'core_grading'), 'i/tick_green_big', 'pick ownform');
86e9ccfd
DM
237 }
238 $out .= $output->box(join(' ', $actions), 'template-actions');
3599b113 239 $out .= $output->box($controller->get_formatted_description(), 'template-description');
20836db9
DM
240
241 // ideally we should highlight just the name, description and the fields
242 // in the preview that were actually searched. to make our life easier, we
243 // simply highlight the tokens everywhere they appear, even if that exact
244 // piece was not searched.
245 echo highlight(join(' ', $tokens), $out);
246}
247$rs->close();
248
249if (!$found) {
3f3ee711 250 echo $output->heading(get_string('nosharedformfound', 'core_grading'));
20836db9
DM
251}
252
4d064218
DM
253echo $output->single_button(
254 new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid)),
255 get_string('back'), 'get');
256
20836db9
DM
257echo $output->footer();
258
259////////////////////////////////////////////////////////////////////////////////
260
261