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 user screen.
      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 grade_seq;
      28  use gradereport_singleview;
      29  use moodle_url;
      30  use pix_icon;
      31  use html_writer;
      32  use gradereport_singleview\local\ui\range;
      33  use gradereport_singleview\local\ui\bulk_insert;
      34  use grade_item;
      35  use grade_grade;
      36  use stdClass;
      37  
      38  defined('MOODLE_INTERNAL') || die;
      39  
      40  /**
      41   * The user screen.
      42   *
      43   * @package   gradereport_singleview
      44   * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
      45   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      46   */
      47  class user extends tablelike implements selectable_items {
      48  
      49      /** @var array $categories A cache for grade_item categories */
      50      private $categories = array();
      51  
      52      /** @var int $requirespaging Do we have more items than the paging limit? */
      53      private $requirespaging = true;
      54  
      55      /**
      56       * Get the description for the screen.
      57       *
      58       * @return string
      59       */
      60      public function description() {
      61          return get_string('gradeitems', 'grades');
      62      }
      63  
      64      /**
      65       * Convert the list of items to a list of options.
      66       *
      67       * @return array
      68       */
      69      public function options() {
      70          $result = array();
      71          foreach ($this->items as $itemid => $item) {
      72              $result[$itemid] = $item->get_name();
      73          }
      74          return $result;
      75      }
      76  
      77      /**
      78       * Get the type of items on this screen.
      79       *
      80       * @return string
      81       */
      82      public function item_type() {
      83          return 'grade';
      84      }
      85  
      86      /**
      87       * Should we show the group selector on this screen?
      88       *
      89       * @return bool
      90       */
      91      public function display_group_selector() {
      92          return false;
      93      }
      94  
      95      /**
      96       * Init the screen
      97       *
      98       * @param bool $selfitemisempty Have we selected an item yet?
      99       */
     100      public function init($selfitemisempty = false) {
     101          global $DB;
     102  
     103          if (!$selfitemisempty) {
     104              $validusers = $this->load_users();
     105              if (!isset($validusers[$this->itemid])) {
     106                  print_error('invaliduserid');
     107              }
     108              $this->item = $validusers[$this->itemid];
     109          }
     110  
     111          $params = array('courseid' => $this->courseid);
     112  
     113          $seq = new grade_seq($this->courseid, true);
     114  
     115          $this->items = array();
     116          foreach ($seq->items as $itemid => $item) {
     117              if (grade::filter($item)) {
     118                  $this->items[$itemid] = $item;
     119              }
     120          }
     121  
     122          $this->requirespaging = count($this->items) > $this->perpage;
     123  
     124          $this->setup_structure();
     125  
     126          $this->definition = array(
     127              'finalgrade', 'feedback', 'override', 'exclude'
     128          );
     129          $this->set_headers($this->original_headers());
     130      }
     131  
     132      /**
     133       * Get the list of headers for the table.
     134       *
     135       * @return array List of headers
     136       */
     137      public function original_headers() {
     138          return array(
     139              '', // For filter icon.
     140              get_string('assessmentname', 'gradereport_singleview'),
     141              get_string('gradecategory', 'grades'),
     142              get_string('range', 'grades'),
     143              get_string('grade', 'grades'),
     144              get_string('feedback', 'grades'),
     145              $this->make_toggle_links('override'),
     146              $this->make_toggle_links('exclude')
     147          );
     148      }
     149  
     150      /**
     151       * Format each row of the table.
     152       *
     153       * @param grade_item $item
     154       * @return string
     155       */
     156      public function format_line($item) {
     157          global $OUTPUT;
     158  
     159          $grade = $this->fetch_grade_or_default($item, $this->item->id);
     160          $lockicon = '';
     161  
     162          $lockeditem = $lockeditemgrade = 0;
     163          if (!empty($grade->locked)) {
     164              $lockeditem = 1;
     165          }
     166          if (!empty($grade->grade_item->locked)) {
     167              $lockeditemgrade = 1;
     168          }
     169          // Check both grade and grade item.
     170          if ($lockeditem || $lockeditemgrade) {
     171               $lockicon = $OUTPUT->pix_icon('t/locked', 'grade is locked');
     172          }
     173  
     174          $iconstring = get_string('filtergrades', 'gradereport_singleview', $item->get_name());
     175  
     176          // Create a fake gradetreeitem so we can call get_element_header().
     177          // The type logic below is from grade_category->_get_children_recursion().
     178          $gradetreeitem = array();
     179          if (in_array($item->itemtype, array('course', 'category'))) {
     180              $gradetreeitem['type'] = $item->itemtype.'item';
     181          } else {
     182              $gradetreeitem['type'] = 'item';
     183          }
     184          $gradetreeitem['object'] = $item;
     185          $gradetreeitem['userid'] = $this->item->id;
     186  
     187          $itemlabel = $this->structure->get_element_header($gradetreeitem, true, false, false, false, true);
     188          $grade->label = $item->get_name();
     189  
     190          $line = array(
     191              $OUTPUT->action_icon($this->format_link('grade', $item->id), new pix_icon('t/editstring', $iconstring)),
     192              $this->format_icon($item) . $lockicon . $itemlabel,
     193              $this->category($item),
     194              new range($item)
     195          );
     196          $lineclasses = array(
     197              "action",
     198              "gradeitem",
     199              "category",
     200              "range"
     201          );
     202  
     203          $outputline = array();
     204          $i = 0;
     205          foreach ($line as $key => $value) {
     206              $cell = new \html_table_cell($value);
     207              if ($isheader = $i == 1) {
     208                  $cell->header = $isheader;
     209                  $cell->scope = "row";
     210              }
     211              if (array_key_exists($key, $lineclasses)) {
     212                  $cell->attributes['class'] = $lineclasses[$key];
     213              }
     214              $outputline[] = $cell;
     215              $i++;
     216          }
     217  
     218          return $this->format_definition($outputline, $grade);
     219      }
     220  
     221      /**
     222       * Helper to get the icon for an item.
     223       *
     224       * @param grade_item $item
     225       * @return string
     226       */
     227      private function format_icon($item) {
     228          $element = array('type' => 'item', 'object' => $item);
     229          return $this->structure->get_element_icon($element);
     230      }
     231  
     232      /**
     233       * Helper to get the category for an item.
     234       *
     235       * @param grade_item $item
     236       * @return grade_category
     237       */
     238      private function category($item) {
     239          global $DB;
     240  
     241          if (empty($item->categoryid)) {
     242  
     243              if ($item->itemtype == 'course') {
     244                  return $this->course->fullname;
     245              }
     246  
     247              $params = array('id' => $item->iteminstance);
     248              $elem = $DB->get_record('grade_categories', $params);
     249  
     250              return $elem->fullname;
     251          }
     252  
     253          if (!isset($this->categories[$item->categoryid])) {
     254              $category = $item->get_parent_category();
     255  
     256              $this->categories[$category->id] = $category;
     257          }
     258  
     259          return $this->categories[$item->categoryid]->get_name();
     260      }
     261  
     262      /**
     263       * Get the heading for the page.
     264       *
     265       * @return string
     266       */
     267      public function heading() {
     268          return fullname($this->item);
     269      }
     270  
     271      /**
     272       * Get the summary for this table.
     273       *
     274       * @return string
     275       */
     276      public function summary() {
     277          return get_string('summaryuser', 'gradereport_singleview');
     278      }
     279  
     280      /**
     281       * Default pager
     282       *
     283       * @return string
     284       */
     285      public function pager() {
     286          global $OUTPUT;
     287  
     288          if (!$this->supports_paging()) {
     289              return '';
     290          }
     291  
     292          return $OUTPUT->paging_bar(
     293              count($this->items), $this->page, $this->perpage,
     294              new moodle_url('/grade/report/singleview/index.php', array(
     295                  'perpage' => $this->perpage,
     296                  'id' => $this->courseid,
     297                  'group' => $this->groupid,
     298                  'itemid' => $this->itemid,
     299                  'item' => 'user'
     300              ))
     301          );
     302      }
     303  
     304      /**
     305       * Does this page require paging?
     306       *
     307       * @return bool
     308       */
     309      public function supports_paging() {
     310          return $this->requirespaging;
     311      }
     312  
     313  
     314      /**
     315       * Process the data from the form.
     316       *
     317       * @param array $data
     318       * @return array of warnings
     319       */
     320      public function process($data) {
     321          $bulk = new bulk_insert($this->item);
     322          // Bulk insert messages the data to be passed in
     323          // ie: for all grades of empty grades apply the specified value.
     324          if ($bulk->is_applied($data)) {
     325              $filter = $bulk->get_type($data);
     326              $insertvalue = $bulk->get_insert_value($data);
     327  
     328              $userid = $this->item->id;
     329              foreach ($this->items as $gradeitemid => $gradeitem) {
     330                  $null = $gradeitem->gradetype == GRADE_TYPE_SCALE ? -1 : '';
     331                  $field = "finalgrade_{$gradeitem->id}_{$this->itemid}";
     332                  if (isset($data->$field)) {
     333                      continue;
     334                  }
     335  
     336                  $oldfinalgradefield = "oldfinalgrade_{$gradeitem->id}_{$this->itemid}";
     337                  // Bulk grade changes for all grades need to be processed and shouldn't be skipped if they had a previous grade.
     338                  if ($gradeitem->is_course_item() || ($filter != 'all' && !empty($data->$oldfinalgradefield))) {
     339                      if ($gradeitem->is_course_item()) {
     340                          // The course total should not be overridden.
     341                          unset($data->$field);
     342                          unset($data->oldfinalgradefield);
     343                          $oldoverride = "oldoverride_{$gradeitem->id}_{$this->itemid}";
     344                          unset($data->$oldoverride);
     345                          $oldfeedback = "oldfeedback_{$gradeitem->id}_{$this->itemid}";
     346                          unset($data->$oldfeedback);
     347                      }
     348                      continue;
     349                  }
     350                  $grade = grade_grade::fetch(array(
     351                      'itemid' => $gradeitemid,
     352                      'userid' => $userid
     353                  ));
     354  
     355                  $data->$field = empty($grade) ? $null : $grade->finalgrade;
     356                  $data->{"old$field"} = $data->$field;
     357              }
     358  
     359              foreach ($data as $varname => $value) {
     360                  if (preg_match('/^oldoverride_(\d+)_(\d+)/', $varname, $matches)) {
     361                      // If we've selected overriding all grades.
     362                      if ($filter == 'all') {
     363                          $override = "override_{$matches[1]}_{$matches[2]}";
     364                          $data->$override = '1';
     365                      }
     366                  }
     367                  if (!preg_match('/^finalgrade_(\d+)_(\d+)/', $varname, $matches)) {
     368                      continue;
     369                  }
     370  
     371                  $gradeitem = grade_item::fetch(array(
     372                      'courseid' => $this->courseid,
     373                      'id' => $matches[1]
     374                  ));
     375  
     376                  $isscale = ($gradeitem->gradetype == GRADE_TYPE_SCALE);
     377  
     378                  $empties = (trim($value) === '' or ($isscale and $value == -1));
     379  
     380                  if ($filter == 'all' or $empties) {
     381                      $data->$varname = ($isscale and empty($insertvalue)) ?
     382                          -1 : $insertvalue;
     383                  }
     384              }
     385          }
     386          return parent::process($data);
     387      }
     388  }
    

    Search This Site: