Saving a form as a public template
[moodle.git] / grade / grading / manage.php
CommitLineData
6832a102
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 * @package core
20 * @subpackage grading
21 * @copyright 2011 David Mudrak <david@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
26require_once($CFG->dirroot.'/grade/grading/lib.php');
27
fde33804 28// identify gradable area by its id
71bd9d20 29$areaid = optional_param('areaid', null, PARAM_INT);
fde33804 30// alternatively the context, component and areaname must be provided
71bd9d20
DM
31$contextid = optional_param('contextid', null, PARAM_INT);
32$component = optional_param('component', null, PARAM_COMPONENT);
33$area = optional_param('area', null, PARAM_AREA);
fde33804 34// keep the caller's URL so that we know where to send the user finally
71bd9d20 35$returnurl = optional_param('returnurl', null, PARAM_LOCALURL);
fde33804 36// active method selector
71bd9d20 37$setmethod = optional_param('setmethod', null, PARAM_PLUGIN);
fde33804
DM
38// publish the given form definition as a new template in the forms bank
39$shareform = optional_param('shareform', null, PARAM_INT);
40// consider the required action as confirmed
41$confirmed = optional_param('confirmed', false, PARAM_BOOL);
42// a message to display, typically a previous action's result
43$message = optional_param('message', null, PARAM_NOTAGS);
6832a102
DM
44
45if (!is_null($areaid)) {
46 // get manager by id
47 $manager = get_grading_manager($areaid);
48} else {
49 // get manager by context and component
98a73bb7 50 if (is_null($contextid) or is_null($component) or is_null($area)) {
6832a102
DM
51 throw new coding_exception('The caller script must identify the gradable area.');
52 }
53 $context = get_context_instance_by_id($contextid, MUST_EXIST);
54 $manager = get_grading_manager($context, $component, $area);
55}
56
98a73bb7
DM
57// currently active method
58$method = $manager->get_active_method();
59
fde33804
DM
60if ($manager->get_context()->contextlevel == CONTEXT_SYSTEM) {
61 // this is a shared area in the forms bank, redirect the user
62 $params = array('areaid' =>$areaid);
63 if (!is_null($returnurl)) {
64 $params['returnurl'] = $returnurl;
65 }
66 redirect(new moodle_url('/grade/grading/bank.php', $params));
67
68} else if ($manager->get_context()->contextlevel >= CONTEXT_COURSE) {
69 list($context, $course, $cm) = get_context_info_array($manager->get_context()->id);
70
71 if (is_null($returnurl)) {
72 $returnurl = new moodle_url('/course/view.php', array('id' => $course->id));
73 } else {
74 $returnurl = new moodle_url($returnurl);
75 }
76
77 require_login($course, true, $cm);
78 require_capability('moodle/grade:managegradingforms', $context);
6832a102 79
6832a102 80} else {
fde33804 81 throw new coding_exception('Unsupported gradable area context level');
6832a102
DM
82}
83
98a73bb7 84$PAGE->set_url($manager->get_management_url($returnurl));
6832a102
DM
85navigation_node::override_active_url($manager->get_management_url());
86$PAGE->set_title(get_string('gradingmanagement', 'core_grading'));
87$PAGE->set_heading(get_string('gradingmanagement', 'core_grading'));
88$output = $PAGE->get_renderer('core_grading');
89
6832a102 90// process the eventual change of the active grading method
71bd9d20 91if (!empty($setmethod)) {
6832a102 92 require_sesskey();
71bd9d20 93 if ($setmethod == 'none') {
6832a102 94 // here we expect that noone would actually want to call their plugin as 'none'
71bd9d20 95 $setmethod = null;
6832a102 96 }
71bd9d20 97 $manager->set_active_method($setmethod);
6832a102
DM
98 redirect($PAGE->url);
99}
100
fde33804
DM
101// publish the form as a template
102if (!empty($shareform)) {
103 require_capability('moodle/grade:sharegradingforms', get_system_context());
104 $controller = $manager->get_controller($method);
105 $definition = $controller->get_definition();
106 if (!$confirmed) {
107 // let the user confirm they understand what they are doing (haha ;-)
108 echo $output->header();
109 echo $output->confirm(get_string('manageactionshareconfirm', 'core_grading', s($definition->name)),
110 new moodle_url($PAGE->url, array('shareform' => $shareform, 'confirmed' => 1)),
111 $PAGE->url);
112 echo $output->footer();
113 die();
114 } else {
115 require_sesskey();
116 $newareaid = $manager->create_shared_area($method);
117 $targetarea = get_grading_manager($newareaid);
118 $targetcontroller = $targetarea->get_controller($method);
119 $targetcontroller->update_definition($controller->get_definition_copy($targetcontroller));
120 redirect(new moodle_url($PAGE->url, array('message' => get_string('manageactionsharedone', 'core_grading'))));
121 }
122}
123
6832a102 124echo $output->header();
fde33804
DM
125
126if (!empty($message)) {
127 echo $output->management_message($message);
128}
129
6832a102
DM
130echo $output->heading(get_string('gradingmanagementtitle', 'core_grading', array(
131 'component' => $manager->get_component_title(), 'area' => $manager->get_area_title())));
132
133// display the active grading method information and selector
134echo $output->management_method_selector($manager, $PAGE->url);
135
136// get the currently active method's controller
6832a102
DM
137if (!empty($method)) {
138 $controller = $manager->get_controller($method);
139 // display relevant actions
140 echo $output->container_start('actions');
141 if ($controller->is_form_defined()) {
fde33804 142 $definition = $controller->get_definition();
6832a102 143 echo $output->management_action_icon($controller->get_editor_url($returnurl),
fde33804 144 get_string('manageactionedit', 'core_grading'), 'b/document-edit');
6832a102
DM
145 echo $output->management_action_icon($PAGE->url,
146 get_string('manageactiondelete', 'core_grading'), 'b/edit-delete');
fde33804
DM
147 if (has_capability('moodle/grade:sharegradingforms', get_system_context())) {
148 echo $output->management_action_icon(new moodle_url($PAGE->url, array('shareform' => $definition->id)),
149 get_string('manageactionshare', 'core_grading'), 'b/bookmark-new');
150 }
6832a102
DM
151 } else {
152 echo $output->management_action_icon($controller->get_editor_url($returnurl),
153 get_string('manageactionnew', 'core_grading'), 'b/document-new');
154 echo $output->management_action_icon($PAGE->url,
155 get_string('manageactionclone', 'core_grading'), 'b/edit-copy');
156 }
157 echo $output->container_end();
158
159 // display the grading form preview
160 if ($controller->is_form_defined()) {
161 echo $output->box($controller->render_preview($PAGE), 'preview');
162 }
163}
164
165
166echo $output->footer();