Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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  /**
  18   * A page for selecting outcomes for use in a course
  19   *
  20   * @package   core_grades
  21   * @copyright 2007 Petr Skoda
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once '../../../config.php';
  26  require_once $CFG->dirroot.'/grade/lib.php';
  27  require_once $CFG->libdir.'/gradelib.php';
  28  
  29  $courseid = required_param('id', PARAM_INT);
  30  
  31  $PAGE->set_url('/grade/edit/outcome/course.php', array('id'=>$courseid));
  32  
  33  $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
  34  
  35  /// Make sure they can even access this course
  36  require_login($course);
  37  $context = context_course::instance($course->id);
  38  require_capability('moodle/course:update', $context);
  39  
  40  /// return tracking object
  41  $gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'outcomes', 'courseid'=>$courseid));
  42  
  43  // first of all fix the state of outcomes_course table
  44  $standardoutcomes    = grade_outcome::fetch_all_global();
  45  $co_custom           = grade_outcome::fetch_all_local($courseid);
  46  $co_standard_used    = array();
  47  $co_standard_notused = array();
  48  
  49  if ($courseused = $DB->get_records('grade_outcomes_courses', array('courseid' => $courseid), '', 'outcomeid')) {
  50      $courseused = array_keys($courseused);
  51  } else {
  52      $courseused = array();
  53  }
  54  
  55  // fix wrong entries in outcomes_courses
  56  foreach ($courseused as $oid) {
  57      if (!array_key_exists($oid, $standardoutcomes) and !array_key_exists($oid, $co_custom)) {
  58          $DB->delete_records('grade_outcomes_courses', array('outcomeid' => $oid, 'courseid' => $courseid));
  59      }
  60  }
  61  
  62  // fix local custom outcomes missing in outcomes_course
  63  foreach($co_custom as $oid=>$outcome) {
  64      if (!in_array($oid, $courseused)) {
  65          $courseused[$oid] = $oid;
  66          $goc = new stdClass();
  67          $goc->courseid = $courseid;
  68          $goc->outcomeid = $oid;
  69          $DB->insert_record('grade_outcomes_courses', $goc);
  70      }
  71  }
  72  
  73  // now check all used standard outcomes are in outcomes_course too
  74  $params = array($courseid);
  75  $sql = "SELECT DISTINCT outcomeid
  76            FROM {grade_items}
  77           WHERE courseid=? and outcomeid IS NOT NULL";
  78  if ($realused = $DB->get_records_sql($sql, $params)) {
  79      $realused = array_keys($realused);
  80      foreach ($realused as $oid) {
  81          if (array_key_exists($oid, $standardoutcomes)) {
  82  
  83              $co_standard_used[$oid] = $standardoutcomes[$oid];
  84              unset($standardoutcomes[$oid]);
  85  
  86              if (!in_array($oid, $courseused)) {
  87                  $courseused[$oid] = $oid;
  88                  $goc = new stdClass();
  89                  $goc->courseid = $courseid;
  90                  $goc->outcomeid = $oid;
  91                  $DB->insert_record('grade_outcomes_courses', $goc);
  92              }
  93          }
  94      }
  95  }
  96  
  97  // find all unused standard course outcomes - candidates for removal
  98  foreach ($standardoutcomes as $oid=>$outcome) {
  99      if (in_array($oid, $courseused)) {
 100          $co_standard_notused[$oid] = $standardoutcomes[$oid];
 101          unset($standardoutcomes[$oid]);
 102      }
 103  }
 104  
 105  
 106  /// form processing
 107  if ($data = data_submitted() and confirm_sesskey()) {
 108      require_capability('moodle/grade:manageoutcomes', $context);
 109      if (!empty($data->add) && !empty($data->addoutcomes)) {
 110      /// add all selected to course list
 111          foreach ($data->addoutcomes as $add) {
 112              $add = clean_param($add, PARAM_INT);
 113              if (!array_key_exists($add, $standardoutcomes)) {
 114                  continue;
 115              }
 116              $goc = new stdClass();
 117              $goc->courseid = $courseid;
 118              $goc->outcomeid = $add;
 119              $DB->insert_record('grade_outcomes_courses', $goc);
 120          }
 121  
 122      } else if (!empty($data->remove) && !empty($data->removeoutcomes)) {
 123      /// remove all selected from course outcomes list
 124          foreach ($data->removeoutcomes as $remove) {
 125              $remove = clean_param($remove, PARAM_INT);
 126              if (!array_key_exists($remove, $co_standard_notused)) {
 127                  continue;
 128              }
 129              $DB->delete_records('grade_outcomes_courses', array('courseid' => $courseid, 'outcomeid' => $remove));
 130          }
 131      }
 132      redirect('course.php?id='.$courseid); // we must redirect to get fresh data
 133  }
 134  
 135  /// Print header
 136  print_grade_page_head($COURSE->id, 'outcome', 'course');
 137  
 138  require('course_form.html');
 139  
 140  echo $OUTPUT->footer();
 141