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

/grade/edit/outcome/course.php

https://github.com/nadavkav/MoodleTAO
PHP | 134 lines | 89 code | 20 blank | 25 comment | 17 complexity | 9bf7ef1bc1bbe1b57a00db6fa4a7cec1 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. require_once '../../../config.php';
  17. require_once $CFG->dirroot.'/grade/lib.php';
  18. require_once $CFG->libdir.'/gradelib.php';
  19. $courseid = required_param('id', PARAM_INT);
  20. /// Make sure they can even access this course
  21. if (!$course = get_record('course', 'id', $courseid)) {
  22. print_error('nocourseid');
  23. }
  24. require_login($course);
  25. $context = get_context_instance(CONTEXT_COURSE, $course->id);
  26. require_capability('moodle/course:update', $context);
  27. /// return tracking object
  28. $gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'outcomes', 'courseid'=>$courseid));
  29. // first of all fix the state of outcomes_course table
  30. $standardoutcomes = grade_outcome::fetch_all_global();
  31. $co_custom = grade_outcome::fetch_all_local($courseid);
  32. $co_standard_used = array();
  33. $co_standard_notused = array();
  34. if ($courseused = get_records('grade_outcomes_courses', 'courseid', $courseid, '', 'outcomeid')) {
  35. $courseused = array_keys($courseused);
  36. } else {
  37. $courseused = array();
  38. }
  39. // fix wrong entries in outcomes_courses
  40. foreach ($courseused as $oid) {
  41. if (!array_key_exists($oid, $standardoutcomes) and !array_key_exists($oid, $co_custom)) {
  42. delete_records('grade_outcomes_courses', 'outcomeid', $oid, 'courseid', $courseid);
  43. }
  44. }
  45. // fix local custom outcomes missing in outcomes_course
  46. foreach($co_custom as $oid=>$outcome) {
  47. if (!in_array($oid, $courseused)) {
  48. $courseused[$oid] = $oid;
  49. $goc = new object();
  50. $goc->courseid = $courseid;
  51. $goc->outcomeid = $oid;
  52. insert_record('grade_outcomes_courses', $goc);
  53. }
  54. }
  55. // now check all used standard outcomes are in outcomes_course too
  56. $sql = "SELECT DISTINCT outcomeid
  57. FROM {$CFG->prefix}grade_items
  58. WHERE courseid=$courseid and outcomeid IS NOT NULL";
  59. if ($realused = get_records_sql($sql)) {
  60. $realused = array_keys($realused);
  61. foreach ($realused as $oid) {
  62. if (array_key_exists($oid, $standardoutcomes)) {
  63. $co_standard_used[$oid] = $standardoutcomes[$oid];
  64. unset($standardoutcomes[$oid]);
  65. if (!in_array($oid, $courseused)) {
  66. $courseused[$oid] = $oid;
  67. $goc = new object();
  68. $goc->courseid = $courseid;
  69. $goc->outcomeid = $oid;
  70. insert_record('grade_outcomes_courses', $goc);
  71. }
  72. }
  73. }
  74. }
  75. // find all unused standard course outcomes - candidates for removal
  76. foreach ($standardoutcomes as $oid=>$outcome) {
  77. if (in_array($oid, $courseused)) {
  78. $co_standard_notused[$oid] = $standardoutcomes[$oid];
  79. unset($standardoutcomes[$oid]);
  80. }
  81. }
  82. /// form processing
  83. if ($data = data_submitted() and confirm_sesskey()) {
  84. require_capability('moodle/grade:manageoutcomes', $context);
  85. if (!empty($data->add) && !empty($data->addoutcomes)) {
  86. /// add all selected to course list
  87. foreach ($data->addoutcomes as $add) {
  88. $add = clean_param($add, PARAM_INT);
  89. if (!array_key_exists($add, $standardoutcomes)) {
  90. continue;
  91. }
  92. $goc = new object();
  93. $goc->courseid = $courseid;
  94. $goc->outcomeid = $add;
  95. insert_record('grade_outcomes_courses', $goc);
  96. }
  97. } else if (!empty($data->remove) && !empty($data->removeoutcomes)) {
  98. /// remove all selected from course outcomes list
  99. foreach ($data->removeoutcomes as $remove) {
  100. $remove = clean_param($remove, PARAM_INT);
  101. if (!array_key_exists($remove, $co_standard_notused)) {
  102. continue;
  103. }
  104. delete_records('grade_outcomes_courses', 'courseid', $courseid, 'outcomeid', $remove);
  105. }
  106. }
  107. redirect('course.php?id='.$courseid); // we must redirect to get fresh data
  108. }
  109. /// Print header
  110. print_grade_page_head($COURSE->id, 'outcome', 'course');
  111. check_theme_arrows();
  112. require('course_form.html');
  113. print_footer($course);
  114. ?>