[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/grade/report/overview/ -> lib.php (source)

   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   * Definition of the grade_overview_report class
  19   *
  20   * @package gradereport_overview
  21   * @copyright 2007 Nicolas Connault
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once($CFG->dirroot . '/grade/report/lib.php');
  26  require_once($CFG->libdir.'/tablelib.php');
  27  
  28  /**
  29   * Class providing an API for the overview report building and displaying.
  30   * @uses grade_report
  31   * @package gradereport_overview
  32   */
  33  class grade_report_overview extends grade_report {
  34  
  35      /**
  36       * The user.
  37       * @var object $user
  38       */
  39      public $user;
  40  
  41      /**
  42       * The user's courses
  43       * @var array $courses
  44       */
  45      public $courses;
  46  
  47      /**
  48       * A flexitable to hold the data.
  49       * @var object $table
  50       */
  51      public $table;
  52  
  53      /**
  54       * Show student ranks within each course.
  55       * @var array $showrank
  56       */
  57      public $showrank;
  58  
  59      /**
  60       * show course/category totals if they contain hidden items
  61       */
  62      var $showtotalsifcontainhidden;
  63  
  64      /**
  65       * Constructor. Sets local copies of user preferences and initialises grade_tree.
  66       * @param int $userid
  67       * @param object $gpr grade plugin return tracking object
  68       * @param string $context
  69       */
  70      public function __construct($userid, $gpr, $context) {
  71          global $CFG, $COURSE, $DB;
  72          parent::__construct($COURSE->id, $gpr, $context);
  73  
  74          // Get the user (for full name).
  75          $this->user = $DB->get_record('user', array('id' => $userid));
  76  
  77          // Load the user's courses.
  78          $this->courses = enrol_get_users_courses($this->user->id, false, 'id, shortname, showgrades');
  79  
  80          $this->showrank = array();
  81          $this->showrank['any'] = false;
  82  
  83          $this->showtotalsifcontainhidden = array();
  84  
  85          if ($this->courses) {
  86              foreach ($this->courses as $course) {
  87                  $this->showrank[$course->id] = grade_get_setting($course->id, 'report_overview_showrank', !empty($CFG->grade_report_overview_showrank));
  88                  if ($this->showrank[$course->id]) {
  89                      $this->showrank['any'] = true;
  90                  }
  91  
  92                  $this->showtotalsifcontainhidden[$course->id] = grade_get_setting($course->id, 'report_overview_showtotalsifcontainhidden', $CFG->grade_report_overview_showtotalsifcontainhidden);
  93              }
  94          }
  95  
  96  
  97          // base url for sorting by first/last name
  98          $this->baseurl = $CFG->wwwroot.'/grade/overview/index.php?id='.$userid;
  99          $this->pbarurl = $this->baseurl;
 100  
 101          $this->setup_table();
 102      }
 103  
 104      /**
 105       * Prepares the headers and attributes of the flexitable.
 106       */
 107      public function setup_table() {
 108          /*
 109           * Table has 3 columns
 110           *| course  | final grade | rank (optional) |
 111           */
 112  
 113          // setting up table headers
 114          if ($this->showrank['any']) {
 115              $tablecolumns = array('coursename', 'grade', 'rank');
 116              $tableheaders = array($this->get_lang_string('coursename', 'grades'),
 117                                    $this->get_lang_string('grade'),
 118                                    $this->get_lang_string('rank', 'grades'));
 119          } else {
 120              $tablecolumns = array('coursename', 'grade');
 121              $tableheaders = array($this->get_lang_string('coursename', 'grades'),
 122                                    $this->get_lang_string('grade'));
 123          }
 124          $this->table = new flexible_table('grade-report-overview-'.$this->user->id);
 125  
 126          $this->table->define_columns($tablecolumns);
 127          $this->table->define_headers($tableheaders);
 128          $this->table->define_baseurl($this->baseurl);
 129  
 130          $this->table->set_attribute('cellspacing', '0');
 131          $this->table->set_attribute('id', 'overview-grade');
 132          $this->table->set_attribute('class', 'boxaligncenter generaltable');
 133  
 134          $this->table->setup();
 135      }
 136  
 137      public function fill_table() {
 138          global $CFG, $DB, $OUTPUT;
 139  
 140          // Only show user's courses instead of all courses.
 141          if ($this->courses) {
 142              $numusers = $this->get_numusers(false);
 143  
 144              foreach ($this->courses as $course) {
 145                  if (!$course->showgrades) {
 146                      continue;
 147                  }
 148  
 149                  $coursecontext = context_course::instance($course->id);
 150  
 151                  if (!$course->visible && !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
 152                      // The course is hidden and the user isn't allowed to see it
 153                      continue;
 154                  }
 155  
 156                  $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
 157                  $courselink = html_writer::link(new moodle_url('/grade/report/user/index.php', array('id' => $course->id, 'userid' => $this->user->id)), $courseshortname);
 158                  $canviewhidden = has_capability('moodle/grade:viewhidden', $coursecontext);
 159  
 160                  // Get course grade_item
 161                  $course_item = grade_item::fetch_course_item($course->id);
 162  
 163                  // Get the stored grade
 164                  $course_grade = new grade_grade(array('itemid'=>$course_item->id, 'userid'=>$this->user->id));
 165                  $course_grade->grade_item =& $course_item;
 166                  $finalgrade = $course_grade->finalgrade;
 167  
 168                  if (!$canviewhidden and !is_null($finalgrade)) {
 169                      if ($course_grade->is_hidden()) {
 170                          $finalgrade = null;
 171                      } else {
 172                          $adjustedgrade = $this->blank_hidden_total_and_adjust_bounds($course->id,
 173                                                                                       $course_item,
 174                                                                                       $finalgrade);
 175  
 176                          // We temporarily adjust the view of this grade item - because the min and
 177                          // max are affected by the hidden values in the aggregation.
 178                          $finalgrade = $adjustedgrade['grade'];
 179                          $course_item->grademax = $adjustedgrade['grademax'];
 180                          $course_item->grademin = $adjustedgrade['grademin'];
 181                      }
 182                  } else {
 183                      // We must use the rawgrademin / rawgrademax because it can be different for
 184                      // each grade_grade when items are excluded from sum of grades.
 185                      if (!is_null($finalgrade)) {
 186                          $course_item->grademin = $course_grade->rawgrademin;
 187                          $course_item->grademax = $course_grade->rawgrademax;
 188                      }
 189                  }
 190  
 191                  $data = array($courselink, grade_format_gradevalue($finalgrade, $course_item, true));
 192  
 193                  if (!$this->showrank['any']) {
 194                      //nothing to do
 195  
 196                  } else if ($this->showrank[$course->id] && !is_null($finalgrade)) {
 197                      /// find the number of users with a higher grade
 198                      /// please note this can not work if hidden grades involved :-( to be fixed in 2.0
 199                      $params = array($finalgrade, $course_item->id);
 200                      $sql = "SELECT COUNT(DISTINCT(userid))
 201                                FROM {grade_grades}
 202                               WHERE finalgrade IS NOT NULL AND finalgrade > ?
 203                                     AND itemid = ?";
 204                      $rank = $DB->count_records_sql($sql, $params) + 1;
 205  
 206                      $data[] = "$rank/$numusers";
 207  
 208                  } else {
 209                      // No grade, no rank.
 210                      // Or this course wants rank hidden.
 211                      $data[] = '-';
 212                  }
 213  
 214                  $this->table->add_data($data);
 215              }
 216              return true;
 217  
 218          } else {
 219              echo $OUTPUT->notification(get_string('nocourses', 'grades'));
 220              return false;
 221          }
 222      }
 223  
 224      /**
 225       * Prints or returns the HTML from the flexitable.
 226       * @param bool $return Whether or not to return the data instead of printing it directly.
 227       * @return string
 228       */
 229      public function print_table($return=false) {
 230          ob_start();
 231          $this->table->print_html();
 232          $html = ob_get_clean();
 233          if ($return) {
 234              return $html;
 235          } else {
 236              echo $html;
 237          }
 238      }
 239  
 240      /**
 241       * Processes the data sent by the form (grades and feedbacks).
 242       * @var array $data
 243       * @return bool Success or Failure (array of errors).
 244       */
 245      function process_data($data) {
 246      }
 247      function process_action($target, $action) {
 248      }
 249  }
 250  
 251  function grade_report_overview_settings_definition(&$mform) {
 252      global $CFG;
 253  
 254      //show rank
 255      $options = array(-1 => get_string('default', 'grades'),
 256                        0 => get_string('hide'),
 257                        1 => get_string('show'));
 258  
 259      if (empty($CFG->grade_overviewreport_showrank)) {
 260          $options[-1] = get_string('defaultprev', 'grades', $options[0]);
 261      } else {
 262          $options[-1] = get_string('defaultprev', 'grades', $options[1]);
 263      }
 264  
 265      $mform->addElement('select', 'report_overview_showrank', get_string('showrank', 'grades'), $options);
 266      $mform->addHelpButton('report_overview_showrank', 'showrank', 'grades');
 267  
 268      //showtotalsifcontainhidden
 269      $options = array(-1 => get_string('default', 'grades'),
 270                        GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN => get_string('hide'),
 271                        GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowexhiddenitems', 'grades'),
 272                        GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowinchiddenitems', 'grades') );
 273  
 274      if (empty($CFG->grade_report_overview_showtotalsifcontainhidden)) {
 275          $options[-1] = get_string('defaultprev', 'grades', $options[0]);
 276      } else {
 277          $options[-1] = get_string('defaultprev', 'grades', $options[1]);
 278      }
 279  
 280      $mform->addElement('select', 'report_overview_showtotalsifcontainhidden', get_string('hidetotalifhiddenitems', 'grades'), $options);
 281      $mform->addHelpButton('report_overview_showtotalsifcontainhidden', 'hidetotalifhiddenitems', 'grades');
 282  }
 283  
 284  


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1