[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/grade/edit/tree/ -> 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   * A library of classes used by the grade edit pages
  19   *
  20   * @package   core_grades
  21   * @copyright 2009 Nicolas Connault
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  class grade_edit_tree {
  26      public $columns = array();
  27  
  28      /**
  29       * @var grade_tree $gtree   @see grade/lib.php
  30       */
  31      public $gtree;
  32  
  33      /**
  34       * @var grade_plugin_return @see grade/lib.php
  35       */
  36      public $gpr;
  37  
  38      /**
  39       * @var string              $moving The eid of the category or item being moved
  40       */
  41      public $moving;
  42  
  43      public $deepest_level;
  44  
  45      public $uses_weight = false;
  46  
  47      public $table;
  48  
  49      public $categories = array();
  50  
  51      /**
  52       * Show calculator icons next to manual grade items
  53       * @var bool $show_calculations
  54       */
  55      private $show_calculations;
  56  
  57      /**
  58       * Constructor
  59       */
  60      public function __construct($gtree, $moving=false, $gpr) {
  61          global $USER, $OUTPUT, $COURSE;
  62  
  63          $systemdefault = get_config('moodle', 'grade_report_showcalculations');
  64          $this->show_calculations = get_user_preferences('grade_report_showcalculations', $systemdefault);
  65  
  66          $this->gtree = $gtree;
  67          $this->moving = $moving;
  68          $this->gpr = $gpr;
  69          $this->deepest_level = $this->get_deepest_level($this->gtree->top_element);
  70  
  71          $this->columns = array(grade_edit_tree_column::factory('name', array('deepest_level' => $this->deepest_level)));
  72  
  73          if ($this->uses_weight) {
  74              $this->columns[] = grade_edit_tree_column::factory('weight', array('adv' => 'weight'));
  75          }
  76  
  77          $this->columns[] = grade_edit_tree_column::factory('range'); // This is not a setting... How do we deal with it?
  78          $this->columns[] = grade_edit_tree_column::factory('actions');
  79  
  80          if ($this->deepest_level > 1) {
  81              $this->columns[] = grade_edit_tree_column::factory('select');
  82          }
  83  
  84          $this->table = new html_table();
  85          $this->table->id = "grade_edit_tree_table";
  86          $this->table->attributes['class'] = 'generaltable simple setup-grades';
  87          if ($this->moving) {
  88              $this->table->attributes['class'] .= ' moving';
  89          }
  90  
  91          foreach ($this->columns as $column) {
  92              if (!($this->moving && $column->hide_when_moving)) {
  93                  $this->table->head[] = $column->get_header_cell();
  94              }
  95          }
  96  
  97          $rowcount = 0;
  98          $this->table->data = $this->build_html_tree($this->gtree->top_element, true, array(), 0, $rowcount);
  99      }
 100  
 101      /**
 102       * Recursive function for building the table holding the grade categories and items,
 103       * with CSS indentation and styles.
 104       *
 105       * @param array   $element The current tree element being rendered
 106       * @param boolean $totals Whether or not to print category grade items (category totals)
 107       * @param array   $parents An array of parent categories for the current element (used for indentation and row classes)
 108       *
 109       * @return string HTML
 110       */
 111      public function build_html_tree($element, $totals, $parents, $level, &$row_count) {
 112          global $CFG, $COURSE, $PAGE, $OUTPUT;
 113  
 114          $object = $element['object'];
 115          $eid    = $element['eid'];
 116          $object->name = $this->gtree->get_element_header($element, true, true, true, true, true);
 117          $object->stripped_name = $this->gtree->get_element_header($element, false, false, false);
 118  
 119          $is_category_item = false;
 120          if ($element['type'] == 'categoryitem' || $element['type'] == 'courseitem') {
 121              $is_category_item = true;
 122          }
 123  
 124          $rowclasses = array();
 125          foreach ($parents as $parent_eid) {
 126              $rowclasses[] = $parent_eid;
 127          }
 128  
 129          $moveaction = '';
 130          $actionsmenu = new action_menu();
 131          $actionsmenu->initialise_js($PAGE);
 132          $actionsmenu->set_menu_trigger(get_string('edit'));
 133          $actionsmenu->set_owner_selector('grade-item-' . $eid);
 134          $actionsmenu->set_alignment(action_menu::TL, action_menu::BL);
 135  
 136          if (!$is_category_item && ($icon = $this->gtree->get_edit_icon($element, $this->gpr, true))) {
 137              $actionsmenu->add($icon);
 138          }
 139  
 140          if ($this->show_calculations && ($icon = $this->gtree->get_calculation_icon($element, $this->gpr, true))) {
 141              $actionsmenu->add($icon);
 142          }
 143  
 144          if ($element['type'] == 'item' or ($element['type'] == 'category' and $element['depth'] > 1)) {
 145              if ($this->element_deletable($element)) {
 146                  $aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'delete', 'eid' => $eid, 'sesskey' => sesskey()));
 147                  $icon = new action_menu_link_secondary($aurl, new pix_icon('t/delete', get_string('delete')), get_string('delete'));
 148                  $actionsmenu->add($icon);
 149              }
 150  
 151              $aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'moveselect', 'eid' => $eid, 'sesskey' => sesskey()));
 152              $moveaction .= $OUTPUT->action_icon($aurl, new pix_icon('t/move', get_string('move')));
 153          }
 154  
 155          if ($icon = $this->gtree->get_hiding_icon($element, $this->gpr, true)) {
 156              $actionsmenu->add($icon);
 157          }
 158  
 159          if ($icon = $this->gtree->get_reset_icon($element, $this->gpr, true)) {
 160              $actionsmenu->add($icon);
 161          }
 162  
 163          $actions = $OUTPUT->render($actionsmenu);
 164  
 165          $returnrows = array();
 166          $root = false;
 167  
 168          $id = required_param('id', PARAM_INT);
 169  
 170          /// prepare move target if needed
 171          $last = '';
 172  
 173          /// print the list items now
 174          if ($this->moving == $eid) {
 175              // do not diplay children
 176              $cell = new html_table_cell();
 177              $cell->colspan = 12;
 178              $cell->attributes['class'] = $element['type'] . ' moving column-name level' .
 179                  ($level + 1) . ' level' . ($level % 2 ? 'even' : 'odd');
 180              $cell->text = $object->name.' ('.get_string('move').')';
 181              return array(new html_table_row(array($cell)));
 182          }
 183  
 184          if ($element['type'] == 'category') {
 185              $level++;
 186              $this->categories[$object->id] = $object->stripped_name;
 187              $category = grade_category::fetch(array('id' => $object->id));
 188              $item = $category->get_grade_item();
 189  
 190              // Add aggregation coef input if not a course item and if parent category has correct aggregation type
 191              $dimmed = ($item->is_hidden()) ? 'dimmed_text' : '';
 192  
 193              // Before we print the category's row, we must find out how many rows will appear below it (for the filler cell's rowspan)
 194              $aggregation_position = grade_get_setting($COURSE->id, 'aggregationposition', $CFG->grade_aggregationposition);
 195              $category_total_data = null; // Used if aggregationposition is set to "last", so we can print it last
 196  
 197              $html_children = array();
 198  
 199              $row_count = 0;
 200  
 201              foreach($element['children'] as $child_el) {
 202                  $moveto = null;
 203  
 204                  if (empty($child_el['object']->itemtype)) {
 205                      $child_el['object']->itemtype = false;
 206                  }
 207  
 208                  if (($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') && !$totals) {
 209                      continue;
 210                  }
 211  
 212                  $child_eid = $child_el['eid'];
 213                  $first = '';
 214  
 215                  if ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') {
 216                      $first = array('first' => 1);
 217                      $child_eid = $eid;
 218                  }
 219  
 220                  if ($this->moving && $this->moving != $child_eid) {
 221  
 222                      $strmove     = get_string('move');
 223                      $strmovehere = get_string('movehere');
 224                      $actions = $moveaction = ''; // no action icons when moving
 225  
 226                      $aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'move', 'eid' => $this->moving, 'moveafter' => $child_eid, 'sesskey' => sesskey()));
 227                      if ($first) {
 228                          $aurl->params($first);
 229                      }
 230  
 231                      $cell = new html_table_cell();
 232                      $cell->colspan = 12;
 233                      $cell->attributes['class'] = 'movehere level' . ($level + 1) . ' level' . ($level % 2 ? 'even' : 'odd');
 234  
 235                      $icon = new pix_icon('movehere', $strmovehere, null, array('class'=>'movetarget'));
 236                      $cell->text = $OUTPUT->action_icon($aurl, $icon);
 237  
 238                      $moveto = new html_table_row(array($cell));
 239                  }
 240  
 241                  $newparents = $parents;
 242                  $newparents[] = $eid;
 243  
 244                  $row_count++;
 245                  $child_row_count = 0;
 246  
 247                  // If moving, do not print course and category totals, but still print the moveto target box
 248                  if ($this->moving && ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category')) {
 249                      $html_children[] = $moveto;
 250                  } elseif ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') {
 251                      // We don't build the item yet because we first need to know the deepest level of categories (for category/name colspans)
 252                      $category_total_item = $this->build_html_tree($child_el, $totals, $newparents, $level, $child_row_count);
 253                      if (!$aggregation_position) {
 254                          $html_children = array_merge($html_children, $category_total_item);
 255                      }
 256                  } else {
 257                      $html_children = array_merge($html_children, $this->build_html_tree($child_el, $totals, $newparents, $level, $child_row_count));
 258                      if (!empty($moveto)) {
 259                          $html_children[] = $moveto;
 260                      }
 261  
 262                      if ($this->moving) {
 263                          $row_count++;
 264                      }
 265                  }
 266  
 267                  $row_count += $child_row_count;
 268  
 269                  // If the child is a category, increment row_count by one more (for the extra coloured row)
 270                  if ($child_el['type'] == 'category') {
 271                      $row_count++;
 272                  }
 273              }
 274  
 275              // Print category total at the end if aggregation position is "last" (1)
 276              if (!empty($category_total_item) && $aggregation_position) {
 277                  $html_children = array_merge($html_children, $category_total_item);
 278              }
 279  
 280              // Determine if we are at the root
 281              if (isset($element['object']->grade_item) && $element['object']->grade_item->is_course_item()) {
 282                  $root = true;
 283              }
 284  
 285              $levelclass = "level$level level" . ($level % 2 ? 'odd' : 'even');
 286  
 287              $courseclass = '';
 288              if ($level == 1) {
 289                  $courseclass = 'coursecategory';
 290              }
 291  
 292              $row = new html_table_row();
 293              $row->id = 'grade-item-' . $eid;
 294              $row->attributes['class'] = $courseclass . ' category ' . $dimmed;
 295              foreach ($rowclasses as $class) {
 296                  $row->attributes['class'] .= ' ' . $class;
 297              }
 298  
 299              $headercell = new html_table_cell();
 300              $headercell->header = true;
 301              $headercell->scope = 'row';
 302              $headercell->attributes['title'] = $object->stripped_name;
 303              $headercell->attributes['class'] = 'cell column-rowspan rowspan ' . $levelclass;
 304              $headercell->rowspan = $row_count + 1;
 305              $row->cells[] = $headercell;
 306  
 307              foreach ($this->columns as $column) {
 308                  if (!($this->moving && $column->hide_when_moving)) {
 309                      $row->cells[] = $column->get_category_cell($category, $levelclass, array('id' => $id,
 310                          'name' => $object->name, 'level' => $level, 'actions' => $actions,
 311                          'moveaction' => $moveaction, 'eid' => $eid));
 312                  }
 313              }
 314  
 315              $returnrows[] = $row;
 316  
 317              $returnrows = array_merge($returnrows, $html_children);
 318  
 319              // Print a coloured row to show the end of the category across the table
 320              $endcell = new html_table_cell();
 321              $endcell->colspan = (19 - $level);
 322              $endcell->attributes['class'] = 'emptyrow colspan ' . $levelclass;
 323  
 324              $returnrows[] = new html_table_row(array($endcell));
 325  
 326          } else { // Dealing with a grade item
 327  
 328              $item = grade_item::fetch(array('id' => $object->id));
 329              $element['type'] = 'item';
 330              $element['object'] = $item;
 331  
 332              $categoryitemclass = '';
 333              if ($item->itemtype == 'category') {
 334                  $categoryitemclass = 'categoryitem';
 335              }
 336              if ($item->itemtype == 'course') {
 337                  $categoryitemclass = 'courseitem';
 338              }
 339  
 340              $dimmed = ($item->is_hidden()) ? "dimmed_text" : "";
 341              $gradeitemrow = new html_table_row();
 342              $gradeitemrow->id = 'grade-item-' . $eid;
 343              $gradeitemrow->attributes['class'] = $categoryitemclass . ' item ' . $dimmed;
 344              foreach ($rowclasses as $class) {
 345                  $gradeitemrow->attributes['class'] .= ' ' . $class;
 346              }
 347  
 348              foreach ($this->columns as $column) {
 349                  if (!($this->moving && $column->hide_when_moving)) {
 350                      $gradeitemrow->cells[] = $column->get_item_cell($item, array('id' => $id, 'name' => $object->name,
 351                          'level' => $level, 'actions' => $actions, 'element' => $element, 'eid' => $eid,
 352                          'moveaction' => $moveaction, 'itemtype' => $object->itemtype));
 353                  }
 354              }
 355  
 356              $returnrows[] = $gradeitemrow;
 357          }
 358  
 359          return $returnrows;
 360  
 361      }
 362  
 363      /**
 364       * Given a grade_item object, returns a labelled input if an aggregation coefficient (weight or extra credit) applies to it.
 365       * @param grade_item $item
 366       * @return string HTML
 367       */
 368      static function get_weight_input($item) {
 369          global $OUTPUT;
 370  
 371          if (!is_object($item) || get_class($item) !== 'grade_item') {
 372              throw new Exception('grade_edit_tree::get_weight_input($item) was given a variable that is not of the required type (grade_item object)');
 373              return false;
 374          }
 375  
 376          if ($item->is_course_item()) {
 377              return '';
 378          }
 379  
 380          $parent_category = $item->get_parent_category();
 381          $parent_category->apply_forced_settings();
 382          $aggcoef = $item->get_coefstring();
 383  
 384          $itemname = $item->itemname;
 385          if ($item->is_category_item()) {
 386              // Remember, the parent category of a category item is the category itself.
 387              $itemname = $parent_category->get_name();
 388          }
 389          $str = '';
 390  
 391          if ($aggcoef == 'aggregationcoefweight' || $aggcoef == 'aggregationcoef' || $aggcoef == 'aggregationcoefextraweight') {
 392              return '<label class="accesshide" for="weight_'.$item->id.'">'.
 393                  get_string('extracreditvalue', 'grades', $itemname).'</label>'.
 394                  '<input type="text" size="6" id="weight_'.$item->id.'" name="weight_'.$item->id.'"
 395                  value="'.grade_edit_tree::format_number($item->aggregationcoef).'" />';
 396          } else if ($aggcoef == 'aggregationcoefextraweightsum') {
 397  
 398              $checkboxname = 'weightoverride_' . $item->id;
 399              $checkboxlbl = html_writer::tag('label', get_string('overrideweightofa', 'grades', $itemname),
 400                  array('for' => $checkboxname, 'class' => 'accesshide'));
 401              $checkbox = html_writer::empty_tag('input', array('name' => $checkboxname,
 402                  'type' => 'hidden', 'value' => 0));
 403              $checkbox .= html_writer::empty_tag('input', array('name' => $checkboxname,
 404                  'type' => 'checkbox', 'value' => 1, 'id' => $checkboxname, 'class' => 'weightoverride',
 405                  'checked' => ($item->weightoverride ? 'checked' : null)));
 406  
 407              $name = 'weight_' . $item->id;
 408              $hiddenlabel = html_writer::tag(
 409                  'label',
 410                  get_string('weightofa', 'grades', $itemname),
 411                  array(
 412                      'class' => 'accesshide',
 413                      'for' => $name
 414                  )
 415              );
 416  
 417              $input = html_writer::empty_tag(
 418                  'input',
 419                  array(
 420                      'type' =>   'text',
 421                      'size' =>   6,
 422                      'id' =>     $name,
 423                      'name' =>   $name,
 424                      'value' =>  grade_edit_tree::format_number($item->aggregationcoef2 * 100.0),
 425                      'disabled' => ($item->weightoverride ? null : 'disabled')
 426                  )
 427              );
 428  
 429              $str .= $checkboxlbl . $checkbox . $hiddenlabel . $input;
 430  
 431              if ($item->aggregationcoef > 0) {
 432                  $str .= ' ' . html_writer::tag('abbr', get_string('aggregationcoefextrasumabbr', 'grades'),
 433                          array('title' => get_string('aggregationcoefextrasum', 'grades')));
 434              }
 435          }
 436  
 437          return $str;
 438      }
 439  
 440      //Trims trailing zeros
 441      //Used on the 'categories and items' page for grade items settings like aggregation co-efficient
 442      //Grader report has its own decimal place settings so they are handled elsewhere
 443      static function format_number($number) {
 444          $formatted = rtrim(format_float($number, 4),'0');
 445          if (substr($formatted, -1)==get_string('decsep', 'langconfig')) { //if last char is the decimal point
 446              $formatted .= '0';
 447          }
 448          return $formatted;
 449      }
 450  
 451      /**
 452       * Given an element of the grade tree, returns whether it is deletable or not (only manual grade items are deletable)
 453       *
 454       * @param array $element
 455       * @return bool
 456       */
 457      function element_deletable($element) {
 458          global $COURSE;
 459  
 460          if ($element['type'] != 'item') {
 461              return true;
 462          }
 463  
 464          $grade_item = $element['object'];
 465  
 466          if ($grade_item->itemtype != 'mod' or $grade_item->is_outcome_item() or $grade_item->gradetype == GRADE_TYPE_NONE) {
 467              return true;
 468          }
 469  
 470          $modinfo = get_fast_modinfo($COURSE);
 471          if (!isset($modinfo->instances[$grade_item->itemmodule][$grade_item->iteminstance])) {
 472              // module does not exist
 473              return true;
 474          }
 475  
 476          return false;
 477      }
 478  
 479      /**
 480       * Given the grade tree and an array of element ids (e.g. c15, i42), and expecting the 'moveafter' URL param,
 481       * moves the selected items to the requested location. Then redirects the user to the given $returnurl
 482       *
 483       * @param object $gtree The grade tree (a recursive representation of the grade categories and grade items)
 484       * @param array $eids
 485       * @param string $returnurl
 486       */
 487      function move_elements($eids, $returnurl) {
 488          $moveafter = required_param('moveafter', PARAM_INT);
 489  
 490          if (!is_array($eids)) {
 491              $eids = array($eids);
 492          }
 493  
 494          if(!$after_el = $this->gtree->locate_element("cg$moveafter")) {
 495              print_error('invalidelementid', '', $returnurl);
 496          }
 497  
 498          $after = $after_el['object'];
 499          $parent = $after;
 500          $sortorder = $after->get_sortorder();
 501  
 502          foreach ($eids as $eid) {
 503              if (!$element = $this->gtree->locate_element($eid)) {
 504                  print_error('invalidelementid', '', $returnurl);
 505              }
 506              $object = $element['object'];
 507  
 508              $object->set_parent($parent->id);
 509              $object->move_after_sortorder($sortorder);
 510              $sortorder++;
 511          }
 512  
 513          redirect($returnurl, '', 0);
 514      }
 515  
 516      /**
 517       * Recurses through the entire grade tree to find and return the maximum depth of the tree.
 518       * This should be run only once from the root element (course category), and is used for the
 519       * indentation of the Name column's cells (colspan)
 520       *
 521       * @param array $element An array of values representing a grade tree's element (all grade items in this case)
 522       * @param int $level The level of the current recursion
 523       * @param int $deepest_level A value passed to each subsequent level of recursion and incremented if $level > $deepest_level
 524       * @return int Deepest level
 525       */
 526      function get_deepest_level($element, $level=0, $deepest_level=1) {
 527          $object = $element['object'];
 528  
 529          $level++;
 530          $coefstring = $element['object']->get_coefstring();
 531          if ($element['type'] == 'category') {
 532              if ($coefstring == 'aggregationcoefweight' || $coefstring == 'aggregationcoefextraweightsum' ||
 533                      $coefstring == 'aggregationcoefextraweight') {
 534                  $this->uses_weight = true;
 535              }
 536  
 537              foreach($element['children'] as $child_el) {
 538                  if ($level > $deepest_level) {
 539                      $deepest_level = $level;
 540                  }
 541                  $deepest_level = $this->get_deepest_level($child_el, $level, $deepest_level);
 542              }
 543          }
 544  
 545          return $deepest_level;
 546      }
 547  }
 548  
 549  /**
 550   * Class grade_edit_tree_column
 551   *
 552   * @package   core_grades
 553   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 554   */
 555  abstract class grade_edit_tree_column {
 556      public $forced;
 557      public $hidden;
 558      public $forced_hidden;
 559      public $advanced_hidden;
 560      public $hide_when_moving = true;
 561      /**
 562       * html_table_cell object used as a template for header cells in all categories.
 563       * It must be cloned before being used.
 564       * @var html_table_cell $headercell
 565       */
 566      public $headercell;
 567      /**
 568       * html_table_cell object used as a template for category cells in all categories.
 569       * It must be cloned before being used.
 570       * @var html_table_cell $categorycell
 571       */
 572      public $categorycell;
 573      /**
 574       * html_table_cell object used as a template for item cells in all categories.
 575       * It must be cloned before being used.
 576       * @var html_table_cell $itemcell
 577       */
 578      public $itemcell;
 579  
 580      public static function factory($name, $params=array()) {
 581          $class_name = "grade_edit_tree_column_$name";
 582          if (class_exists($class_name)) {
 583              return new $class_name($params);
 584          }
 585      }
 586  
 587      public abstract function get_header_cell();
 588  
 589      public function get_category_cell($category, $levelclass, $params) {
 590          $cell = clone($this->categorycell);
 591          $cell->attributes['class'] .= ' ' . $levelclass;
 592          $cell->attributes['text'] = '';
 593          return $cell;
 594      }
 595  
 596      public function get_item_cell($item, $params) {
 597          $cell = clone($this->itemcell);
 598          $cell->attributes['text'] = '';
 599          if (isset($params['level'])) {
 600              $level = $params['level'] + (($item->itemtype == 'category' || $item->itemtype == 'course') ? 0 : 1);
 601              $cell->attributes['class'] .= ' level' . $level;
 602              $cell->attributes['class'] .= ' level' . ($level % 2 ? 'odd' : 'even');
 603          }
 604          return $cell;
 605      }
 606  
 607      public function __construct() {
 608          $this->headercell = new html_table_cell();
 609          $this->headercell->header = true;
 610          $this->headercell->attributes['class'] = 'header';
 611  
 612          $this->categorycell = new html_table_cell();
 613          $this->categorycell->attributes['class']  = 'cell';
 614  
 615          $this->itemcell = new html_table_cell();
 616          $this->itemcell->attributes['class'] = 'cell';
 617  
 618          if (preg_match('/^grade_edit_tree_column_(\w*)$/', get_class($this), $matches)) {
 619              $this->headercell->attributes['class'] .= ' column-' . $matches[1];
 620              $this->categorycell->attributes['class'] .= ' column-' . $matches[1];
 621              $this->itemcell->attributes['class'] .= ' column-' . $matches[1];
 622          }
 623      }
 624  }
 625  
 626  /**
 627   * Class grade_edit_tree_column_name
 628   *
 629   * @package   core_grades
 630   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 631   */
 632  class grade_edit_tree_column_name extends grade_edit_tree_column {
 633      public $forced = false;
 634      public $hidden = false;
 635      public $forced_hidden = false;
 636      public $advanced_hidden = false;
 637      public $deepest_level = 1;
 638      public $hide_when_moving = false;
 639  
 640      public function __construct($params) {
 641          if (empty($params['deepest_level'])) {
 642              throw new Exception('Tried to instantiate a grade_edit_tree_column_name object without the "deepest_level" param!');
 643          }
 644  
 645          $this->deepest_level = $params['deepest_level'];
 646          parent::__construct();
 647      }
 648  
 649      public function get_header_cell() {
 650          $headercell = clone($this->headercell);
 651          $headercell->colspan = $this->deepest_level + 1;
 652          $headercell->text = get_string('name');
 653          return $headercell;
 654      }
 655  
 656      public function get_category_cell($category, $levelclass, $params) {
 657          global $OUTPUT;
 658          if (empty($params['name']) || empty($params['level'])) {
 659              throw new Exception('Array key (name or level) missing from 3rd param of grade_edit_tree_column_name::get_category_cell($category, $levelclass, $params)');
 660          }
 661          $moveaction = isset($params['moveaction']) ? $params['moveaction'] : '';
 662          $categorycell = parent::get_category_cell($category, $levelclass, $params);
 663          $categorycell->colspan = ($this->deepest_level +1) - $params['level'];
 664          $categorycell->text = $OUTPUT->heading($moveaction . $params['name'], 4);
 665          return $categorycell;
 666      }
 667  
 668      public function get_item_cell($item, $params) {
 669          global $CFG;
 670  
 671          if (empty($params['element']) || empty($params['name']) || empty($params['level'])) {
 672              throw new Exception('Array key (name, level or element) missing from 2nd param of grade_edit_tree_column_name::get_item_cell($item, $params)');
 673          }
 674  
 675          $name = $params['name'];
 676          $moveaction = isset($params['moveaction']) ? $params['moveaction'] : '';
 677  
 678          $itemcell = parent::get_item_cell($item, $params);
 679          $itemcell->colspan = ($this->deepest_level + 1) - $params['level'];
 680          $itemcell->text = $moveaction . $name;
 681          return $itemcell;
 682      }
 683  }
 684  
 685  /**
 686   * Class grade_edit_tree_column_weight
 687   *
 688   * @package   core_grades
 689   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 690   */
 691  class grade_edit_tree_column_weight extends grade_edit_tree_column {
 692  
 693      public function get_header_cell() {
 694          global $OUTPUT;
 695          $headercell = clone($this->headercell);
 696          $headercell->text = get_string('weights', 'grades').$OUTPUT->help_icon('aggregationcoefweight', 'grades');
 697          return $headercell;
 698      }
 699  
 700      public function get_category_cell($category, $levelclass, $params) {
 701  
 702          $item = $category->get_grade_item();
 703          $categorycell = parent::get_category_cell($category, $levelclass, $params);
 704          $categorycell->text = grade_edit_tree::get_weight_input($item);
 705          return $categorycell;
 706      }
 707  
 708      public function get_item_cell($item, $params) {
 709          global $CFG;
 710          if (empty($params['element'])) {
 711              throw new Exception('Array key (element) missing from 2nd param of grade_edit_tree_column_weightorextracredit::get_item_cell($item, $params)');
 712          }
 713          $itemcell = parent::get_item_cell($item, $params);
 714          $itemcell->text = '&nbsp;';
 715          $object = $params['element']['object'];
 716  
 717          if (!in_array($object->itemtype, array('courseitem', 'categoryitem', 'category'))
 718                  && !in_array($object->gradetype, array(GRADE_TYPE_NONE, GRADE_TYPE_TEXT))
 719                  && (!$object->is_outcome_item() || $object->load_parent_category()->aggregateoutcomes)
 720                  && ($object->gradetype != GRADE_TYPE_SCALE || !empty($CFG->grade_includescalesinaggregation))) {
 721              $itemcell->text = grade_edit_tree::get_weight_input($item);
 722          }
 723  
 724          return $itemcell;
 725      }
 726  }
 727  
 728  /**
 729   * Class grade_edit_tree_column_range
 730   *
 731   * @package   core_grades
 732   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 733   */
 734  class grade_edit_tree_column_range extends grade_edit_tree_column {
 735  
 736      public function get_header_cell() {
 737          $headercell = clone($this->headercell);
 738          $headercell->text = get_string('maxgrade', 'grades');
 739          return $headercell;
 740      }
 741  
 742      public function get_category_cell($category, $levelclass, $params) {
 743          $categorycell = parent::get_category_cell($category, $levelclass, $params);
 744          $categorycell->text = ' - ';
 745          return $categorycell;
 746      }
 747  
 748      public function get_item_cell($item, $params) {
 749          global $DB, $OUTPUT;
 750  
 751          // If the parent aggregation is Natural, we should show the number, even for scales, as that value is used...
 752          // ...in the computation. For text grades, the grademax is not used, so we can still show the no value string.
 753          $parent_cat = $item->get_parent_category();
 754          if ($item->gradetype == GRADE_TYPE_TEXT) {
 755              $grademax = ' - ';
 756          } else if ($item->gradetype == GRADE_TYPE_SCALE) {
 757              $scale = $DB->get_record('scale', array('id' => $item->scaleid));
 758              $scale_items = null;
 759              if (empty($scale)) { //if the item is using a scale that's been removed
 760                  $scale_items = array();
 761              } else {
 762                  $scale_items = explode(',', $scale->scale);
 763              }
 764              if ($parent_cat->aggregation == GRADE_AGGREGATE_SUM) {
 765                  $grademax = end($scale_items) . ' (' .
 766                          format_float($item->grademax, $item->get_decimals()) . ')';
 767              } else {
 768                  $grademax = end($scale_items) . ' (' . count($scale_items) . ')';
 769              }
 770          } else {
 771              $grademax = format_float($item->grademax, $item->get_decimals());
 772          }
 773  
 774          $itemcell = parent::get_item_cell($item, $params);
 775          $itemcell->text = $grademax;
 776          return $itemcell;
 777      }
 778  }
 779  
 780  /**
 781   * Class grade_edit_tree_column_actions
 782   *
 783   * @package   core_grades
 784   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 785   */
 786  class grade_edit_tree_column_actions extends grade_edit_tree_column {
 787  
 788      public function __construct($params) {
 789          parent::__construct();
 790      }
 791  
 792      public function get_header_cell() {
 793          $headercell = clone($this->headercell);
 794          $headercell->text = get_string('actions');
 795          return $headercell;
 796      }
 797  
 798      public function get_category_cell($category, $levelclass, $params) {
 799  
 800          if (empty($params['actions'])) {
 801              throw new Exception('Array key (actions) missing from 3rd param of grade_edit_tree_column_actions::get_category_actions($category, $levelclass, $params)');
 802          }
 803  
 804          $categorycell = parent::get_category_cell($category, $levelclass, $params);
 805          $categorycell->text = $params['actions'];
 806          return $categorycell;
 807      }
 808  
 809      public function get_item_cell($item, $params) {
 810          if (empty($params['actions'])) {
 811              throw new Exception('Array key (actions) missing from 2nd param of grade_edit_tree_column_actions::get_item_cell($item, $params)');
 812          }
 813          $itemcell = parent::get_item_cell($item, $params);
 814          $itemcell->text = $params['actions'];
 815          return $itemcell;
 816      }
 817  }
 818  
 819  /**
 820   * Class grade_edit_tree_column_select
 821   *
 822   * @package   core_grades
 823   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 824   */
 825  class grade_edit_tree_column_select extends grade_edit_tree_column {
 826  
 827      public function get_header_cell() {
 828          $headercell = clone($this->headercell);
 829          $headercell->text = get_string('select');
 830          return $headercell;
 831      }
 832  
 833      public function get_category_cell($category, $levelclass, $params) {
 834          global $OUTPUT;
 835          if (empty($params['eid'])) {
 836              throw new Exception('Array key (eid) missing from 3rd param of grade_edit_tree_column_select::get_category_cell($category, $levelclass, $params)');
 837          }
 838          $selectall  = new action_link(new moodle_url('#'), get_string('all'), new component_action('click', 'togglecheckboxes', array('eid' => $params['eid'], 'check' => true)));
 839          $selectnone = new action_link(new moodle_url('#'), get_string('none'), new component_action('click', 'togglecheckboxes', array('eid' => $params['eid'], 'check' => false)));
 840  
 841          $categorycell = parent::get_category_cell($category, $levelclass, $params);
 842          $categorycell->text = $OUTPUT->render($selectall) . ' / ' . $OUTPUT->render($selectnone);
 843          return $categorycell;
 844      }
 845  
 846      public function get_item_cell($item, $params) {
 847          if (empty($params['itemtype']) || empty($params['eid'])) {
 848              error('Array key (itemtype or eid) missing from 2nd param of grade_edit_tree_column_select::get_item_cell($item, $params)');
 849          }
 850          $itemcell = parent::get_item_cell($item, $params);
 851  
 852          if ($params['itemtype'] != 'course' && $params['itemtype'] != 'category') {
 853              $itemcell->text = '<label class="accesshide" for="select_'.$params['eid'].'">'.
 854                  get_string('select', 'grades', $item->itemname).'</label>
 855                  <input class="itemselect ignoredirty" type="checkbox" name="select_'.$params['eid'].'" id="select_'.$params['eid'].
 856                  '" onchange="toggleCategorySelector();"/>'; // TODO: convert to YUI handler
 857          }
 858          return $itemcell;
 859      }
 860  }
 861  


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