PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/grade/edit/scale/edit.php

https://bitbucket.org/moodle/moodle
PHP | 154 lines | 109 code | 19 blank | 26 comment | 22 complexity | 67eba8107e006e5a65a3904479ead03a 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. * Edit page for grade scales
  18. *
  19. * @package core_grades
  20. * @copyright 2007 Petr Skoda
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require_once '../../../config.php';
  24. require_once $CFG->dirroot.'/grade/lib.php';
  25. require_once $CFG->dirroot.'/grade/report/lib.php';
  26. require_once 'edit_form.php';
  27. $courseid = optional_param('courseid', 0, PARAM_INT);
  28. $id = optional_param('id', 0, PARAM_INT);
  29. $PAGE->set_url('/grade/edit/scale/edit.php', array('id' => $id, 'courseid' => $courseid));
  30. $PAGE->set_pagelayout('admin');
  31. navigation_node::override_active_url(new moodle_url('/grade/edit/scale/index.php',
  32. array('id' => $courseid)));
  33. $systemcontext = context_system::instance();
  34. $heading = '';
  35. // a bit complex access control :-O
  36. if ($id) {
  37. $heading = get_string('editscale', 'grades');
  38. /// editing existing scale
  39. if (!$scale_rec = $DB->get_record('scale', array('id' => $id))) {
  40. print_error('invalidscaleid');
  41. }
  42. if ($scale_rec->courseid) {
  43. $scale_rec->standard = 0;
  44. if (!$course = $DB->get_record('course', array('id' => $scale_rec->courseid))) {
  45. print_error('invalidcourseid');
  46. }
  47. require_login($course);
  48. $context = context_course::instance($course->id);
  49. require_capability('moodle/course:managescales', $context);
  50. $courseid = $course->id;
  51. } else {
  52. if ($courseid) {
  53. if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  54. print_error('invalidcourseid');
  55. }
  56. }
  57. $scale_rec->standard = 1;
  58. $scale_rec->courseid = $courseid;
  59. require_login($courseid);
  60. require_capability('moodle/course:managescales', $systemcontext);
  61. }
  62. } else if ($courseid){
  63. $heading = get_string('addscale', 'grades');
  64. /// adding new scale from course
  65. if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  66. print_error('invalidcourseid');
  67. }
  68. $scale_rec = new stdClass();
  69. $scale_rec->standard = 0;
  70. $scale_rec->courseid = $courseid;
  71. require_login($course);
  72. $context = context_course::instance($course->id);
  73. require_capability('moodle/course:managescales', $context);
  74. } else {
  75. /// adding new scale from admin section
  76. $scale_rec = new stdClass();
  77. $scale_rec->standard = 1;
  78. $scale_rec->courseid = 0;
  79. require_login();
  80. require_capability('moodle/course:managescales', $systemcontext);
  81. }
  82. if (!$courseid) {
  83. require_once $CFG->libdir.'/adminlib.php';
  84. admin_externalpage_setup('scales');
  85. }
  86. // default return url
  87. $gpr = new grade_plugin_return();
  88. $returnurl = $gpr->get_return_url('index.php?id='.$courseid);
  89. $editoroptions = array(
  90. 'maxfiles' => EDITOR_UNLIMITED_FILES,
  91. 'maxbytes' => $CFG->maxbytes,
  92. 'trusttext' => false,
  93. 'noclean' => true,
  94. 'context' => $systemcontext
  95. );
  96. if (!empty($scale_rec->id)) {
  97. $editoroptions['subdirs'] = file_area_contains_subdirs($systemcontext, 'grade', 'scale', $scale_rec->id);
  98. $scale_rec = file_prepare_standard_editor($scale_rec, 'description', $editoroptions, $systemcontext, 'grade', 'scale', $scale_rec->id);
  99. } else {
  100. $editoroptions['subdirs'] = false;
  101. $scale_rec = file_prepare_standard_editor($scale_rec, 'description', $editoroptions, $systemcontext, 'grade', 'scale', null);
  102. }
  103. $mform = new edit_scale_form(null, compact('gpr', 'editoroptions'));
  104. $mform->set_data($scale_rec);
  105. if ($mform->is_cancelled()) {
  106. redirect($returnurl);
  107. } else if ($data = $mform->get_data()) {
  108. $scale = new grade_scale(array('id'=>$id));
  109. $data->userid = $USER->id;
  110. if (empty($scale->id)) {
  111. $data->description = $data->description_editor['text'];
  112. $data->descriptionformat = $data->description_editor['format'];
  113. grade_scale::set_properties($scale, $data);
  114. if (!has_capability('moodle/grade:manage', $systemcontext)) {
  115. $data->standard = 0;
  116. }
  117. $scale->courseid = !empty($data->standard) ? 0 : $courseid;
  118. $scale->insert();
  119. $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $systemcontext, 'grade', 'scale', $scale->id);
  120. $DB->set_field($scale->table, 'description', $data->description, array('id'=>$scale->id));
  121. } else {
  122. $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $systemcontext, 'grade', 'scale', $id);
  123. grade_scale::set_properties($scale, $data);
  124. if (isset($data->standard)) {
  125. $scale->courseid = !empty($data->standard) ? 0 : $courseid;
  126. } else {
  127. unset($scale->courseid); // keep previous
  128. }
  129. $scale->update();
  130. }
  131. redirect($returnurl);
  132. }
  133. print_grade_page_head($COURSE->id, 'scale', null, $heading, false, false, false);
  134. $mform->display();
  135. echo $OUTPUT->footer();