[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/grade/report/singleview/classes/local/screen/ -> grade.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   * The screen with a list of users.
  19   *
  20   * @package   gradereport_singleview
  21   * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace gradereport_singleview\local\screen;
  26  
  27  use gradereport_singleview\local\ui\range;
  28  use gradereport_singleview\local\ui\bulk_insert;
  29  use grade_grade;
  30  use grade_item;
  31  use moodle_url;
  32  use pix_icon;
  33  use html_writer;
  34  use gradereport_singleview;
  35  
  36  defined('MOODLE_INTERNAL') || die;
  37  
  38  /**
  39   * The screen with a list of users.
  40   *
  41   * @package   gradereport_singleview
  42   * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
  43   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class grade extends tablelike implements selectable_items, filterable_items {
  46  
  47      /** @var int $totalitemcount Used for paging */
  48      private $totalitemcount = 0;
  49  
  50      /** @var bool $requiresextra True if this is a manual grade item */
  51      private $requiresextra = false;
  52  
  53      /** @var bool $requirepaging True if there are more users than our limit. */
  54      private $requirespaging = true;
  55  
  56      /**
  57       * True if $CFG->grade_overridecat is true
  58       *
  59       * @return bool
  60       */
  61      public static function allowcategories() {
  62          return get_config('moodle', 'grade_overridecat');
  63      }
  64  
  65      /**
  66       * Filter the list excluding category items (if required)?
  67       * @param grade_item $item The grade item.
  68       */
  69      public static function filter($item) {
  70          return get_config('moodle', 'grade_overridecat') ||
  71                  !($item->is_course_item() || $item->is_category_item());
  72      }
  73  
  74      /**
  75       * Get the description of this page
  76       * @return string
  77       */
  78      public function description() {
  79          return get_string('users');
  80      }
  81  
  82      /**
  83       * Convert this list of items into an options list
  84       *
  85       * @return array
  86       */
  87      public function options() {
  88          $options = array();
  89          foreach ($this->items as $userid => $user) {
  90              $options[$userid] = fullname($user);
  91          }
  92  
  93          return $options;
  94      }
  95  
  96      /**
  97       * Return the type of the things in this list.
  98       * @return string
  99       */
 100      public function item_type() {
 101          return 'user';
 102      }
 103  
 104      /**
 105       * Get the original settings for this item
 106       * @return array
 107       */
 108      public function original_definition() {
 109          $def = array('finalgrade', 'feedback');
 110  
 111          $def[] = 'override';
 112  
 113          $def[] = 'exclude';
 114  
 115          return $def;
 116      }
 117  
 118      /**
 119       * Init this page
 120       *
 121       * @param bool $selfitemisempty True if we have not selected a user.
 122       */
 123      public function init($selfitemisempty = false) {
 124          $roleids = explode(',', get_config('moodle', 'gradebookroles'));
 125  
 126          $this->items = get_role_users(
 127              $roleids, $this->context, false, '',
 128              'u.lastname, u.firstname', null, $this->groupid);
 129  
 130          $this->totalitemcount = count_role_users($roleids, $this->context);
 131  
 132          if ($selfitemisempty) {
 133              return;
 134          }
 135  
 136          $params = array(
 137              'id' => $this->itemid,
 138              'courseid' => $this->courseid
 139          );
 140  
 141          $this->item = grade_item::fetch($params);
 142          if (!self::filter($this->item)) {
 143              $this->items = array();
 144              $this->set_init_error(get_string('gradeitemcannotbeoverridden', 'gradereport_singleview'));
 145          }
 146  
 147          $this->requiresextra = !$this->item->is_manual_item();
 148  
 149          $this->setup_structure();
 150  
 151          $this->set_definition($this->original_definition());
 152          $this->set_headers($this->original_headers());
 153      }
 154  
 155      /**
 156       * Get the table headers
 157       *
 158       * @return array
 159       */
 160      public function original_headers() {
 161          return array(
 162              '', // For filter icon.
 163              get_string('firstname') . ' (' . get_string('alternatename') . ') ' . get_string('lastname'),
 164              get_string('range', 'grades'),
 165              get_string('grade', 'grades'),
 166              get_string('feedback', 'grades'),
 167              $this->make_toggle_links('override'),
 168              $this->make_toggle_links('exclude')
 169          );
 170      }
 171  
 172      /**
 173       * Format a row in the table
 174       *
 175       * @param user $item
 176       * @return string
 177       */
 178      public function format_line($item) {
 179          global $OUTPUT;
 180  
 181          $grade = $this->fetch_grade_or_default($this->item, $item->id);
 182  
 183          $lockicon = '';
 184  
 185          $lockedgrade = $lockedgradeitem = 0;
 186          if (!empty($grade->locked)) {
 187              $lockedgrade = 1;
 188          }
 189          if (!empty($grade->grade_item->locked)) {
 190              $lockedgradeitem = 1;
 191          }
 192          // Check both grade and grade item.
 193          if ( $lockedgrade || $lockedgradeitem ) {
 194              $lockicon = $OUTPUT->pix_icon('t/locked', 'grade is locked') . ' ';
 195          }
 196  
 197          if (!empty($item->alternatename)) {
 198              $fullname = $lockicon . $item->alternatename . ' (' . $item->firstname . ') ' . $item->lastname;
 199          } else {
 200              $fullname = $lockicon . fullname($item);
 201          }
 202  
 203          $item->imagealt = $fullname;
 204          $url = new moodle_url("/user/view.php", array('id' => $item->id, 'course' => $this->courseid));
 205          $iconstring = get_string('filtergrades', 'gradereport_singleview', $fullname);
 206          $grade->label = $fullname;
 207  
 208          $line = array(
 209              $OUTPUT->action_icon($this->format_link('user', $item->id), new pix_icon('t/editstring', $iconstring)),
 210              $OUTPUT->user_picture($item, array('visibletoscreenreaders' => false)) .
 211              html_writer::link($url, $fullname),
 212              $this->item_range()
 213          );
 214          $lineclasses = array(
 215              "action",
 216              "user",
 217              "range"
 218          );
 219          $outputline = array();
 220          $i = 0;
 221          foreach ($line as $key => $value) {
 222              $cell = new \html_table_cell($value);
 223              if ($isheader = $i == 1) {
 224                  $cell->header = $isheader;
 225                  $cell->scope = "row";
 226              }
 227              if (array_key_exists($key, $lineclasses)) {
 228                  $cell->attributes['class'] = $lineclasses[$key];
 229              }
 230              $outputline[] = $cell;
 231              $i++;
 232          }
 233  
 234          return $this->format_definition($outputline, $grade);
 235      }
 236  
 237      /**
 238       * Get the range ui element for this grade_item
 239       *
 240       * @return element;
 241       */
 242      public function item_range() {
 243          if (empty($this->range)) {
 244              $this->range = new range($this->item);
 245          }
 246  
 247          return $this->range;
 248      }
 249  
 250      /**
 251       * Does this page require paging?
 252       *
 253       * @return bool
 254       */
 255      public function supports_paging() {
 256          return $this->requirespaging;
 257      }
 258  
 259      /**
 260       * Get the pager for this page.
 261       *
 262       * @return string
 263       */
 264      public function pager() {
 265          global $OUTPUT;
 266  
 267          return $OUTPUT->paging_bar(
 268              $this->totalitemcount, $this->page, $this->perpage,
 269              new moodle_url('/grade/report/singleview/index.php', array(
 270                  'perpage' => $this->perpage,
 271                  'id' => $this->courseid,
 272                  'groupid' => $this->groupid,
 273                  'itemid' => $this->itemid,
 274                  'item' => 'grade'
 275              ))
 276          );
 277      }
 278  
 279      /**
 280       * Get the heading for this page.
 281       *
 282       * @return string
 283       */
 284      public function heading() {
 285          return $this->item->get_name();
 286      }
 287  
 288      /**
 289       * Get the summary for this table.
 290       *
 291       * @return string
 292       */
 293      public function summary() {
 294          return get_string('summarygrade', 'gradereport_singleview');
 295      }
 296  
 297      /**
 298       * Process the data from the form.
 299       *
 300       * @param array $data
 301       * @return array of warnings
 302       */
 303      public function process($data) {
 304          $bulk = new bulk_insert($this->item);
 305          // Bulk insert messages the data to be passed in
 306          // ie: for all grades of empty grades apply the specified value.
 307          if ($bulk->is_applied($data)) {
 308              $filter = $bulk->get_type($data);
 309              $insertvalue = $bulk->get_insert_value($data);
 310              // Appropriately massage data that may not exist.
 311              if ($this->supports_paging()) {
 312                  $gradeitem = grade_item::fetch(array(
 313                      'courseid' => $this->courseid,
 314                      'id' => $this->item->id
 315                  ));
 316  
 317                  $null = $gradeitem->gradetype == GRADE_TYPE_SCALE ? -1 : '';
 318  
 319                  foreach ($this->items as $itemid => $item) {
 320                      $field = "finalgrade_{$gradeitem->id}_{$itemid}";
 321                      if (isset($data->$field)) {
 322                          continue;
 323                      }
 324  
 325                      $grade = grade_grade::fetch(array(
 326                          'itemid' => $gradeitem->id,
 327                          'userid' => $itemid
 328                      ));
 329  
 330                      $data->$field = empty($grade) ? $null : $grade->finalgrade;
 331                      $data->{"old$field"} = $data->$field;
 332  
 333                      preg_match('/_(\d+)_(\d+)/', $field, $oldoverride);
 334                      $oldoverride = 'oldoverride' . $oldoverride[0];
 335                      if (empty($data->$oldoverride)) {
 336                          $data->$field = (!isset($grade->rawgrade)) ? $null : $grade->rawgrade;
 337                      }
 338                  }
 339              }
 340  
 341              foreach ($data as $varname => $value) {
 342                  if (preg_match('/override_(\d+)_(\d+)/', $varname, $matches)) {
 343                      $data->$matches[0] = '1';
 344                  }
 345                  if (!preg_match('/^finalgrade_(\d+)_/', $varname, $matches)) {
 346                      continue;
 347                  }
 348  
 349                  $gradeitem = grade_item::fetch(array(
 350                      'courseid' => $this->courseid,
 351                      'id' => $matches[1]
 352                  ));
 353  
 354                  $isscale = ($gradeitem->gradetype == GRADE_TYPE_SCALE);
 355  
 356                  $empties = (trim($value) === '' or ($isscale and $value == -1));
 357  
 358                  if ($filter == 'all' or $empties) {
 359                      $data->$varname = ($isscale and empty($insertvalue)) ?
 360                          -1 : $insertvalue;
 361                  }
 362              }
 363          }
 364  
 365          return parent::process($data);
 366      }
 367  
 368  }


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