[ Index ]

PHP Cross Reference of Unnamed Project

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       * An array of course ids that the user is a student in.
  66       * @var array $studentcourseids
  67       */
  68      public $studentcourseids;
  69  
  70      /**
  71       * An array of courses that the user is a teacher in.
  72       * @var array $teachercourses
  73       */
  74      public $teachercourses;
  75  
  76      /**
  77       * Constructor. Sets local copies of user preferences and initialises grade_tree.
  78       * @param int $userid
  79       * @param object $gpr grade plugin return tracking object
  80       * @param string $context
  81       */
  82      public function __construct($userid, $gpr, $context) {
  83          global $CFG, $COURSE, $DB;
  84          parent::__construct($COURSE->id, $gpr, $context);
  85  
  86          // Get the user (for full name).
  87          $this->user = $DB->get_record('user', array('id' => $userid));
  88  
  89          // Load the user's courses.
  90          $this->courses = enrol_get_users_courses($this->user->id, false, 'id, shortname, showgrades');
  91  
  92          $this->showrank = array();
  93          $this->showrank['any'] = false;
  94  
  95          $this->showtotalsifcontainhidden = array();
  96  
  97          $this->studentcourseids = array();
  98          $this->teachercourses = array();
  99          $roleids = explode(',', get_config('moodle', 'gradebookroles'));
 100  
 101          if ($this->courses) {
 102              foreach ($this->courses as $course) {
 103                  $this->showrank[$course->id] = grade_get_setting($course->id, 'report_overview_showrank', !empty($CFG->grade_report_overview_showrank));
 104                  if ($this->showrank[$course->id]) {
 105                      $this->showrank['any'] = true;
 106                  }
 107  
 108                  $this->showtotalsifcontainhidden[$course->id] = grade_get_setting($course->id, 'report_overview_showtotalsifcontainhidden', $CFG->grade_report_overview_showtotalsifcontainhidden);
 109  
 110                  $coursecontext = context_course::instance($course->id);
 111  
 112                  foreach ($roleids as $roleid) {
 113                      if (user_has_role_assignment($userid, $roleid, $coursecontext->id)) {
 114                          $this->studentcourseids[$course->id] = $course->id;
 115                          // We only need to check if one of the roleids has been assigned.
 116                          break;
 117                      }
 118                  }
 119  
 120                  if (has_capability('moodle/grade:viewall', $coursecontext, $userid)) {
 121                      $this->teachercourses[$course->id] = $course;
 122                  }
 123              }
 124          }
 125  
 126  
 127          // base url for sorting by first/last name
 128          $this->baseurl = $CFG->wwwroot.'/grade/overview/index.php?id='.$userid;
 129          $this->pbarurl = $this->baseurl;
 130  
 131          $this->setup_table();
 132      }
 133  
 134      /**
 135       * Prepares the headers and attributes of the flexitable.
 136       */
 137      public function setup_table() {
 138          /*
 139           * Table has 3 columns
 140           *| course  | final grade | rank (optional) |
 141           */
 142  
 143          // setting up table headers
 144          if ($this->showrank['any']) {
 145              $tablecolumns = array('coursename', 'grade', 'rank');
 146              $tableheaders = array($this->get_lang_string('coursename', 'grades'),
 147                                    $this->get_lang_string('grade'),
 148                                    $this->get_lang_string('rank', 'grades'));
 149          } else {
 150              $tablecolumns = array('coursename', 'grade');
 151              $tableheaders = array($this->get_lang_string('coursename', 'grades'),
 152                                    $this->get_lang_string('grade'));
 153          }
 154          $this->table = new flexible_table('grade-report-overview-'.$this->user->id);
 155  
 156          $this->table->define_columns($tablecolumns);
 157          $this->table->define_headers($tableheaders);
 158          $this->table->define_baseurl($this->baseurl);
 159  
 160          $this->table->set_attribute('cellspacing', '0');
 161          $this->table->set_attribute('id', 'overview-grade');
 162          $this->table->set_attribute('class', 'boxaligncenter generaltable');
 163  
 164          $this->table->setup();
 165      }
 166  
 167      /**
 168       * Fill the table for displaying.
 169       *
 170       * @param bool $activitylink If this report link to the activity report or the user report.
 171       * @param bool $studentcoursesonly Only show courses that the user is a student of.
 172       */
 173      public function fill_table($activitylink = false, $studentcoursesonly = false) {
 174          global $CFG, $DB, $OUTPUT, $USER;
 175  
 176          if ($studentcoursesonly && count($this->studentcourseids) == 0) {
 177              return false;
 178          }
 179  
 180          // Only show user's courses instead of all courses.
 181          if ($this->courses) {
 182              $numusers = $this->get_numusers(false);
 183  
 184              foreach ($this->courses as $course) {
 185                  if (!$course->showgrades) {
 186                      continue;
 187                  }
 188  
 189                  // If we are only showing student courses and this course isn't part of the group, then move on.
 190                  if ($studentcoursesonly && !isset($this->studentcourseids[$course->id])) {
 191                      continue;
 192                  }
 193  
 194                  $coursecontext = context_course::instance($course->id);
 195  
 196                  if (!$course->visible && !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
 197                      // The course is hidden and the user isn't allowed to see it
 198                      continue;
 199                  }
 200  
 201                  if (!has_capability('moodle/user:viewuseractivitiesreport', context_user::instance($this->user->id)) &&
 202                          ((!has_capability('moodle/grade:view', $coursecontext) || $this->user->id != $USER->id) &&
 203                          !has_capability('moodle/grade:viewall', $coursecontext))) {
 204                      continue;
 205                  }
 206  
 207                  $coursename = format_string(get_course_display_name_for_list($course), true, array('context' => $coursecontext));
 208                  // Link to the activity report version of the user grade report.
 209                  if ($activitylink) {
 210                      $courselink = html_writer::link(new moodle_url('/course/user.php', array('mode' => 'grade', 'id' => $course->id,
 211                          'user' => $this->user->id)), $coursename);
 212                  } else {
 213                      $courselink = html_writer::link(new moodle_url('/grade/report/user/index.php', array('id' => $course->id,
 214                          'userid' => $this->user->id)), $coursename);
 215                  }
 216                  $canviewhidden = has_capability('moodle/grade:viewhidden', $coursecontext);
 217  
 218                  // Get course grade_item
 219                  $course_item = grade_item::fetch_course_item($course->id);
 220  
 221                  // Get the stored grade
 222                  $course_grade = new grade_grade(array('itemid'=>$course_item->id, 'userid'=>$this->user->id));
 223                  $course_grade->grade_item =& $course_item;
 224                  $finalgrade = $course_grade->finalgrade;
 225  
 226                  if (!$canviewhidden and !is_null($finalgrade)) {
 227                      if ($course_grade->is_hidden()) {
 228                          $finalgrade = null;
 229                      } else {
 230                          $adjustedgrade = $this->blank_hidden_total_and_adjust_bounds($course->id,
 231                                                                                       $course_item,
 232                                                                                       $finalgrade);
 233  
 234                          // We temporarily adjust the view of this grade item - because the min and
 235                          // max are affected by the hidden values in the aggregation.
 236                          $finalgrade = $adjustedgrade['grade'];
 237                          $course_item->grademax = $adjustedgrade['grademax'];
 238                          $course_item->grademin = $adjustedgrade['grademin'];
 239                      }
 240                  } else {
 241                      // We must use the specific max/min because it can be different for
 242                      // each grade_grade when items are excluded from sum of grades.
 243                      if (!is_null($finalgrade)) {
 244                          $course_item->grademin = $course_grade->get_grade_min();
 245                          $course_item->grademax = $course_grade->get_grade_max();
 246                      }
 247                  }
 248  
 249                  $data = array($courselink, grade_format_gradevalue($finalgrade, $course_item, true));
 250  
 251                  if (!$this->showrank['any']) {
 252                      //nothing to do
 253  
 254                  } else if ($this->showrank[$course->id] && !is_null($finalgrade)) {
 255                      /// find the number of users with a higher grade
 256                      /// please note this can not work if hidden grades involved :-( to be fixed in 2.0
 257                      $params = array($finalgrade, $course_item->id);
 258                      $sql = "SELECT COUNT(DISTINCT(userid))
 259                                FROM {grade_grades}
 260                               WHERE finalgrade IS NOT NULL AND finalgrade > ?
 261                                     AND itemid = ?";
 262                      $rank = $DB->count_records_sql($sql, $params) + 1;
 263  
 264                      $data[] = "$rank/$numusers";
 265  
 266                  } else {
 267                      // No grade, no rank.
 268                      // Or this course wants rank hidden.
 269                      $data[] = '-';
 270                  }
 271  
 272                  $this->table->add_data($data);
 273              }
 274              return true;
 275  
 276          } else {
 277              echo $OUTPUT->notification(get_string('notenrolled', 'grades'), 'notifymessage');
 278              return false;
 279          }
 280      }
 281  
 282      /**
 283       * Prints or returns the HTML from the flexitable.
 284       * @param bool $return Whether or not to return the data instead of printing it directly.
 285       * @return string
 286       */
 287      public function print_table($return=false) {
 288          ob_start();
 289          $this->table->print_html();
 290          $html = ob_get_clean();
 291          if ($return) {
 292              return $html;
 293          } else {
 294              echo $html;
 295          }
 296      }
 297  
 298      /**
 299       * Print a table to show courses that the user is able to grade.
 300       */
 301      public function print_teacher_table() {
 302          $table = new html_table();
 303          $table->head = array(get_string('coursename', 'grades'));
 304          $table->data = null;
 305          foreach ($this->teachercourses as $courseid => $course) {
 306              $url = new moodle_url('/grade/report/index.php', array('id' => $courseid));
 307              $table->data[] = array(html_writer::link($url, $course->fullname));
 308          }
 309          echo html_writer::table($table);
 310      }
 311  
 312      /**
 313       * Processes the data sent by the form (grades and feedbacks).
 314       * @param array $data
 315       * @return bool Success or Failure (array of errors).
 316       */
 317      function process_data($data) {
 318      }
 319      function process_action($target, $action) {
 320      }
 321  
 322      /**
 323       * This report supports being set as the 'grades' report.
 324       */
 325      public static function supports_mygrades() {
 326          return true;
 327      }
 328  }
 329  
 330  function grade_report_overview_settings_definition(&$mform) {
 331      global $CFG;
 332  
 333      //show rank
 334      $options = array(-1 => get_string('default', 'grades'),
 335                        0 => get_string('hide'),
 336                        1 => get_string('show'));
 337  
 338      if (empty($CFG->grade_report_overview_showrank)) {
 339          $options[-1] = get_string('defaultprev', 'grades', $options[0]);
 340      } else {
 341          $options[-1] = get_string('defaultprev', 'grades', $options[1]);
 342      }
 343  
 344      $mform->addElement('select', 'report_overview_showrank', get_string('showrank', 'grades'), $options);
 345      $mform->addHelpButton('report_overview_showrank', 'showrank', 'grades');
 346  
 347      //showtotalsifcontainhidden
 348      $options = array(-1 => get_string('default', 'grades'),
 349                        GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN => get_string('hide'),
 350                        GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowexhiddenitems', 'grades'),
 351                        GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowinchiddenitems', 'grades') );
 352  
 353      if (!array_key_exists($CFG->grade_report_overview_showtotalsifcontainhidden, $options)) {
 354          $options[-1] = get_string('defaultprev', 'grades', $options[0]);
 355      } else {
 356          $options[-1] = get_string('defaultprev', 'grades', $options[$CFG->grade_report_overview_showtotalsifcontainhidden]);
 357      }
 358  
 359      $mform->addElement('select', 'report_overview_showtotalsifcontainhidden', get_string('hidetotalifhiddenitems', 'grades'), $options);
 360      $mform->addHelpButton('report_overview_showtotalsifcontainhidden', 'hidetotalifhiddenitems', 'grades');
 361  }
 362  
 363  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1