Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • Differences Between: [Versions 28 and 29] [Versions 28 and 30] [Versions 28 and 31] [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

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

    Search This Site: