Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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   * Contains renderer used for displaying rubric
  19   *
  20   * @package    gradingform_rubric
  21   * @copyright  2011 Marina Glancy
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Grading method plugin renderer
  29   *
  30   * @package    gradingform_rubric
  31   * @copyright  2011 Marina Glancy
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class gradingform_rubric_renderer extends plugin_renderer_base {
  35  
  36      /**
  37       * This function returns html code for displaying criterion. Depending on $mode it may be the
  38       * code to edit rubric, to preview the rubric, to evaluate somebody or to review the evaluation.
  39       *
  40       * This function may be called from display_rubric() to display the whole rubric, or it can be
  41       * called by itself to return a template used by JavaScript to add new empty criteria to the
  42       * rubric being designed.
  43       * In this case it will use macros like {NAME}, {LEVELS}, {CRITERION-id}, etc.
  44       *
  45       * When overriding this function it is very important to remember that all elements of html
  46       * form (in edit or evaluate mode) must have the name $elementname.
  47       *
  48       * Also JavaScript relies on the class names of elements and when developer changes them
  49       * script might stop working.
  50       *
  51       * @param int $mode rubric display mode, see {@link gradingform_rubric_controller}
  52       * @param array $options display options for this rubric, defaults are: {@link gradingform_rubric_controller::get_default_options()}
  53       * @param string $elementname the name of the form element (in editor mode) or the prefix for div ids (in view mode)
  54       * @param array|null $criterion criterion data
  55       * @param string $levelsstr evaluated templates for this criterion levels
  56       * @param array|null $value (only in view mode) teacher's feedback on this criterion
  57       * @return string
  58       */
  59      public function criterion_template($mode, $options, $elementname = '{NAME}', $criterion = null, $levelsstr = '{LEVELS}', $value = null) {
  60          // TODO MDL-31235 description format, remark format
  61          if ($criterion === null || !is_array($criterion) || !array_key_exists('id', $criterion)) {
  62              $criterion = array('id' => '{CRITERION-id}', 'description' => '{CRITERION-description}', 'sortorder' => '{CRITERION-sortorder}', 'class' => '{CRITERION-class}');
  63          } else {
  64              foreach (array('sortorder', 'description', 'class') as $key) {
  65                  // set missing array elements to empty strings to avoid warnings
  66                  if (!array_key_exists($key, $criterion)) {
  67                      $criterion[$key] = '';
  68                  }
  69              }
  70          }
  71          $criteriontemplate = html_writer::start_tag('tr', array('class' => 'criterion'. $criterion['class'], 'id' => '{NAME}-criteria-{CRITERION-id}'));
  72          if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) {
  73              $criteriontemplate .= html_writer::start_tag('td', array('class' => 'controls'));
  74              foreach (array('moveup', 'delete', 'movedown', 'duplicate') as $key) {
  75                  $value = get_string('criterion'.$key, 'gradingform_rubric');
  76                  $button = html_writer::empty_tag('input', array('type' => 'submit', 'name' => '{NAME}[criteria][{CRITERION-id}]['.$key.']',
  77                      'id' => '{NAME}-criteria-{CRITERION-id}-'.$key, 'value' => $value));
  78                  $criteriontemplate .= html_writer::tag('div', $button, array('class' => $key));
  79              }
  80              $criteriontemplate .= html_writer::empty_tag('input', array('type' => 'hidden',
  81                                                                          'name' => '{NAME}[criteria][{CRITERION-id}][sortorder]',
  82                                                                          'value' => $criterion['sortorder']));
  83              $criteriontemplate .= html_writer::end_tag('td'); // .controls
  84  
  85              // Criterion description text area.
  86              $descriptiontextareaparams = array(
  87                  'name' => '{NAME}[criteria][{CRITERION-id}][description]',
  88                  'id' => '{NAME}-criteria-{CRITERION-id}-description',
  89                  'aria-label' => get_string('criterion', 'gradingform_rubric', ''),
  90                  'cols' => '10', 'rows' => '5'
  91              );
  92              $description = html_writer::tag('textarea', s($criterion['description']), $descriptiontextareaparams);
  93          } else {
  94              if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN) {
  95                  $criteriontemplate .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][sortorder]', 'value' => $criterion['sortorder']));
  96                  $criteriontemplate .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][description]', 'value' => $criterion['description']));
  97              }
  98              $description = s($criterion['description']);
  99          }
 100          $descriptionclass = 'description';
 101          if (isset($criterion['error_description'])) {
 102              $descriptionclass .= ' error';
 103          }
 104  
 105          // Description cell params.
 106          $descriptiontdparams = array(
 107              'class' => $descriptionclass,
 108              'id' => '{NAME}-criteria-{CRITERION-id}-description-cell'
 109          );
 110          if ($mode != gradingform_rubric_controller::DISPLAY_EDIT_FULL &&
 111              $mode != gradingform_rubric_controller::DISPLAY_EDIT_FROZEN) {
 112              // Set description's cell as tab-focusable.
 113              $descriptiontdparams['tabindex'] = '0';
 114              // Set label for the criterion cell.
 115              $descriptiontdparams['aria-label'] = get_string('criterion', 'gradingform_rubric', s($criterion['description']));
 116          }
 117  
 118          // Description cell.
 119          $criteriontemplate .= html_writer::tag('td', $description, $descriptiontdparams);
 120  
 121          // Levels table.
 122          $levelsrowparams = array('id' => '{NAME}-criteria-{CRITERION-id}-levels');
 123          // Add radiogroup role only when not previewing or editing.
 124          $isradiogroup = !in_array($mode, [
 125              gradingform_rubric_controller::DISPLAY_EDIT_FULL,
 126              gradingform_rubric_controller::DISPLAY_EDIT_FROZEN,
 127              gradingform_rubric_controller::DISPLAY_PREVIEW,
 128              gradingform_rubric_controller::DISPLAY_PREVIEW_GRADED,
 129          ]);
 130          if ($isradiogroup) {
 131              $levelsrowparams['role'] = 'radiogroup';
 132          }
 133          $levelsrow = html_writer::tag('tr', $levelsstr, $levelsrowparams);
 134  
 135          $levelstableparams = array(
 136              'id' => '{NAME}-criteria-{CRITERION-id}-levels-table',
 137              'aria-label' => get_string('levelsgroup', 'gradingform_rubric')
 138          );
 139          $levelsstrtable = html_writer::tag('table', $levelsrow, $levelstableparams);
 140          $levelsclass = 'levels';
 141          if (isset($criterion['error_levels'])) {
 142              $levelsclass .= ' error';
 143          }
 144          $criteriontemplate .= html_writer::tag('td', $levelsstrtable, array('class' => $levelsclass));
 145          if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) {
 146              $value = get_string('criterionaddlevel', 'gradingform_rubric');
 147              $button = html_writer::empty_tag('input', array('type' => 'submit', 'name' => '{NAME}[criteria][{CRITERION-id}][levels][addlevel]',
 148                  'id' => '{NAME}-criteria-{CRITERION-id}-levels-addlevel', 'value' => $value, 'class' => 'btn btn-secondary'));
 149              $criteriontemplate .= html_writer::tag('td', $button, array('class' => 'addlevel'));
 150          }
 151          $displayremark = ($options['enableremarks'] && ($mode != gradingform_rubric_controller::DISPLAY_VIEW || $options['showremarksstudent']));
 152          if ($displayremark) {
 153              $currentremark = '';
 154              if (isset($value['remark'])) {
 155                  $currentremark = $value['remark'];
 156              }
 157  
 158              // Label for criterion remark.
 159              $remarkinfo = new stdClass();
 160              $remarkinfo->description = s($criterion['description']);
 161              $remarkinfo->remark = $currentremark;
 162              $remarklabeltext = get_string('criterionremark', 'gradingform_rubric', $remarkinfo);
 163  
 164              if ($mode == gradingform_rubric_controller::DISPLAY_EVAL) {
 165                  // HTML parameters for remarks text area.
 166                  $remarkparams = array(
 167                      'name' => '{NAME}[criteria][{CRITERION-id}][remark]',
 168                      'id' => '{NAME}-criteria-{CRITERION-id}-remark',
 169                      'cols' => '10', 'rows' => '5',
 170                      'aria-label' => $remarklabeltext
 171                  );
 172                  $input = html_writer::tag('textarea', s($currentremark), $remarkparams);
 173                  $criteriontemplate .= html_writer::tag('td', $input, array('class' => 'remark'));
 174              } else if ($mode == gradingform_rubric_controller::DISPLAY_EVAL_FROZEN) {
 175                  $criteriontemplate .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][remark]', 'value' => $currentremark));
 176              }else if ($mode == gradingform_rubric_controller::DISPLAY_REVIEW || $mode == gradingform_rubric_controller::DISPLAY_VIEW) {
 177                  // HTML parameters for remarks cell.
 178                  $remarkparams = array(
 179                      'class' => 'remark',
 180                      'tabindex' => '0',
 181                      'id' => '{NAME}-criteria-{CRITERION-id}-remark',
 182                      'aria-label' => $remarklabeltext
 183                  );
 184                  $criteriontemplate .= html_writer::tag('td', s($currentremark), $remarkparams);
 185              }
 186          }
 187          $criteriontemplate .= html_writer::end_tag('tr'); // .criterion
 188  
 189          $criteriontemplate = str_replace('{NAME}', $elementname, $criteriontemplate);
 190          $criteriontemplate = str_replace('{CRITERION-id}', $criterion['id'], $criteriontemplate);
 191          return $criteriontemplate;
 192      }
 193  
 194      /**
 195       * This function returns html code for displaying one level of one criterion. Depending on $mode
 196       * it may be the code to edit rubric, to preview the rubric, to evaluate somebody or to review the evaluation.
 197       *
 198       * This function may be called from display_rubric() to display the whole rubric, or it can be
 199       * called by itself to return a template used by JavaScript to add new empty level to the
 200       * criterion during the design of rubric.
 201       * In this case it will use macros like {NAME}, {CRITERION-id}, {LEVEL-id}, etc.
 202       *
 203       * When overriding this function it is very important to remember that all elements of html
 204       * form (in edit or evaluate mode) must have the name $elementname.
 205       *
 206       * Also JavaScript relies on the class names of elements and when developer changes them
 207       * script might stop working.
 208       *
 209       * @param int $mode rubric display mode see {@link gradingform_rubric_controller}
 210       * @param array $options display options for this rubric, defaults are: {@link gradingform_rubric_controller::get_default_options()}
 211       * @param string $elementname the name of the form element (in editor mode) or the prefix for div ids (in view mode)
 212       * @param string|int $criterionid either id of the nesting criterion or a macro for template
 213       * @param array|null $level level data, also in view mode it might also have property $level['checked'] whether this level is checked
 214       * @return string
 215       */
 216      public function level_template($mode, $options, $elementname = '{NAME}', $criterionid = '{CRITERION-id}', $level = null) {
 217          // TODO MDL-31235 definition format
 218          if (!isset($level['id'])) {
 219              $level = array('id' => '{LEVEL-id}', 'definition' => '{LEVEL-definition}', 'score' => '{LEVEL-score}', 'class' => '{LEVEL-class}', 'checked' => false);
 220          } else {
 221              foreach (array('score', 'definition', 'class', 'checked', 'index') as $key) {
 222                  // set missing array elements to empty strings to avoid warnings
 223                  if (!array_key_exists($key, $level)) {
 224                      $level[$key] = '';
 225                  }
 226              }
 227          }
 228  
 229          // Get level index.
 230          $levelindex = isset($level['index']) ? $level['index'] : '{LEVEL-index}';
 231  
 232          // Template for one level within one criterion
 233          $tdattributes = array(
 234              'id' => '{NAME}-criteria-{CRITERION-id}-levels-{LEVEL-id}',
 235              'class' => 'text-break level' . $level['class']
 236          );
 237          if (isset($level['tdwidth'])) {
 238              $tdattributes['style'] = "width: " . round($level['tdwidth']).'%;';
 239          }
 240  
 241          $leveltemplate = html_writer::start_tag('div', array('class' => 'level-wrapper'));
 242          if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) {
 243              $definitionparams = array(
 244                  'id' => '{NAME}-criteria-{CRITERION-id}-levels-{LEVEL-id}-definition',
 245                  'name' => '{NAME}[criteria][{CRITERION-id}][levels][{LEVEL-id}][definition]',
 246                  'aria-label' => get_string('leveldefinition', 'gradingform_rubric', $levelindex),
 247                  'cols' => '10', 'rows' => '4'
 248              );
 249              $definition = html_writer::tag('textarea', s($level['definition']), $definitionparams);
 250  
 251              $scoreparams = array(
 252                  'type' => 'text',
 253                  'id' => '{NAME}[criteria][{CRITERION-id}][levels][{LEVEL-id}][score]',
 254                  'name' => '{NAME}[criteria][{CRITERION-id}][levels][{LEVEL-id}][score]',
 255                  'aria-label' => get_string('scoreinputforlevel', 'gradingform_rubric', $levelindex),
 256                  'size' => '3',
 257                  'value' => $level['score']
 258              );
 259              $score = html_writer::empty_tag('input', $scoreparams);
 260          } else {
 261              if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN) {
 262                  $leveltemplate .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][levels][{LEVEL-id}][definition]', 'value' => $level['definition']));
 263                  $leveltemplate .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => '{NAME}[criteria][{CRITERION-id}][levels][{LEVEL-id}][score]', 'value' => $level['score']));
 264              }
 265              $definition = s($level['definition']);
 266              $score = $level['score'];
 267          }
 268          if ($mode == gradingform_rubric_controller::DISPLAY_EVAL) {
 269              $levelradioparams = array(
 270                  'type' => 'radio',
 271                  'id' => '{NAME}-criteria-{CRITERION-id}-levels-{LEVEL-id}-definition',
 272                  'name' => '{NAME}[criteria][{CRITERION-id}][levelid]',
 273                  'value' => $level['id']
 274              );
 275              if ($level['checked']) {
 276                  $levelradioparams['checked'] = 'checked';
 277              }
 278              $input = html_writer::empty_tag('input', $levelradioparams);
 279              $leveltemplate .= html_writer::div($input, 'radio');
 280          }
 281          if ($mode == gradingform_rubric_controller::DISPLAY_EVAL_FROZEN && $level['checked']) {
 282              $leveltemplate .= html_writer::empty_tag('input',
 283                  array(
 284                      'type' => 'hidden',
 285                      'name' => '{NAME}[criteria][{CRITERION-id}][levelid]',
 286                      'value' => $level['id']
 287                  )
 288              );
 289          }
 290          $score = html_writer::tag('span', $score, array('id' => '{NAME}-criteria-{CRITERION-id}-levels-{LEVEL-id}-score', 'class' => 'scorevalue'));
 291          $definitionclass = 'definition';
 292          if (isset($level['error_definition'])) {
 293              $definitionclass .= ' error';
 294          }
 295  
 296          if ($mode != gradingform_rubric_controller::DISPLAY_EDIT_FULL &&
 297              $mode != gradingform_rubric_controller::DISPLAY_EDIT_FROZEN) {
 298  
 299              $tdattributes['tabindex'] = '0';
 300              $levelinfo = new stdClass();
 301              $levelinfo->definition = s($level['definition']);
 302              $levelinfo->score = $level['score'];
 303              $tdattributes['aria-label'] = get_string('level', 'gradingform_rubric', $levelinfo);
 304  
 305              if ($mode != gradingform_rubric_controller::DISPLAY_PREVIEW &&
 306                  $mode != gradingform_rubric_controller::DISPLAY_PREVIEW_GRADED) {
 307                  // Add role of radio button to level cell if not in edit and preview mode.
 308                  $tdattributes['role'] = 'radio';
 309                  if ($level['checked']) {
 310                      $tdattributes['aria-checked'] = 'true';
 311                  } else {
 312                      $tdattributes['aria-checked'] = 'false';
 313                  }
 314              }
 315          }
 316  
 317          $leveltemplateparams = array(
 318              'id' => '{NAME}-criteria-{CRITERION-id}-levels-{LEVEL-id}-definition-container'
 319          );
 320          $leveltemplate .= html_writer::div($definition, $definitionclass, $leveltemplateparams);
 321          $displayscore = true;
 322          if (!$options['showscoreteacher'] && in_array($mode, array(gradingform_rubric_controller::DISPLAY_EVAL, gradingform_rubric_controller::DISPLAY_EVAL_FROZEN, gradingform_rubric_controller::DISPLAY_REVIEW))) {
 323              $displayscore = false;
 324          }
 325          if (!$options['showscorestudent'] && in_array($mode, array(gradingform_rubric_controller::DISPLAY_VIEW, gradingform_rubric_controller::DISPLAY_PREVIEW_GRADED))) {
 326              $displayscore = false;
 327          }
 328          if ($displayscore) {
 329              $scoreclass = 'score d-inline';
 330              if (isset($level['error_score'])) {
 331                  $scoreclass .= ' error';
 332              }
 333              $leveltemplate .= html_writer::tag('div', get_string('scorepostfix', 'gradingform_rubric', $score), array('class' => $scoreclass));
 334          }
 335          if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) {
 336              $value = get_string('leveldelete', 'gradingform_rubric', $levelindex);
 337              $buttonparams = array(
 338                  'type' => 'submit',
 339                  'name' => '{NAME}[criteria][{CRITERION-id}][levels][{LEVEL-id}][delete]',
 340                  'id' => '{NAME}-criteria-{CRITERION-id}-levels-{LEVEL-id}-delete',
 341                  'value' => $value
 342              );
 343              $button = html_writer::empty_tag('input', $buttonparams);
 344              $leveltemplate .= html_writer::tag('div', $button, array('class' => 'delete'));
 345          }
 346          $leveltemplate .= html_writer::end_tag('div'); // .level-wrapper
 347  
 348          $leveltemplate = html_writer::tag('td', $leveltemplate, $tdattributes); // The .level cell.
 349  
 350          $leveltemplate = str_replace('{NAME}', $elementname, $leveltemplate);
 351          $leveltemplate = str_replace('{CRITERION-id}', $criterionid, $leveltemplate);
 352          $leveltemplate = str_replace('{LEVEL-id}', $level['id'], $leveltemplate);
 353          return $leveltemplate;
 354      }
 355  
 356      /**
 357       * This function returns html code for displaying rubric template (content before and after
 358       * criteria list). Depending on $mode it may be the code to edit rubric, to preview the rubric,
 359       * to evaluate somebody or to review the evaluation.
 360       *
 361       * This function is called from display_rubric() to display the whole rubric.
 362       *
 363       * When overriding this function it is very important to remember that all elements of html
 364       * form (in edit or evaluate mode) must have the name $elementname.
 365       *
 366       * Also JavaScript relies on the class names of elements and when developer changes them
 367       * script might stop working.
 368       *
 369       * @param int $mode rubric display mode see {@link gradingform_rubric_controller}
 370       * @param array $options display options for this rubric, defaults are: {@link gradingform_rubric_controller::get_default_options()}
 371       * @param string $elementname the name of the form element (in editor mode) or the prefix for div ids (in view mode)
 372       * @param string $criteriastr evaluated templates for this rubric's criteria
 373       * @return string
 374       */
 375      protected function rubric_template($mode, $options, $elementname, $criteriastr) {
 376          $classsuffix = ''; // CSS suffix for class of the main div. Depends on the mode
 377          switch ($mode) {
 378              case gradingform_rubric_controller::DISPLAY_EDIT_FULL:
 379                  $classsuffix = ' editor editable'; break;
 380              case gradingform_rubric_controller::DISPLAY_EDIT_FROZEN:
 381                  $classsuffix = ' editor frozen';  break;
 382              case gradingform_rubric_controller::DISPLAY_PREVIEW:
 383              case gradingform_rubric_controller::DISPLAY_PREVIEW_GRADED:
 384                  $classsuffix = ' editor preview';  break;
 385              case gradingform_rubric_controller::DISPLAY_EVAL:
 386                  $classsuffix = ' evaluate editable'; break;
 387              case gradingform_rubric_controller::DISPLAY_EVAL_FROZEN:
 388                  $classsuffix = ' evaluate frozen';  break;
 389              case gradingform_rubric_controller::DISPLAY_REVIEW:
 390                  $classsuffix = ' review';  break;
 391              case gradingform_rubric_controller::DISPLAY_VIEW:
 392                  $classsuffix = ' view';  break;
 393          }
 394  
 395          $rubrictemplate = html_writer::start_tag('div', array('id' => 'rubric-{NAME}', 'class' => 'clearfix gradingform_rubric'.$classsuffix));
 396  
 397          // Rubric table.
 398          $rubrictableparams = array(
 399              'class' => 'criteria',
 400              'id' => '{NAME}-criteria',
 401              'aria-label' => get_string('rubric', 'gradingform_rubric'));
 402          $rubrictable = html_writer::tag('table', $criteriastr, $rubrictableparams);
 403          $rubrictemplate .= $rubrictable;
 404          if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) {
 405              $value = get_string('addcriterion', 'gradingform_rubric');
 406              $criteriainputparams = array(
 407                  'type' => 'submit',
 408                  'name' => '{NAME}[criteria][addcriterion]',
 409                  'id' => '{NAME}-criteria-addcriterion',
 410                  'value' => $value
 411              );
 412              $input = html_writer::empty_tag('input', $criteriainputparams);
 413              $rubrictemplate .= html_writer::tag('div', $input, array('class' => 'addcriterion btn btn-secondary'));
 414          }
 415          $rubrictemplate .= $this->rubric_edit_options($mode, $options);
 416          $rubrictemplate .= html_writer::end_tag('div');
 417  
 418          return str_replace('{NAME}', $elementname, $rubrictemplate);
 419      }
 420  
 421      /**
 422       * Generates html template to view/edit the rubric options. Expression {NAME} is used in
 423       * template for the form element name
 424       *
 425       * @param int $mode rubric display mode see {@link gradingform_rubric_controller}
 426       * @param array $options display options for this rubric, defaults are: {@link gradingform_rubric_controller::get_default_options()}
 427       * @return string
 428       */
 429      protected function rubric_edit_options($mode, $options) {
 430          if ($mode != gradingform_rubric_controller::DISPLAY_EDIT_FULL
 431                  && $mode != gradingform_rubric_controller::DISPLAY_EDIT_FROZEN
 432                  && $mode != gradingform_rubric_controller::DISPLAY_PREVIEW) {
 433              // Options are displayed only for people who can manage
 434              return;
 435          }
 436          $html = html_writer::start_tag('div', array('class' => 'options'));
 437          $html .= html_writer::tag('div', get_string('rubricoptions', 'gradingform_rubric'), array('class' => 'optionsheading'));
 438          $attrs = array('type' => 'hidden', 'name' => '{NAME}[options][optionsset]', 'value' => 1);
 439          foreach ($options as $option => $value) {
 440              $html .= html_writer::start_tag('div', array('class' => 'option '.$option));
 441              $attrs = array('name' => '{NAME}[options]['.$option.']', 'id' => '{NAME}-options-'.$option);
 442              switch ($option) {
 443                  case 'sortlevelsasc':
 444                      // Display option as dropdown
 445                      $html .= html_writer::label(get_string($option, 'gradingform_rubric'), $attrs['id'], false);
 446                      $value = (int)(!!$value); // make sure $value is either 0 or 1
 447                      if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FULL) {
 448                          $selectoptions = array(0 => get_string($option.'0', 'gradingform_rubric'), 1 => get_string($option.'1', 'gradingform_rubric'));
 449                          $valuestr = html_writer::select($selectoptions, $attrs['name'], $value, false, array('id' => $attrs['id']));
 450                          $html .= html_writer::tag('span', $valuestr, array('class' => 'value'));
 451                      } else {
 452                          $html .= html_writer::tag('span', get_string($option.$value, 'gradingform_rubric'), array('class' => 'value'));
 453                          if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN) {
 454                              $html .= html_writer::empty_tag('input', $attrs + array('type' => 'hidden', 'value' => $value));
 455                          }
 456                      }
 457                      break;
 458                  default:
 459                      if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN && $value) {
 460                          // Id should be different then the actual input added later.
 461                          $attrs['id'] .= '_hidden';
 462                          $html .= html_writer::empty_tag('input', $attrs + array('type' => 'hidden', 'value' => $value));
 463                      }
 464                      // Display option as checkbox
 465                      $attrs['type'] = 'checkbox';
 466                      $attrs['value'] = 1;
 467                      if ($value) {
 468                          $attrs['checked'] = 'checked';
 469                      }
 470                      if ($mode == gradingform_rubric_controller::DISPLAY_EDIT_FROZEN || $mode == gradingform_rubric_controller::DISPLAY_PREVIEW) {
 471                          $attrs['disabled'] = 'disabled';
 472                          unset($attrs['name']);
 473                          // Id should be different then the actual input added later.
 474                          $attrs['id'] .= '_disabled';
 475                      }
 476                      $html .= html_writer::empty_tag('input', $attrs);
 477                      $html .= html_writer::tag('label', get_string($option, 'gradingform_rubric'), array('for' => $attrs['id']));
 478                      break;
 479              }
 480              if (get_string_manager()->string_exists($option.'_help', 'gradingform_rubric')) {
 481                  $html .= $this->help_icon($option, 'gradingform_rubric');
 482              }
 483              $html .= html_writer::end_tag('div'); // .option
 484          }
 485          $html .= html_writer::end_tag('div'); // .options
 486          return $html;
 487      }
 488  
 489      /**
 490       * This function returns html code for displaying rubric. Depending on $mode it may be the code
 491       * to edit rubric, to preview the rubric, to evaluate somebody or to review the evaluation.
 492       *
 493       * It is very unlikely that this function needs to be overriden by theme. It does not produce
 494       * any html code, it just prepares data about rubric design and evaluation, adds the CSS
 495       * class to elements and calls the functions level_template, criterion_template and
 496       * rubric_template
 497       *
 498       * @param array $criteria data about the rubric design
 499       * @param array $options display options for this rubric, defaults are: {@link gradingform_rubric_controller::get_default_options()}
 500       * @param int $mode rubric display mode, see {@link gradingform_rubric_controller}
 501       * @param string $elementname the name of the form element (in editor mode) or the prefix for div ids (in view mode)
 502       * @param array $values evaluation result
 503       * @return string
 504       */
 505      public function display_rubric($criteria, $options, $mode, $elementname = null, $values = null) {
 506          $criteriastr = '';
 507          $cnt = 0;
 508          foreach ($criteria as $id => $criterion) {
 509              $criterion['class'] = $this->get_css_class_suffix($cnt++, sizeof($criteria) -1);
 510              $criterion['id'] = $id;
 511              $levelsstr = '';
 512              $levelcnt = 0;
 513              if (isset($values['criteria'][$id])) {
 514                  $criterionvalue = $values['criteria'][$id];
 515              } else {
 516                  $criterionvalue = null;
 517              }
 518              $index = 1;
 519              foreach ($criterion['levels'] as $levelid => $level) {
 520                  $level['id'] = $levelid;
 521                  $level['class'] = $this->get_css_class_suffix($levelcnt++, sizeof($criterion['levels']) -1);
 522                  $level['checked'] = (isset($criterionvalue['levelid']) && ((int)$criterionvalue['levelid'] === $levelid));
 523                  if ($level['checked'] && ($mode == gradingform_rubric_controller::DISPLAY_EVAL_FROZEN || $mode == gradingform_rubric_controller::DISPLAY_REVIEW || $mode == gradingform_rubric_controller::DISPLAY_VIEW)) {
 524                      $level['class'] .= ' checked';
 525                      //in mode DISPLAY_EVAL the class 'checked' will be added by JS if it is enabled. If JS is not enabled, the 'checked' class will only confuse
 526                  }
 527                  if (isset($criterionvalue['savedlevelid']) && ((int)$criterionvalue['savedlevelid'] === $levelid)) {
 528                      $level['class'] .= ' currentchecked';
 529                  }
 530                  $level['tdwidth'] = 100/count($criterion['levels']);
 531                  $level['index'] = $index;
 532                  $levelsstr .= $this->level_template($mode, $options, $elementname, $id, $level);
 533                  $index++;
 534              }
 535              $criteriastr .= $this->criterion_template($mode, $options, $elementname, $criterion, $levelsstr, $criterionvalue);
 536          }
 537          return $this->rubric_template($mode, $options, $elementname, $criteriastr);
 538      }
 539  
 540      /**
 541       * Help function to return CSS class names for element (first/last/even/odd) with leading space
 542       *
 543       * @param int $idx index of this element in the row/column
 544       * @param int $maxidx maximum index of the element in the row/column
 545       * @return string
 546       */
 547      protected function get_css_class_suffix($idx, $maxidx) {
 548          $class = '';
 549          if ($idx == 0) {
 550              $class .= ' first';
 551          }
 552          if ($idx == $maxidx) {
 553              $class .= ' last';
 554          }
 555          if ($idx%2) {
 556              $class .= ' odd';
 557          } else {
 558              $class .= ' even';
 559          }
 560          return $class;
 561      }
 562  
 563      /**
 564       * Displays for the student the list of instances or default content if no instances found
 565       *
 566       * @param array $instances array of objects of type gradingform_rubric_instance
 567       * @param string $defaultcontent default string that would be displayed without advanced grading
 568       * @param boolean $cangrade whether current user has capability to grade in this context
 569       * @return string
 570       */
 571      public function display_instances($instances, $defaultcontent, $cangrade) {
 572          $return = '';
 573          if (sizeof($instances)) {
 574              $return .= html_writer::start_tag('div', array('class' => 'advancedgrade'));
 575              $idx = 0;
 576              foreach ($instances as $instance) {
 577                  $return .= $this->display_instance($instance, $idx++, $cangrade);
 578              }
 579              $return .= html_writer::end_tag('div');
 580          }
 581          return $return. $defaultcontent;
 582      }
 583  
 584      /**
 585       * Displays one grading instance
 586       *
 587       * @param gradingform_rubric_instance $instance
 588       * @param int $idx unique number of instance on page
 589       * @param bool $cangrade whether current user has capability to grade in this context
 590       */
 591      public function display_instance(gradingform_rubric_instance $instance, $idx, $cangrade) {
 592          $criteria = $instance->get_controller()->get_definition()->rubric_criteria;
 593          $options = $instance->get_controller()->get_options();
 594          $values = $instance->get_rubric_filling();
 595          if ($cangrade) {
 596              $mode = gradingform_rubric_controller::DISPLAY_REVIEW;
 597              $showdescription = $options['showdescriptionteacher'];
 598          } else {
 599              $mode = gradingform_rubric_controller::DISPLAY_VIEW;
 600              $showdescription = $options['showdescriptionstudent'];
 601          }
 602          $output = '';
 603          if ($showdescription) {
 604              $output .= $this->box($instance->get_controller()->get_formatted_description(), 'gradingform_rubric-description');
 605          }
 606          $output .= $this->display_rubric($criteria, $options, $mode, 'rubric'.$idx, $values);
 607          return $output;
 608      }
 609  
 610      /**
 611       * Displays confirmation that students require re-grading
 612       *
 613       * @param string $elementname
 614       * @param int $changelevel
 615       * @param string $value
 616       * @return string
 617       */
 618      public function display_regrade_confirmation($elementname, $changelevel, $value) {
 619          $html = html_writer::start_tag('div', array('class' => 'gradingform_rubric-regrade', 'role' => 'alert'));
 620          if ($changelevel<=2) {
 621              $html .= html_writer::label(get_string('regrademessage1', 'gradingform_rubric'), 'menu' . $elementname . 'regrade');
 622              $selectoptions = array(
 623                  0 => get_string('regradeoption0', 'gradingform_rubric'),
 624                  1 => get_string('regradeoption1', 'gradingform_rubric')
 625              );
 626              $html .= html_writer::select($selectoptions, $elementname.'[regrade]', $value, false);
 627          } else {
 628              $html .= get_string('regrademessage5', 'gradingform_rubric');
 629              $html .= html_writer::empty_tag('input', array('name' => $elementname.'[regrade]', 'value' => 1, 'type' => 'hidden'));
 630          }
 631          $html .= html_writer::end_tag('div');
 632          return $html;
 633      }
 634  
 635      /**
 636       * Generates and returns HTML code to display information box about how rubric score is converted to the grade
 637       *
 638       * @param array $scores
 639       * @return string
 640       */
 641      public function display_rubric_mapping_explained($scores) {
 642          $html = '';
 643          if (!$scores) {
 644              return $html;
 645          }
 646          if ($scores['minscore'] <> 0) {
 647              $html .= $this->output->notification(get_string('zerolevelsabsent', 'gradingform_rubric'), 'error');
 648          }
 649          $html .= $this->output->notification(get_string('rubricmappingexplained', 'gradingform_rubric', (object)$scores), 'info');
 650          return $html;
 651      }
 652  }