PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/grade/grading/pick.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 257 lines | 177 code | 34 blank | 46 comment | 30 complexity | 4a9f111d6ea70caf0aeb48c489bf9545 MD5 | raw file
  1. <?php
  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. * Allows to choose a form from the list of available templates
  18. *
  19. * @package core_grading
  20. * @copyright 2011 David Mudrak <david@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
  24. require_once($CFG->dirroot.'/grade/grading/lib.php');
  25. require_once($CFG->dirroot.'/grade/grading/pick_form.php');
  26. $targetid = required_param('targetid', PARAM_INT); // area we are coming from
  27. $pick = optional_param('pick', null, PARAM_INT); // create new form from this template
  28. $remove = optional_param('remove', null, PARAM_INT); // remove this template
  29. $confirmed = optional_param('confirmed', false, PARAM_BOOL); // is the action confirmed
  30. // the manager of the target area
  31. $targetmanager = get_grading_manager($targetid);
  32. if ($targetmanager->get_context()->contextlevel < CONTEXT_COURSE) {
  33. throw new coding_exception('Unsupported gradable area context level');
  34. }
  35. // currently active method in the target area
  36. $method = $targetmanager->get_active_method();
  37. $targetcontroller = $targetmanager->get_controller($method);
  38. $targetcontrollerclass = get_class($targetcontroller);
  39. // make sure there is no such form defined in the target area
  40. if ($targetcontroller->is_form_defined()) {
  41. redirect(new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid)));
  42. }
  43. list($context, $course, $cm) = get_context_info_array($targetmanager->get_context()->id);
  44. require_login($course, true, $cm);
  45. require_capability('moodle/grade:managegradingforms', $context);
  46. // user's capability in the templates bank
  47. $canshare = has_capability('moodle/grade:sharegradingforms', context_system::instance());
  48. $canmanage = has_capability('moodle/grade:managesharedforms', context_system::instance());
  49. // setup the page
  50. $PAGE->set_url(new moodle_url('/grade/grading/pick.php', array('targetid' => $targetid)));
  51. navigation_node::override_active_url($targetmanager->get_management_url());
  52. $PAGE->set_title(get_string('gradingmanagement', 'core_grading'));
  53. $PAGE->set_heading(get_string('gradingmanagement', 'core_grading'));
  54. $output = $PAGE->get_renderer('core_grading');
  55. // process picking a template
  56. if ($pick) {
  57. $sourceid = $DB->get_field('grading_definitions', 'areaid', array('id' => $pick), MUST_EXIST);
  58. $sourcemanager = get_grading_manager($sourceid);
  59. $sourcecontroller = $sourcemanager->get_controller($method);
  60. if (!$sourcecontroller->is_shared_template() and !$sourcecontroller->is_own_form()) {
  61. // note that we don't actually check whether the user has still the capability
  62. // moodle/grade:managegradingforms in the source area. so when users loose
  63. // their teacher role in a course, they can't access the course but they can
  64. // still copy the forms they have created there.
  65. throw new moodle_exception('attempt_to_pick_others_form', 'core_grading');
  66. }
  67. if (!$sourcecontroller->is_form_defined()) {
  68. throw new moodle_exception('form_definition_mismatch', 'core_grading');
  69. }
  70. $definition = $sourcecontroller->get_definition();
  71. if (!$confirmed) {
  72. echo $output->header();
  73. echo $output->confirm(get_string('templatepickconfirm', 'core_grading',array(
  74. 'formname' => s($definition->name),
  75. 'component' => $targetmanager->get_component_title(),
  76. 'area' => $targetmanager->get_area_title())),
  77. new moodle_url($PAGE->url, array('pick' => $pick, 'confirmed' => 1)),
  78. $PAGE->url);
  79. echo $output->box($sourcecontroller->render_preview($PAGE), 'template-preview-confirm');
  80. echo $output->footer();
  81. die();
  82. } else {
  83. require_sesskey();
  84. $targetcontroller->update_definition($sourcecontroller->get_definition_copy($targetcontroller));
  85. $DB->set_field('grading_definitions', 'timecopied', time(), array('id' => $definition->id));
  86. redirect(new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid)));
  87. }
  88. }
  89. // process removing a template
  90. if ($remove) {
  91. $sourceid = $DB->get_field('grading_definitions', 'areaid', array('id' => $remove), MUST_EXIST);
  92. $sourcemanager = get_grading_manager($sourceid);
  93. $sourcecontroller = $sourcemanager->get_controller($method);
  94. if (!$sourcecontroller->is_shared_template()) {
  95. throw new moodle_exception('attempt_to_delete_nontemplate', 'core_grading');
  96. }
  97. if (!$sourcecontroller->is_form_defined()) {
  98. throw new moodle_exception('form_definition_mismatch', 'core_grading');
  99. }
  100. $definition = $sourcecontroller->get_definition();
  101. if ($canmanage or ($canshare and ($definition->usercreated == $USER->id))) {
  102. // ok, this user can drop the template
  103. } else {
  104. throw new moodle_exception('no_permission_to_remove_template', 'core_grading');
  105. }
  106. if (!$confirmed) {
  107. echo $output->header();
  108. echo $output->confirm(get_string('templatedeleteconfirm', 'core_grading', s($definition->name)),
  109. new moodle_url($PAGE->url, array('remove' => $remove, 'confirmed' => 1)),
  110. $PAGE->url);
  111. echo $output->box($sourcecontroller->render_preview($PAGE), 'template-preview-confirm');
  112. echo $output->footer();
  113. die();
  114. } else {
  115. require_sesskey();
  116. $sourcecontroller->delete_definition();
  117. redirect($PAGE->url);
  118. }
  119. }
  120. $searchform = new grading_search_template_form($PAGE->url, null, 'GET', '', array('class' => 'templatesearchform'));
  121. if ($searchdata = $searchform->get_data()) {
  122. $tokens = grading_manager::tokenize($searchdata->needle);
  123. $includeownforms = (!empty($searchdata->mode));
  124. } else {
  125. $tokens = array();
  126. $includeownforms = false;
  127. }
  128. // construct the SQL to find all matching templates
  129. $sql = "SELECT DISTINCT gd.id, gd.areaid, gd.name, gd.usercreated
  130. FROM {grading_definitions} gd
  131. JOIN {grading_areas} ga ON (gd.areaid = ga.id)
  132. JOIN {context} cx ON (ga.contextid = cx.id)";
  133. // join method-specific tables from the plugin scope
  134. $sql .= $targetcontrollerclass::sql_search_from_tables('gd.id');
  135. $sql .= " WHERE gd.method = ?";
  136. $params = array($method);
  137. if (!$includeownforms) {
  138. // search for public templates only
  139. $sql .= " AND ga.contextid = ? AND ga.component = 'core_grading'";
  140. $params[] = context_system::instance()->id;
  141. } else {
  142. // search both templates and own forms in other areas
  143. $sql .= " AND ((ga.contextid = ? AND ga.component = 'core_grading')
  144. OR (gd.usercreated = ? AND gd.status = ?))";
  145. $params = array_merge($params, array(context_system::instance()->id, $USER->id,
  146. gradingform_controller::DEFINITION_STATUS_READY));
  147. }
  148. if ($tokens) {
  149. $subsql = array();
  150. // search for any of the tokens in the definition name
  151. foreach ($tokens as $token) {
  152. $subsql[] = $DB->sql_like('gd.name', '?', false, false);
  153. $params[] = '%'.$DB->sql_like_escape($token).'%';
  154. }
  155. // search for any of the tokens in the definition description
  156. foreach ($tokens as $token) {
  157. $subsql[] = $DB->sql_like('gd.description', '?', false, false);
  158. $params[] = '%'.$DB->sql_like_escape($token).'%';
  159. }
  160. // search for the needle in method-specific tables
  161. foreach ($tokens as $token) {
  162. list($methodsql, $methodparams) = $targetcontrollerclass::sql_search_where($token);
  163. $subsql = array_merge($subsql, $methodsql);
  164. $params = array_merge($params, $methodparams);
  165. }
  166. $sql .= " AND ((" . join(")\n OR (", $subsql) . "))";
  167. }
  168. $sql .= " ORDER BY gd.name";
  169. $rs = $DB->get_recordset_sql($sql, $params);
  170. echo $output->header();
  171. $searchform->display();
  172. $found = 0;
  173. foreach ($rs as $template) {
  174. $found++;
  175. $out = '';
  176. $manager = get_grading_manager($template->areaid);
  177. $controller = $manager->get_controller($method);
  178. if ($controller->is_shared_template()) {
  179. $templatetag = html_writer::tag('span', get_string('templatetypeshared', 'core_grading'),
  180. array('class' => 'type shared'));
  181. $templatesrc = '';
  182. } else if ($controller->is_own_form()) {
  183. $templatetag = html_writer::tag('span', get_string('templatetypeown', 'core_grading'),
  184. array('class' => 'type ownform'));
  185. $templatesrc = get_string('templatesource', 'core_grading', array(
  186. 'component' => $manager->get_component_title(),
  187. 'area' => $manager->get_area_title()));
  188. } else {
  189. throw new coding_exception('Something is wrong, the displayed form must be either template or own form');
  190. }
  191. $out .= $output->heading(s($template->name).' '.$templatetag, 2, 'template-name');
  192. $out .= $output->container($templatesrc, 'template-source');
  193. $out .= $output->box($controller->render_preview($PAGE), 'template-preview');
  194. $actions = array();
  195. if ($controller->is_shared_template()) {
  196. $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('pick' => $template->id)),
  197. get_string('templatepick', 'core_grading'), 'i/tick_green_big', 'pick template');
  198. if ($canmanage or ($canshare and ($template->usercreated == $USER->id))) {
  199. //$actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('edit' => $template->id)),
  200. // get_string('templateedit', 'core_grading'), 'i/edit', 'edit');
  201. $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('remove' => $template->id)),
  202. get_string('templatedelete', 'core_grading'), 't/delete', 'remove');
  203. }
  204. } else if ($controller->is_own_form()) {
  205. $actions[] = $output->pick_action_icon(new moodle_url($PAGE->url, array('pick' => $template->id)),
  206. get_string('templatepickownform', 'core_grading'), 'i/tick_green_big', 'pick ownform');
  207. }
  208. $out .= $output->box(join(' ', $actions), 'template-actions');
  209. $out .= $output->box($controller->get_formatted_description(), 'template-description');
  210. // ideally we should highlight just the name, description and the fields
  211. // in the preview that were actually searched. to make our life easier, we
  212. // simply highlight the tokens everywhere they appear, even if that exact
  213. // piece was not searched.
  214. echo highlight(join(' ', $tokens), $out);
  215. }
  216. $rs->close();
  217. if (!$found) {
  218. echo $output->heading(get_string('nosharedformfound', 'core_grading'));
  219. }
  220. echo $output->single_button(
  221. new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid)),
  222. get_string('back'), 'get');
  223. echo $output->footer();