[ Index ]

PHP Cross Reference of Unnamed Project

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 label for the select box that chooses items for this page.
  76       * @return string
  77       */
  78      public function select_label() {
  79          return get_string('selectuser', 'gradereport_singleview');
  80      }
  81  
  82      /**
  83       * Get the description of this page
  84       * @return string
  85       */
  86      public function description() {
  87          return get_string('users');
  88      }
  89  
  90      /**
  91       * Convert this list of items into an options list
  92       *
  93       * @return array
  94       */
  95      public function options() {
  96          $options = array();
  97          foreach ($this->items as $userid => $user) {
  98              $options[$userid] = fullname($user);
  99          }
 100  
 101          return $options;
 102      }
 103  
 104      /**
 105       * Return the type of the things in this list.
 106       * @return string
 107       */
 108      public function item_type() {
 109          return 'user';
 110      }
 111  
 112      /**
 113       * Get the original settings for this item
 114       * @return array
 115       */
 116      public function original_definition() {
 117          $def = array('finalgrade', 'feedback');
 118  
 119          $def[] = 'override';
 120  
 121          $def[] = 'exclude';
 122  
 123          return $def;
 124      }
 125  
 126      /**
 127       * Init this page
 128       *
 129       * @param bool $selfitemisempty True if we have not selected a user.
 130       */
 131      public function init($selfitemisempty = false) {
 132  
 133          $this->items = $this->load_users();
 134          $this->totalitemcount = count($this->items);
 135  
 136          if ($selfitemisempty) {
 137              return;
 138          }
 139  
 140          $params = array(
 141              'id' => $this->itemid,
 142              'courseid' => $this->courseid
 143          );
 144  
 145          $this->item = grade_item::fetch($params);
 146          if (!self::filter($this->item)) {
 147              $this->items = array();
 148              $this->set_init_error(get_string('gradeitemcannotbeoverridden', 'gradereport_singleview'));
 149          }
 150  
 151          $this->requiresextra = !$this->item->is_manual_item();
 152  
 153          $this->setup_structure();
 154  
 155          $this->set_definition($this->original_definition());
 156          $this->set_headers($this->original_headers());
 157      }
 158  
 159      /**
 160       * Get the table headers
 161       *
 162       * @return array
 163       */
 164      public function original_headers() {
 165          return array(
 166              '', // For filter icon.
 167              get_string('firstname') . ' (' . get_string('alternatename') . ') ' . get_string('lastname'),
 168              get_string('range', 'grades'),
 169              get_string('grade', 'grades'),
 170              get_string('feedback', 'grades'),
 171              $this->make_toggle_links('override'),
 172              $this->make_toggle_links('exclude')
 173          );
 174      }
 175  
 176      /**
 177       * Format a row in the table
 178       *
 179       * @param user $item
 180       * @return string
 181       */
 182      public function format_line($item) {
 183          global $OUTPUT;
 184  
 185          $grade = $this->fetch_grade_or_default($this->item, $item->id);
 186  
 187          $lockicon = '';
 188  
 189          $lockedgrade = $lockedgradeitem = 0;
 190          if (!empty($grade->locked)) {
 191              $lockedgrade = 1;
 192          }
 193          if (!empty($grade->grade_item->locked)) {
 194              $lockedgradeitem = 1;
 195          }
 196          // Check both grade and grade item.
 197          if ( $lockedgrade || $lockedgradeitem ) {
 198              $lockicon = $OUTPUT->pix_icon('t/locked', 'grade is locked') . ' ';
 199          }
 200  
 201          if (!empty($item->alternatename)) {
 202              $fullname = $lockicon . $item->alternatename . ' (' . $item->firstname . ') ' . $item->lastname;
 203          } else {
 204              $fullname = $lockicon . fullname($item);
 205          }
 206  
 207          $item->imagealt = $fullname;
 208          $url = new moodle_url("/user/view.php", array('id' => $item->id, 'course' => $this->courseid));
 209          $iconstring = get_string('filtergrades', 'gradereport_singleview', $fullname);
 210          $grade->label = $fullname;
 211  
 212          $line = array(
 213              $OUTPUT->action_icon($this->format_link('user', $item->id), new pix_icon('t/editstring', $iconstring)),
 214              $OUTPUT->user_picture($item, array('visibletoscreenreaders' => false)) .
 215              html_writer::link($url, $fullname),
 216              $this->item_range()
 217          );
 218          $lineclasses = array(
 219              "action",
 220              "user",
 221              "range"
 222          );
 223          $outputline = array();
 224          $i = 0;
 225          foreach ($line as $key => $value) {
 226              $cell = new \html_table_cell($value);
 227              if ($isheader = $i == 1) {
 228                  $cell->header = $isheader;
 229                  $cell->scope = "row";
 230              }
 231              if (array_key_exists($key, $lineclasses)) {
 232                  $cell->attributes['class'] = $lineclasses[$key];
 233              }
 234              $outputline[] = $cell;
 235              $i++;
 236          }
 237  
 238          return $this->format_definition($outputline, $grade);
 239      }
 240  
 241      /**
 242       * Get the range ui element for this grade_item
 243       *
 244       * @return element;
 245       */
 246      public function item_range() {
 247          if (empty($this->range)) {
 248              $this->range = new range($this->item);
 249          }
 250  
 251          return $this->range;
 252      }
 253  
 254      /**
 255       * Does this page require paging?
 256       *
 257       * @return bool
 258       */
 259      public function supports_paging() {
 260          return $this->requirespaging;
 261      }
 262  
 263      /**
 264       * Get the pager for this page.
 265       *
 266       * @return string
 267       */
 268      public function pager() {
 269          global $OUTPUT;
 270  
 271          return $OUTPUT->paging_bar(
 272              $this->totalitemcount, $this->page, $this->perpage,
 273              new moodle_url('/grade/report/singleview/index.php', array(
 274                  'perpage' => $this->perpage,
 275                  'id' => $this->courseid,
 276                  'group' => $this->groupid,
 277                  'itemid' => $this->itemid,
 278                  'item' => 'grade'
 279              ))
 280          );
 281      }
 282  
 283      /**
 284       * Get the heading for this page.
 285       *
 286       * @return string
 287       */
 288      public function heading() {
 289          return get_string('gradeitem', 'gradereport_singleview', $this->item->get_name());
 290      }
 291  
 292      /**
 293       * Get the summary for this table.
 294       *
 295       * @return string
 296       */
 297      public function summary() {
 298          return get_string('summarygrade', 'gradereport_singleview');
 299      }
 300  
 301      /**
 302       * Process the data from the form.
 303       *
 304       * @param array $data
 305       * @return array of warnings
 306       */
 307      public function process($data) {
 308          $bulk = new bulk_insert($this->item);
 309          // Bulk insert messages the data to be passed in
 310          // ie: for all grades of empty grades apply the specified value.
 311          if ($bulk->is_applied($data)) {
 312              $filter = $bulk->get_type($data);
 313              $insertvalue = $bulk->get_insert_value($data);
 314              // Appropriately massage data that may not exist.
 315              if ($this->supports_paging()) {
 316                  $gradeitem = grade_item::fetch(array(
 317                      'courseid' => $this->courseid,
 318                      'id' => $this->item->id
 319                  ));
 320  
 321                  $null = $gradeitem->gradetype == GRADE_TYPE_SCALE ? -1 : '';
 322  
 323                  foreach ($this->items as $itemid => $item) {
 324                      $field = "finalgrade_{$gradeitem->id}_{$itemid}";
 325                      if (isset($data->$field)) {
 326                          continue;
 327                      }
 328  
 329                      $grade = grade_grade::fetch(array(
 330                          'itemid' => $gradeitem->id,
 331                          'userid' => $itemid
 332                      ));
 333  
 334                      $data->$field = empty($grade) ? $null : $grade->finalgrade;
 335                      $data->{"old$field"} = $data->$field;
 336                  }
 337              }
 338  
 339              foreach ($data as $varname => $value) {
 340                  if (preg_match('/^oldoverride_(\d+)_(\d+)/', $varname, $matches)) {
 341                      // If we've selected overriding all grades.
 342                      if ($filter == 'all') {
 343                          $override = "override_{$matches[1]}_{$matches[2]}";
 344                          $data->$override = '1';
 345                      }
 346                  }
 347                  if (!preg_match('/^finalgrade_(\d+)_/', $varname, $matches)) {
 348                      continue;
 349                  }
 350  
 351                  $gradeitem = grade_item::fetch(array(
 352                      'courseid' => $this->courseid,
 353                      'id' => $matches[1]
 354                  ));
 355  
 356                  $isscale = ($gradeitem->gradetype == GRADE_TYPE_SCALE);
 357  
 358                  $empties = (trim($value) === '' or ($isscale and $value == -1));
 359  
 360                  if ($filter == 'all' or $empties) {
 361                      $data->$varname = ($isscale and empty($insertvalue)) ?
 362                          -1 : $insertvalue;
 363                  }
 364              }
 365          }
 366  
 367          return parent::process($data);
 368      }
 369  
 370  }


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