PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/grade/edit/outcome/edit.php

https://bitbucket.org/moodle/moodle
PHP | 175 lines | 126 code | 23 blank | 26 comment | 28 complexity | 0dbb2b979bf15644a6283155a073c389 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 outcomes.
  18. *
  19. * @package core_grades
  20. * @copyright 2008 Nicolas Connault
  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. $url = new moodle_url('/grade/edit/outcome/edit.php');
  30. if ($courseid !== 0) {
  31. $url->param('courseid', $courseid);
  32. }
  33. if ($id !== 0) {
  34. $url->param('id', $id);
  35. }
  36. $PAGE->set_url($url);
  37. $PAGE->set_pagelayout('admin');
  38. $systemcontext = context_system::instance();
  39. $heading = null;
  40. // a bit complex access control :-O
  41. if ($id) {
  42. $heading = get_string('editoutcome', 'grades');
  43. /// editing existing outcome
  44. if (!$outcome_rec = $DB->get_record('grade_outcomes', array('id' => $id))) {
  45. print_error('invalidoutcome');
  46. }
  47. if ($outcome_rec->courseid) {
  48. $outcome_rec->standard = 0;
  49. if (!$course = $DB->get_record('course', array('id' => $outcome_rec->courseid))) {
  50. print_error('invalidcourseid');
  51. }
  52. require_login($course);
  53. $context = context_course::instance($course->id);
  54. require_capability('moodle/grade:manage', $context);
  55. $courseid = $course->id;
  56. } else {
  57. if ($courseid) {
  58. if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  59. print_error('invalidcourseid');
  60. }
  61. }
  62. $outcome_rec->standard = 1;
  63. $outcome_rec->courseid = $courseid;
  64. require_login();
  65. require_capability('moodle/grade:manage', $systemcontext);
  66. $PAGE->set_context($systemcontext);
  67. }
  68. } else if ($courseid){
  69. $heading = get_string('addoutcome', 'grades');
  70. /// adding new outcome from course
  71. $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
  72. require_login($course);
  73. $context = context_course::instance($course->id);
  74. require_capability('moodle/grade:manage', $context);
  75. navigation_node::override_active_url(new moodle_url('/grade/edit/outcome/course.php', array('id'=>$courseid)));
  76. $outcome_rec = new stdClass();
  77. $outcome_rec->standard = 0;
  78. $outcome_rec->courseid = $courseid;
  79. } else {
  80. require_login();
  81. require_capability('moodle/grade:manage', $systemcontext);
  82. $PAGE->set_context($systemcontext);
  83. /// adding new outcome from admin section
  84. $outcome_rec = new stdClass();
  85. $outcome_rec->standard = 1;
  86. $outcome_rec->courseid = 0;
  87. }
  88. if (!$courseid) {
  89. require_once $CFG->libdir.'/adminlib.php';
  90. admin_externalpage_setup('outcomes');
  91. }
  92. // default return url
  93. $gpr = new grade_plugin_return();
  94. $returnurl = $gpr->get_return_url('index.php?id='.$courseid);
  95. $editoroptions = array(
  96. 'maxfiles' => EDITOR_UNLIMITED_FILES,
  97. 'maxbytes' => $CFG->maxbytes,
  98. 'trusttext' => false,
  99. 'noclean' => true,
  100. 'context' => $systemcontext
  101. );
  102. if (!empty($outcome_rec->id)) {
  103. $editoroptions['subdirs'] = file_area_contains_subdirs($systemcontext, 'grade', 'outcome', $outcome_rec->id);
  104. $outcome_rec = file_prepare_standard_editor($outcome_rec, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', $outcome_rec->id);
  105. } else {
  106. $editoroptions['subdirs'] = false;
  107. $outcome_rec = file_prepare_standard_editor($outcome_rec, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', null);
  108. }
  109. $mform = new edit_outcome_form(null, compact('gpr', 'editoroptions'));
  110. $mform->set_data($outcome_rec);
  111. if ($mform->is_cancelled()) {
  112. redirect($returnurl);
  113. } else if ($data = $mform->get_data()) {
  114. $outcome = new grade_outcome(array('id'=>$id));
  115. $data->usermodified = $USER->id;
  116. if (empty($outcome->id)) {
  117. $data->description = $data->description_editor['text'];
  118. grade_outcome::set_properties($outcome, $data);
  119. if (!has_capability('moodle/grade:manage', $systemcontext)) {
  120. $data->standard = 0;
  121. }
  122. $outcome->courseid = !empty($data->standard) ? null : $courseid;
  123. if (empty($outcome->courseid)) {
  124. $outcome->courseid = null;
  125. }
  126. $outcome->insert();
  127. $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', $outcome->id);
  128. $DB->set_field($outcome->table, 'description', $data->description, array('id'=>$outcome->id));
  129. } else {
  130. $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', $id);
  131. grade_outcome::set_properties($outcome, $data);
  132. if (isset($data->standard)) {
  133. $outcome->courseid = !empty($data->standard) ? null : $courseid;
  134. } else {
  135. unset($outcome->courseid); // keep previous
  136. }
  137. $outcome->update();
  138. }
  139. redirect($returnurl);
  140. }
  141. if ($courseid) {
  142. print_grade_page_head($courseid, 'outcome', 'edit', $heading);
  143. } else {
  144. echo $OUTPUT->header();
  145. }
  146. if (!grade_scale::fetch_all_local($courseid) && !grade_scale::fetch_all_global()) {
  147. echo $OUTPUT->confirm(get_string('noscales', 'grades'), $CFG->wwwroot.'/grade/edit/scale/edit.php?courseid='.$courseid, $returnurl);
  148. echo $OUTPUT->footer();
  149. die();
  150. }
  151. $mform->display();
  152. echo $OUTPUT->footer();