PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/group/grouping.php

https://bitbucket.org/moodle/moodle
PHP | 149 lines | 101 code | 22 blank | 26 comment | 26 complexity | 16df14e5bb0cb50b326cdd0be522ed74 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * Create grouping OR edit grouping settings.
  18. *
  19. * @copyright 2006 The Open University, N.D.Freear AT open.ac.uk, J.White AT open.ac.uk
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package core_group
  22. */
  23. require_once('../config.php');
  24. require_once('lib.php');
  25. require_once('grouping_form.php');
  26. /// get url variables
  27. $courseid = optional_param('courseid', 0, PARAM_INT);
  28. $id = optional_param('id', 0, PARAM_INT);
  29. $delete = optional_param('delete', 0, PARAM_BOOL);
  30. $confirm = optional_param('confirm', 0, PARAM_BOOL);
  31. $url = new moodle_url('/group/grouping.php');
  32. if ($id) {
  33. $url->param('id', $id);
  34. if (!$grouping = $DB->get_record('groupings', array('id'=>$id))) {
  35. print_error('invalidgroupid');
  36. }
  37. $grouping->description = clean_text($grouping->description);
  38. if (empty($courseid)) {
  39. $courseid = $grouping->courseid;
  40. } else if ($courseid != $grouping->courseid) {
  41. print_error('invalidcourseid');
  42. } else {
  43. $url->param('courseid', $courseid);
  44. }
  45. if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
  46. print_error('invalidcourseid');
  47. }
  48. } else {
  49. $url->param('courseid', $courseid);
  50. if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
  51. print_error('invalidcourseid');
  52. }
  53. $grouping = new stdClass();
  54. $grouping->courseid = $course->id;
  55. }
  56. $PAGE->set_url($url);
  57. require_login($course);
  58. $context = context_course::instance($course->id);
  59. require_capability('moodle/course:managegroups', $context);
  60. $strgroupings = get_string('groupings', 'group');
  61. $PAGE->set_title($strgroupings);
  62. $PAGE->set_heading($course->fullname. ': '.$strgroupings);
  63. $PAGE->set_pagelayout('admin');
  64. navigation_node::override_active_url(new moodle_url('/group/index.php', array('id' => $course->id)));
  65. $returnurl = $CFG->wwwroot.'/group/groupings.php?id='.$course->id;
  66. if ($id and $delete) {
  67. if (!empty($grouping->idnumber) && !has_capability('moodle/course:changeidnumber', $context)) {
  68. print_error('groupinghasidnumber', '', '', $grouping->name);
  69. }
  70. if (!$confirm) {
  71. $PAGE->set_title(get_string('deletegrouping', 'group'));
  72. $PAGE->set_heading($course->fullname. ': '. get_string('deletegrouping', 'group'));
  73. echo $OUTPUT->header();
  74. $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
  75. $optionsno = array('id'=>$courseid);
  76. $formcontinue = new single_button(new moodle_url('grouping.php', $optionsyes), get_string('yes'), 'get');
  77. $formcancel = new single_button(new moodle_url('groupings.php', $optionsno), get_string('no'), 'get');
  78. echo $OUTPUT->confirm(get_string('deletegroupingconfirm', 'group', $grouping->name), $formcontinue, $formcancel);
  79. echo $OUTPUT->footer();
  80. die;
  81. } else if (confirm_sesskey()){
  82. if (groups_delete_grouping($id)) {
  83. redirect($returnurl);
  84. } else {
  85. print_error('erroreditgrouping', 'group', $returnurl);
  86. }
  87. }
  88. }
  89. // Prepare the description editor: We do support files for grouping descriptions
  90. $editoroptions = array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$course->maxbytes, 'trust'=>true, 'context'=>$context, 'noclean'=>true);
  91. if (!empty($grouping->id)) {
  92. $grouping = file_prepare_standard_editor($grouping, 'description', $editoroptions, $context, 'grouping', 'description', $grouping->id);
  93. } else {
  94. $grouping = file_prepare_standard_editor($grouping, 'description', $editoroptions, $context, 'grouping', 'description', null);
  95. }
  96. /// First create the form
  97. $editform = new grouping_form(null, compact('editoroptions'));
  98. $editform->set_data($grouping);
  99. if ($editform->is_cancelled()) {
  100. redirect($returnurl);
  101. } elseif ($data = $editform->get_data()) {
  102. $success = true;
  103. if (!has_capability('moodle/course:changeidnumber', $context)) {
  104. // Remove the idnumber if the user doesn't have permission to modify it
  105. unset($data->idnumber);
  106. }
  107. if ($data->id) {
  108. groups_update_grouping($data, $editoroptions);
  109. } else {
  110. groups_create_grouping($data, $editoroptions);
  111. }
  112. redirect($returnurl);
  113. }
  114. $strparticipants = get_string('participants');
  115. if ($id) {
  116. $strheading = get_string('editgroupingsettings', 'group');
  117. } else {
  118. $strheading = get_string('creategrouping', 'group');
  119. }
  120. $PAGE->navbar->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$courseid)));
  121. $PAGE->navbar->add($strgroupings, new moodle_url('/group/groupings.php', array('id'=>$courseid)));
  122. $PAGE->navbar->add($strheading);
  123. /// Print header
  124. echo $OUTPUT->header();
  125. echo $OUTPUT->heading($strheading);
  126. $editform->display();
  127. echo $OUTPUT->footer();