Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Workshop module renderering methods are defined here
  20   *
  21   * @package    mod_workshop
  22   * @copyright  2009 David Mudrak <david.mudrak@gmail.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Workshop module renderer class
  30   *
  31   * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class mod_workshop_renderer extends plugin_renderer_base {
  35  
  36      ////////////////////////////////////////////////////////////////////////////
  37      // External API - methods to render workshop renderable components
  38      ////////////////////////////////////////////////////////////////////////////
  39  
  40      /**
  41       * Renders the tertiary nav for the allocation pages
  42       *
  43       * @param \mod_workshop\output\actionbar $actionbar
  44       * @return bool|string the rendered output
  45       */
  46      public function render_allocation_menu(\mod_workshop\output\actionbar $actionbar): string {
  47          return $this->render_from_template('mod_workshop/action_bar', $actionbar->export_for_template($this));
  48      }
  49  
  50      /**
  51       * Renders workshop message
  52       *
  53       * @param workshop_message $message to display
  54       * @return string html code
  55       */
  56      protected function render_workshop_message(workshop_message $message) {
  57  
  58          $text   = $message->get_message();
  59          $url    = $message->get_action_url();
  60          $label  = $message->get_action_label();
  61  
  62          if (empty($text) and empty($label)) {
  63              return '';
  64          }
  65  
  66          switch ($message->get_type()) {
  67          case workshop_message::TYPE_OK:
  68              $sty = 'ok';
  69              break;
  70          case workshop_message::TYPE_ERROR:
  71              $sty = 'error';
  72              break;
  73          default:
  74              $sty = 'info';
  75          }
  76  
  77          $o = html_writer::tag('span', $message->get_message());
  78  
  79          if (!is_null($url) and !is_null($label)) {
  80              $o .= $this->output->single_button($url, $label, 'get');
  81          }
  82  
  83          return $this->output->container($o, array('message', $sty));
  84      }
  85  
  86  
  87      /**
  88       * Renders full workshop submission
  89       *
  90       * @param workshop_submission $submission
  91       * @return string HTML
  92       */
  93      protected function render_workshop_submission(workshop_submission $submission) {
  94          global $CFG;
  95  
  96          $o  = '';    // output HTML code
  97          $anonymous = $submission->is_anonymous();
  98          $classes = 'submission-full';
  99          if ($anonymous) {
 100              $classes .= ' anonymous';
 101          }
 102          $o .= $this->output->container_start($classes);
 103          $o .= $this->output->container_start('header');
 104  
 105          $title = format_string($submission->title);
 106  
 107          if ($this->page->url != $submission->url) {
 108              $title = html_writer::link($submission->url, $title);
 109          }
 110  
 111          $o .= $this->output->heading($title, 3, 'title');
 112  
 113          if (!$anonymous) {
 114              $author = new stdclass();
 115              $additionalfields = explode(',', implode(',', \core_user\fields::get_picture_fields()));
 116              $author = username_load_fields_from_object($author, $submission, 'author', $additionalfields);
 117              $userpic            = $this->output->user_picture($author, array('courseid' => $this->page->course->id, 'size' => 64));
 118              $userurl            = new moodle_url('/user/view.php',
 119                                              array('id' => $author->id, 'course' => $this->page->course->id));
 120              $a                  = new stdclass();
 121              $a->name            = fullname($author);
 122              $a->url             = $userurl->out();
 123              $byfullname         = get_string('byfullname', 'workshop', $a);
 124              $oo  = $this->output->container($userpic, 'picture');
 125              $oo .= $this->output->container($byfullname, 'fullname');
 126  
 127              $o .= $this->output->container($oo, 'author');
 128          }
 129  
 130          $created = get_string('userdatecreated', 'workshop', userdate($submission->timecreated));
 131          $o .= $this->output->container($created, 'userdate created');
 132  
 133          if ($submission->timemodified > $submission->timecreated) {
 134              $modified = get_string('userdatemodified', 'workshop', userdate($submission->timemodified));
 135              $o .= $this->output->container($modified, 'userdate modified');
 136          }
 137  
 138          $o .= $this->output->container_end(); // end of header
 139  
 140          $content = file_rewrite_pluginfile_urls($submission->content, 'pluginfile.php', $this->page->context->id,
 141                                                          'mod_workshop', 'submission_content', $submission->id);
 142          $content = format_text($content, $submission->contentformat, array('overflowdiv'=>true));
 143          if (!empty($content)) {
 144              if (!empty($CFG->enableplagiarism)) {
 145                  require_once($CFG->libdir.'/plagiarismlib.php');
 146                  $content .= plagiarism_get_links(array('userid' => $submission->authorid,
 147                      'content' => $submission->content,
 148                      'cmid' => $this->page->cm->id,
 149                      'course' => $this->page->course));
 150              }
 151          }
 152          $o .= $this->output->container($content, 'content');
 153  
 154          $o .= $this->helper_submission_attachments($submission->id, 'html');
 155  
 156          $o .= $this->output->container_end(); // end of submission-full
 157  
 158          return $o;
 159      }
 160  
 161      /**
 162       * Renders short summary of the submission
 163       *
 164       * @param workshop_submission_summary $summary
 165       * @return string text to be echo'ed
 166       */
 167      protected function render_workshop_submission_summary(workshop_submission_summary $summary) {
 168  
 169          $o  = '';    // output HTML code
 170          $anonymous = $summary->is_anonymous();
 171          $classes = 'submission-summary';
 172  
 173          if ($anonymous) {
 174              $classes .= ' anonymous';
 175          }
 176  
 177          $gradestatus = '';
 178  
 179          if ($summary->status == 'notgraded') {
 180              $classes    .= ' notgraded';
 181              $gradestatus = $this->output->container(get_string('nogradeyet', 'workshop'), 'grade-status');
 182  
 183          } else if ($summary->status == 'graded') {
 184              $classes    .= ' graded';
 185              $gradestatus = $this->output->container(get_string('alreadygraded', 'workshop'), 'grade-status');
 186          }
 187  
 188          $o .= $this->output->container_start($classes);  // main wrapper
 189          $o .= html_writer::link($summary->url, format_string($summary->title), array('class' => 'title'));
 190  
 191          if (!$anonymous) {
 192              $author             = new stdClass();
 193              $additionalfields = explode(',', implode(',', \core_user\fields::get_picture_fields()));
 194              $author = username_load_fields_from_object($author, $summary, 'author', $additionalfields);
 195              $userpic            = $this->output->user_picture($author, array('courseid' => $this->page->course->id, 'size' => 35));
 196              $userurl            = new moodle_url('/user/view.php',
 197                                              array('id' => $author->id, 'course' => $this->page->course->id));
 198              $a                  = new stdClass();
 199              $a->name            = fullname($author);
 200              $a->url             = $userurl->out();
 201              $byfullname         = get_string('byfullname', 'workshop', $a);
 202  
 203              $oo  = $this->output->container($userpic, 'picture');
 204              $oo .= $this->output->container($byfullname, 'fullname');
 205              $o  .= $this->output->container($oo, 'author');
 206          }
 207  
 208          $created = get_string('userdatecreated', 'workshop', userdate($summary->timecreated));
 209          $o .= $this->output->container($created, 'userdate created');
 210  
 211          if ($summary->timemodified > $summary->timecreated) {
 212              $modified = get_string('userdatemodified', 'workshop', userdate($summary->timemodified));
 213              $o .= $this->output->container($modified, 'userdate modified');
 214          }
 215  
 216          $o .= $gradestatus;
 217          $o .= $this->output->container_end(); // end of the main wrapper
 218          return $o;
 219      }
 220  
 221      /**
 222       * Renders full workshop example submission
 223       *
 224       * @param workshop_example_submission $example
 225       * @return string HTML
 226       */
 227      protected function render_workshop_example_submission(workshop_example_submission $example) {
 228  
 229          $o  = '';    // output HTML code
 230          $classes = 'submission-full example';
 231          $o .= $this->output->container_start($classes);
 232          $o .= $this->output->container_start('header');
 233          $o .= $this->output->container(format_string($example->title), array('class' => 'title'));
 234          $o .= $this->output->container_end(); // end of header
 235  
 236          $content = file_rewrite_pluginfile_urls($example->content, 'pluginfile.php', $this->page->context->id,
 237                                                          'mod_workshop', 'submission_content', $example->id);
 238          $content = format_text($content, $example->contentformat, array('overflowdiv'=>true));
 239          $o .= $this->output->container($content, 'content');
 240  
 241          $o .= $this->helper_submission_attachments($example->id, 'html');
 242  
 243          $o .= $this->output->container_end(); // end of submission-full
 244  
 245          return $o;
 246      }
 247  
 248      /**
 249       * Renders short summary of the example submission
 250       *
 251       * @param workshop_example_submission_summary $summary
 252       * @return string text to be echo'ed
 253       */
 254      protected function render_workshop_example_submission_summary(workshop_example_submission_summary $summary) {
 255  
 256          $o  = '';    // output HTML code
 257  
 258          // wrapping box
 259          $o .= $this->output->box_start('generalbox example-summary ' . $summary->status);
 260  
 261          // title
 262          $o .= $this->output->container_start('example-title');
 263          $o .= html_writer::link($summary->url, format_string($summary->title), array('class' => 'title'));
 264  
 265          if ($summary->editable) {
 266              $o .= $this->output->action_icon($summary->editurl, new pix_icon('i/edit', get_string('edit')));
 267          }
 268          $o .= $this->output->container_end();
 269  
 270          // additional info
 271          if ($summary->status == 'notgraded') {
 272              $o .= $this->output->container(get_string('nogradeyet', 'workshop'), 'example-info nograde');
 273          } else {
 274              $o .= $this->output->container(get_string('gradeinfo', 'workshop' , $summary->gradeinfo), 'example-info grade');
 275          }
 276  
 277          // button to assess
 278          $button = new single_button($summary->assessurl, $summary->assesslabel, 'get');
 279          $o .= $this->output->container($this->output->render($button), 'example-actions');
 280  
 281          // end of wrapping box
 282          $o .= $this->output->box_end();
 283  
 284          return $o;
 285      }
 286  
 287      /**
 288       * Renders the user plannner tool
 289       *
 290       * @param workshop_user_plan $plan prepared for the user
 291       * @return string html code to be displayed
 292       */
 293      protected function render_workshop_user_plan(workshop_user_plan $plan) {
 294          $o  = '';    // Output HTML code.
 295          $numberofphases = count($plan->phases);
 296          $o .= html_writer::start_tag('div', array(
 297              'class' => 'userplan',
 298              'aria-labelledby' => 'mod_workshop-userplanheading',
 299              'aria-describedby' => 'mod_workshop-userplanaccessibilitytitle',
 300          ));
 301          $o .= html_writer::span(get_string('userplanaccessibilitytitle', 'workshop', $numberofphases),
 302              'accesshide', array('id' => 'mod_workshop-userplanaccessibilitytitle'));
 303          $o .= html_writer::link('#mod_workshop-userplancurrenttasks', get_string('userplanaccessibilityskip', 'workshop'),
 304              array('class' => 'accesshide'));
 305          foreach ($plan->phases as $phasecode => $phase) {
 306              $o .= html_writer::start_tag('dl', array('class' => 'phase'));
 307              $actions = '';
 308  
 309              if ($phase->active) {
 310                  // Mark the section as the current one.
 311                  $icon = $this->output->pix_icon('i/marked', '', 'moodle', ['role' => 'presentation']);
 312                  $actions .= get_string('userplancurrentphase', 'workshop').' '.$icon;
 313  
 314              } else {
 315                  // Display a control widget to switch to the given phase or mark the phase as the current one.
 316                  foreach ($phase->actions as $action) {
 317                      if ($action->type === 'switchphase') {
 318                          if ($phasecode == workshop::PHASE_ASSESSMENT && $plan->workshop->phase == workshop::PHASE_SUBMISSION
 319                                  && $plan->workshop->phaseswitchassessment) {
 320                              $icon = new pix_icon('i/scheduled', get_string('switchphaseauto', 'mod_workshop'));
 321                          } else {
 322                              $icon = new pix_icon('i/marker', get_string('switchphase'.$phasecode, 'mod_workshop'));
 323                          }
 324                          $actions .= $this->output->action_icon($action->url, $icon, null, null, true);
 325                      }
 326                  }
 327              }
 328  
 329              if (!empty($actions)) {
 330                  $actions = $this->output->container($actions, 'actions');
 331              }
 332              $classes = 'phase' . $phasecode;
 333              if ($phase->active) {
 334                  $title = html_writer::span($phase->title, 'phasetitle', ['id' => 'mod_workshop-userplancurrenttasks']);
 335                  $classes .= ' active';
 336              } else {
 337                  $title = html_writer::span($phase->title, 'phasetitle');
 338                  $classes .= ' nonactive';
 339              }
 340              $o .= html_writer::start_tag('dt', array('class' => $classes));
 341              $o .= $this->output->container($title . $actions);
 342              $o .= html_writer::start_tag('dd', array('class' => $classes. ' phasetasks'));
 343              $o .= $this->helper_user_plan_tasks($phase->tasks);
 344              $o .= html_writer::end_tag('dd');
 345              $o .= html_writer::end_tag('dl');
 346          }
 347          $o .= html_writer::end_tag('div');
 348          return $o;
 349      }
 350  
 351      /**
 352       * Renders the result of the submissions allocation process
 353       *
 354       * @param workshop_allocation_result $result as returned by the allocator's init() method
 355       * @return string HTML to be echoed
 356       */
 357      protected function render_workshop_allocation_result(workshop_allocation_result $result) {
 358          global $CFG;
 359  
 360          $status = $result->get_status();
 361  
 362          if (is_null($status) or $status == workshop_allocation_result::STATUS_VOID) {
 363              debugging('Attempt to render workshop_allocation_result with empty status', DEBUG_DEVELOPER);
 364              return '';
 365          }
 366  
 367          switch ($status) {
 368          case workshop_allocation_result::STATUS_FAILED:
 369              if ($message = $result->get_message()) {
 370                  $message = new workshop_message($message, workshop_message::TYPE_ERROR);
 371              } else {
 372                  $message = new workshop_message(get_string('allocationerror', 'workshop'), workshop_message::TYPE_ERROR);
 373              }
 374              break;
 375  
 376          case workshop_allocation_result::STATUS_CONFIGURED:
 377              if ($message = $result->get_message()) {
 378                  $message = new workshop_message($message, workshop_message::TYPE_INFO);
 379              } else {
 380                  $message = new workshop_message(get_string('allocationconfigured', 'workshop'), workshop_message::TYPE_INFO);
 381              }
 382              break;
 383  
 384          case workshop_allocation_result::STATUS_EXECUTED:
 385              if ($message = $result->get_message()) {
 386                  $message = new workshop_message($message, workshop_message::TYPE_OK);
 387              } else {
 388                  $message = new workshop_message(get_string('allocationdone', 'workshop'), workshop_message::TYPE_OK);
 389              }
 390              break;
 391  
 392          default:
 393              throw new coding_exception('Unknown allocation result status', $status);
 394          }
 395  
 396          // start with the message
 397          $o = $this->render($message);
 398  
 399          // display the details about the process if available
 400          $logs = $result->get_logs();
 401          if (is_array($logs) and !empty($logs)) {
 402              $o .= html_writer::start_tag('ul', array('class' => 'allocation-init-results'));
 403              foreach ($logs as $log) {
 404                  if ($log->type == 'debug' and !$CFG->debugdeveloper) {
 405                      // display allocation debugging messages for developers only
 406                      continue;
 407                  }
 408                  $class = $log->type;
 409                  if ($log->indent) {
 410                      $class .= ' indent';
 411                  }
 412                  $o .= html_writer::tag('li', $log->message, array('class' => $class)).PHP_EOL;
 413              }
 414              $o .= html_writer::end_tag('ul');
 415          }
 416  
 417          return $o;
 418      }
 419  
 420      /**
 421       * Renders the workshop grading report
 422       *
 423       * @param workshop_grading_report $gradingreport
 424       * @return string html code
 425       */
 426      protected function render_workshop_grading_report(workshop_grading_report $gradingreport) {
 427  
 428          $data       = $gradingreport->get_data();
 429          $options    = $gradingreport->get_options();
 430          $grades     = $data->grades;
 431          $userinfo   = $data->userinfo;
 432  
 433          if (empty($grades)) {
 434              return '';
 435          }
 436  
 437          $table = new html_table();
 438          $table->attributes['class'] = 'grading-report table-striped table-hover';
 439  
 440          $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'), 'firstname', $options->sortby, $options->sorthow);
 441          $sortbylastname = $this->helper_sortable_heading(get_string('lastname'), 'lastname', $options->sortby, $options->sorthow);
 442          if (self::fullname_format() == 'lf') {
 443              $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
 444          } else {
 445              $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
 446          }
 447  
 448          $sortbysubmisstiontitle = $this->helper_sortable_heading(get_string('submission', 'workshop'), 'submissiontitle',
 449                  $options->sortby, $options->sorthow);
 450          $sortbysubmisstionlastmodified = $this->helper_sortable_heading(get_string('submissionlastmodified', 'workshop'),
 451                  'submissionmodified', $options->sortby, $options->sorthow);
 452          $sortbysubmisstion = $sortbysubmisstiontitle . ' / ' . $sortbysubmisstionlastmodified;
 453  
 454          $table->head = array();
 455          $table->head[] = $sortbyname;
 456          $table->head[] = $sortbysubmisstion;
 457  
 458          // If we are in submission phase ignore the following headers (columns).
 459          if ($options->workshopphase != workshop::PHASE_SUBMISSION) {
 460              $table->head[] = $this->helper_sortable_heading(get_string('receivedgrades', 'workshop'));
 461              if ($options->showsubmissiongrade) {
 462                  $table->head[] = $this->helper_sortable_heading(get_string('submissiongradeof', 'workshop', $data->maxgrade),
 463                          'submissiongrade', $options->sortby, $options->sorthow);
 464              }
 465              $table->head[] = $this->helper_sortable_heading(get_string('givengrades', 'workshop'));
 466              if ($options->showgradinggrade) {
 467                  $table->head[] = $this->helper_sortable_heading(get_string('gradinggradeof', 'workshop', $data->maxgradinggrade),
 468                          'gradinggrade', $options->sortby, $options->sorthow);
 469              }
 470          }
 471          $table->rowclasses  = array();
 472          $table->colclasses  = array();
 473          $table->data        = array();
 474  
 475          foreach ($grades as $participant) {
 476              $numofreceived  = count($participant->reviewedby);
 477              $numofgiven     = count($participant->reviewerof);
 478              $published      = $participant->submissionpublished;
 479  
 480              // compute the number of <tr> table rows needed to display this participant
 481              if ($numofreceived > 0 and $numofgiven > 0) {
 482                  $numoftrs       = workshop::lcm($numofreceived, $numofgiven);
 483                  $spanreceived   = $numoftrs / $numofreceived;
 484                  $spangiven      = $numoftrs / $numofgiven;
 485              } elseif ($numofreceived == 0 and $numofgiven > 0) {
 486                  $numoftrs       = $numofgiven;
 487                  $spanreceived   = $numoftrs;
 488                  $spangiven      = $numoftrs / $numofgiven;
 489              } elseif ($numofreceived > 0 and $numofgiven == 0) {
 490                  $numoftrs       = $numofreceived;
 491                  $spanreceived   = $numoftrs / $numofreceived;
 492                  $spangiven      = $numoftrs;
 493              } else {
 494                  $numoftrs       = 1;
 495                  $spanreceived   = 1;
 496                  $spangiven      = 1;
 497              }
 498  
 499              for ($tr = 0; $tr < $numoftrs; $tr++) {
 500                  $row = new html_table_row();
 501                  if ($published) {
 502                      $row->attributes['class'] = 'published';
 503                  }
 504                  // column #1 - participant - spans over all rows
 505                  if ($tr == 0) {
 506                      $cell = new html_table_cell();
 507                      $cell->text = $this->helper_grading_report_participant($participant, $userinfo);
 508                      $cell->rowspan = $numoftrs;
 509                      $cell->attributes['class'] = 'participant';
 510                      $row->cells[] = $cell;
 511                  }
 512                  // column #2 - submission - spans over all rows
 513                  if ($tr == 0) {
 514                      $cell = new html_table_cell();
 515                      $cell->text = $this->helper_grading_report_submission($participant);
 516                      $cell->rowspan = $numoftrs;
 517                      $cell->attributes['class'] = 'submission';
 518                      $row->cells[] = $cell;
 519                  }
 520  
 521                  // If we are in submission phase ignore the following columns.
 522                  if ($options->workshopphase == workshop::PHASE_SUBMISSION) {
 523                      $table->data[] = $row;
 524                      continue;
 525                  }
 526  
 527                  // column #3 - received grades
 528                  if ($tr % $spanreceived == 0) {
 529                      $idx = intval($tr / $spanreceived);
 530                      $assessment = self::array_nth($participant->reviewedby, $idx);
 531                      $cell = new html_table_cell();
 532                      $cell->text = $this->helper_grading_report_assessment($assessment, $options->showreviewernames, $userinfo,
 533                              get_string('gradereceivedfrom', 'workshop'));
 534                      $cell->rowspan = $spanreceived;
 535                      $cell->attributes['class'] = 'receivedgrade';
 536                      if (is_null($assessment) or is_null($assessment->grade)) {
 537                          $cell->attributes['class'] .= ' null';
 538                      } else {
 539                          $cell->attributes['class'] .= ' notnull';
 540                      }
 541                      $row->cells[] = $cell;
 542                  }
 543                  // column #4 - total grade for submission
 544                  if ($options->showsubmissiongrade and $tr == 0) {
 545                      $cell = new html_table_cell();
 546                      $cell->text = $this->helper_grading_report_grade($participant->submissiongrade, $participant->submissiongradeover);
 547                      $cell->rowspan = $numoftrs;
 548                      $cell->attributes['class'] = 'submissiongrade';
 549                      $row->cells[] = $cell;
 550                  }
 551                  // column #5 - given grades
 552                  if ($tr % $spangiven == 0) {
 553                      $idx = intval($tr / $spangiven);
 554                      $assessment = self::array_nth($participant->reviewerof, $idx);
 555                      $cell = new html_table_cell();
 556                      $cell->text = $this->helper_grading_report_assessment($assessment, $options->showauthornames, $userinfo,
 557                              get_string('gradegivento', 'workshop'));
 558                      $cell->rowspan = $spangiven;
 559                      $cell->attributes['class'] = 'givengrade';
 560                      if (is_null($assessment) or is_null($assessment->grade)) {
 561                          $cell->attributes['class'] .= ' null';
 562                      } else {
 563                          $cell->attributes['class'] .= ' notnull';
 564                      }
 565                      $row->cells[] = $cell;
 566                  }
 567                  // column #6 - total grade for assessment
 568                  if ($options->showgradinggrade and $tr == 0) {
 569                      $cell = new html_table_cell();
 570                      $cell->text = $this->helper_grading_report_grade($participant->gradinggrade);
 571                      $cell->rowspan = $numoftrs;
 572                      $cell->attributes['class'] = 'gradinggrade';
 573                      $row->cells[] = $cell;
 574                  }
 575  
 576                  $table->data[] = $row;
 577              }
 578          }
 579  
 580          return html_writer::table($table);
 581      }
 582  
 583      /**
 584       * Renders the feedback for the author of the submission
 585       *
 586       * @param workshop_feedback_author $feedback
 587       * @return string HTML
 588       */
 589      protected function render_workshop_feedback_author(workshop_feedback_author $feedback) {
 590          return $this->helper_render_feedback($feedback);
 591      }
 592  
 593      /**
 594       * Renders the feedback for the reviewer of the submission
 595       *
 596       * @param workshop_feedback_reviewer $feedback
 597       * @return string HTML
 598       */
 599      protected function render_workshop_feedback_reviewer(workshop_feedback_reviewer $feedback) {
 600          return $this->helper_render_feedback($feedback);
 601      }
 602  
 603      /**
 604       * Helper method to rendering feedback
 605       *
 606       * @param workshop_feedback_author|workshop_feedback_reviewer $feedback
 607       * @return string HTML
 608       */
 609      private function helper_render_feedback($feedback) {
 610  
 611          $o  = '';    // output HTML code
 612          $o .= $this->output->container_start('feedback feedbackforauthor');
 613          $o .= $this->output->container_start('header');
 614          $o .= $this->output->heading(get_string('feedbackby', 'workshop', s(fullname($feedback->get_provider()))), 3, 'title');
 615  
 616          $userpic = $this->output->user_picture($feedback->get_provider(), array('courseid' => $this->page->course->id, 'size' => 32));
 617          $o .= $this->output->container($userpic, 'picture');
 618          $o .= $this->output->container_end(); // end of header
 619  
 620          $content = format_text($feedback->get_content(), $feedback->get_format(), array('overflowdiv' => true));
 621          $o .= $this->output->container($content, 'content');
 622  
 623          $o .= $this->output->container_end();
 624  
 625          return $o;
 626      }
 627  
 628      /**
 629       * Renders the full assessment
 630       *
 631       * @param workshop_assessment $assessment
 632       * @return string HTML
 633       */
 634      protected function render_workshop_assessment(workshop_assessment $assessment) {
 635  
 636          $o = ''; // output HTML code
 637          $anonymous = is_null($assessment->reviewer);
 638          $classes = 'assessment-full';
 639          if ($anonymous) {
 640              $classes .= ' anonymous';
 641          }
 642  
 643          $o .= $this->output->container_start($classes);
 644          $o .= $this->output->container_start('header');
 645  
 646          if (!empty($assessment->title)) {
 647              $title = s($assessment->title);
 648          } else {
 649              $title = get_string('assessment', 'workshop');
 650          }
 651          if (($assessment->url instanceof moodle_url) and ($this->page->url != $assessment->url)) {
 652              $o .= $this->output->container(html_writer::link($assessment->url, $title), 'title');
 653          } else {
 654              $o .= $this->output->container($title, 'title');
 655          }
 656  
 657          if (!$anonymous) {
 658              $reviewer   = $assessment->reviewer;
 659              $userpic    = $this->output->user_picture($reviewer, array('courseid' => $this->page->course->id, 'size' => 32));
 660  
 661              $userurl    = new moodle_url('/user/view.php',
 662                                         array('id' => $reviewer->id, 'course' => $this->page->course->id));
 663              $a          = new stdClass();
 664              $a->name    = fullname($reviewer);
 665              $a->url     = $userurl->out();
 666              $byfullname = get_string('assessmentby', 'workshop', $a);
 667              $oo         = $this->output->container($userpic, 'picture');
 668              $oo        .= $this->output->container($byfullname, 'fullname');
 669  
 670              $o .= $this->output->container($oo, 'reviewer');
 671          }
 672  
 673          if (is_null($assessment->realgrade)) {
 674              $o .= $this->output->container(
 675                  get_string('notassessed', 'workshop'),
 676                  'grade nograde'
 677              );
 678          } else {
 679              $a              = new stdClass();
 680              $a->max         = $assessment->maxgrade;
 681              $a->received    = $assessment->realgrade;
 682              $o .= $this->output->container(
 683                  get_string('gradeinfo', 'workshop', $a),
 684                  'grade'
 685              );
 686  
 687              if (!is_null($assessment->weight) and $assessment->weight != 1) {
 688                  $o .= $this->output->container(
 689                      get_string('weightinfo', 'workshop', $assessment->weight),
 690                      'weight'
 691                  );
 692              }
 693          }
 694  
 695          $o .= $this->output->container_start('actions');
 696          foreach ($assessment->actions as $action) {
 697              $o .= $this->output->single_button($action->url, $action->label, $action->method);
 698          }
 699          $o .= $this->output->container_end(); // actions
 700  
 701          $o .= $this->output->container_end(); // header
 702  
 703          if (!is_null($assessment->form)) {
 704              $o .= print_collapsible_region_start('assessment-form-wrapper', uniqid('workshop-assessment'),
 705                      get_string('assessmentform', 'workshop'), 'workshop-viewlet-assessmentform-collapsed', false, true);
 706              $o .= $this->output->container(self::moodleform($assessment->form), 'assessment-form');
 707              $o .= print_collapsible_region_end(true);
 708  
 709              if (!$assessment->form->is_editable()) {
 710                  $o .= $this->overall_feedback($assessment);
 711              }
 712          }
 713  
 714          $o .= $this->output->container_end(); // main wrapper
 715  
 716          return $o;
 717      }
 718  
 719      /**
 720       * Renders the assessment of an example submission
 721       *
 722       * @param workshop_example_assessment $assessment
 723       * @return string HTML
 724       */
 725      protected function render_workshop_example_assessment(workshop_example_assessment $assessment) {
 726          return $this->render_workshop_assessment($assessment);
 727      }
 728  
 729      /**
 730       * Renders the reference assessment of an example submission
 731       *
 732       * @param workshop_example_reference_assessment $assessment
 733       * @return string HTML
 734       */
 735      protected function render_workshop_example_reference_assessment(workshop_example_reference_assessment $assessment) {
 736          return $this->render_workshop_assessment($assessment);
 737      }
 738  
 739      /**
 740       * Renders the overall feedback for the author of the submission
 741       *
 742       * @param workshop_assessment $assessment
 743       * @return string HTML
 744       */
 745      protected function overall_feedback(workshop_assessment $assessment) {
 746  
 747          $content = $assessment->get_overall_feedback_content();
 748  
 749          if ($content === false) {
 750              return '';
 751          }
 752  
 753          $o = '';
 754  
 755          if (!is_null($content)) {
 756              $o .= $this->output->container($content, 'content');
 757          }
 758  
 759          $attachments = $assessment->get_overall_feedback_attachments();
 760  
 761          if (!empty($attachments)) {
 762              $o .= $this->output->container_start('attachments');
 763              $images = '';
 764              $files = '';
 765              foreach ($attachments as $attachment) {
 766                  $icon = $this->output->pix_icon(file_file_icon($attachment), get_mimetype_description($attachment),
 767                      'moodle', array('class' => 'icon'));
 768                  $link = html_writer::link($attachment->fileurl, $icon.' '.substr($attachment->filepath.$attachment->filename, 1));
 769                  if (file_mimetype_in_typegroup($attachment->mimetype, 'web_image')) {
 770                      $preview = html_writer::empty_tag('img', array('src' => $attachment->previewurl, 'alt' => '', 'class' => 'preview'));
 771                      $preview = html_writer::tag('a', $preview, array('href' => $attachment->fileurl));
 772                      $images .= $this->output->container($preview);
 773                  } else {
 774                      $files .= html_writer::tag('li', $link, array('class' => $attachment->mimetype));
 775                  }
 776              }
 777              if ($images) {
 778                  $images = $this->output->container($images, 'images');
 779              }
 780  
 781              if ($files) {
 782                  $files = html_writer::tag('ul', $files, array('class' => 'files'));
 783              }
 784  
 785              $o .= $images.$files;
 786              $o .= $this->output->container_end();
 787          }
 788  
 789          if ($o === '') {
 790              return '';
 791          }
 792  
 793          $o = $this->output->box($o, 'overallfeedback');
 794          $o = print_collapsible_region($o, 'overall-feedback-wrapper', uniqid('workshop-overall-feedback'),
 795                  get_string('overallfeedback', 'workshop'), 'workshop-viewlet-overallfeedback-collapsed', false, true);
 796  
 797          return $o;
 798      }
 799  
 800      /**
 801       * Renders a perpage selector for workshop listings
 802       *
 803       * The scripts using this have to define the $PAGE->url prior to calling this
 804       * and deal with eventually submitted value themselves.
 805       *
 806       * @param int $current current value of the perpage parameter
 807       * @return string HTML
 808       */
 809      public function perpage_selector($current=10) {
 810  
 811          $options = array();
 812          foreach (array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 1000) as $option) {
 813              if ($option != $current) {
 814                  $options[$option] = $option;
 815              }
 816          }
 817          $select = new single_select($this->page->url, 'perpage', $options, '', array('' => get_string('showingperpagechange', 'mod_workshop')));
 818          $select->label = get_string('showingperpage', 'mod_workshop', $current);
 819          $select->method = 'post';
 820  
 821          return $this->output->container($this->output->render($select), 'perpagewidget');
 822      }
 823  
 824      /**
 825       * Renders the user's final grades
 826       *
 827       * @param workshop_final_grades $grades with the info about grades in the gradebook
 828       * @return string HTML
 829       */
 830      protected function render_workshop_final_grades(workshop_final_grades $grades) {
 831  
 832          $out = html_writer::start_tag('div', array('class' => 'finalgrades'));
 833  
 834          if (!empty($grades->submissiongrade)) {
 835              $cssclass = 'grade submissiongrade';
 836              if ($grades->submissiongrade->hidden) {
 837                  $cssclass .= ' hiddengrade';
 838              }
 839              $out .= html_writer::tag(
 840                  'div',
 841                  html_writer::tag('div', get_string('submissiongrade', 'mod_workshop'), array('class' => 'gradetype')) .
 842                  html_writer::tag('div', $grades->submissiongrade->str_long_grade, array('class' => 'gradevalue')),
 843                  array('class' => $cssclass)
 844              );
 845          }
 846  
 847          if (!empty($grades->assessmentgrade)) {
 848              $cssclass = 'grade assessmentgrade';
 849              if ($grades->assessmentgrade->hidden) {
 850                  $cssclass .= ' hiddengrade';
 851              }
 852              $out .= html_writer::tag(
 853                  'div',
 854                  html_writer::tag('div', get_string('gradinggrade', 'mod_workshop'), array('class' => 'gradetype')) .
 855                  html_writer::tag('div', $grades->assessmentgrade->str_long_grade, array('class' => 'gradevalue')),
 856                  array('class' => $cssclass)
 857              );
 858          }
 859  
 860          $out .= html_writer::end_tag('div');
 861  
 862          return $out;
 863      }
 864  
 865      ////////////////////////////////////////////////////////////////////////////
 866      // Internal rendering helper methods
 867      ////////////////////////////////////////////////////////////////////////////
 868  
 869      /**
 870       * Renders a list of files attached to the submission
 871       *
 872       * If format==html, then format a html string. If format==text, then format a text-only string.
 873       * Otherwise, returns html for non-images and html to display the image inline.
 874       *
 875       * @param int $submissionid submission identifier
 876       * @param string format the format of the returned string - html|text
 877       * @return string formatted text to be echoed
 878       */
 879      protected function helper_submission_attachments($submissionid, $format = 'html') {
 880          global $CFG;
 881          require_once($CFG->libdir.'/filelib.php');
 882  
 883          $fs     = get_file_storage();
 884          $ctx    = $this->page->context;
 885          $files  = $fs->get_area_files($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid);
 886  
 887          $outputimgs     = '';   // images to be displayed inline
 888          $outputfiles    = '';   // list of attachment files
 889  
 890          foreach ($files as $file) {
 891              if ($file->is_directory()) {
 892                  continue;
 893              }
 894  
 895              $filepath   = $file->get_filepath();
 896              $filename   = $file->get_filename();
 897              $fileurl    = moodle_url::make_pluginfile_url($ctx->id, 'mod_workshop', 'submission_attachment',
 898                              $submissionid, $filepath, $filename, true);
 899              $embedurl   = moodle_url::make_pluginfile_url($ctx->id, 'mod_workshop', 'submission_attachment',
 900                              $submissionid, $filepath, $filename, false);
 901              $embedurl   = new moodle_url($embedurl, array('preview' => 'bigthumb'));
 902              $type       = $file->get_mimetype();
 903              $image      = $this->output->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon'));
 904  
 905              $linkhtml   = html_writer::link($fileurl, $image . substr($filepath, 1) . $filename);
 906              $linktxt    = "$filename [$fileurl]";
 907  
 908              if ($format == 'html') {
 909                  if (file_mimetype_in_typegroup($type, 'web_image')) {
 910                      $preview     = html_writer::empty_tag('img', array('src' => $embedurl, 'alt' => '', 'class' => 'preview'));
 911                      $preview     = html_writer::tag('a', $preview, array('href' => $fileurl));
 912                      $outputimgs .= $this->output->container($preview);
 913  
 914                  } else {
 915                      $outputfiles .= html_writer::tag('li', $linkhtml, array('class' => $type));
 916                  }
 917  
 918              } else if ($format == 'text') {
 919                  $outputfiles .= $linktxt . PHP_EOL;
 920              }
 921  
 922              if (!empty($CFG->enableplagiarism)) {
 923                  require_once($CFG->libdir.'/plagiarismlib.php');
 924                  $outputfiles .= plagiarism_get_links(array('userid' => $file->get_userid(),
 925                      'file' => $file,
 926                      'cmid' => $this->page->cm->id,
 927                      'course' => $this->page->course->id));
 928              }
 929          }
 930  
 931          if ($format == 'html') {
 932              if ($outputimgs) {
 933                  $outputimgs = $this->output->container($outputimgs, 'images');
 934              }
 935  
 936              if ($outputfiles) {
 937                  $outputfiles = html_writer::tag('ul', $outputfiles, array('class' => 'files'));
 938              }
 939  
 940              return $this->output->container($outputimgs . $outputfiles, 'attachments');
 941  
 942          } else {
 943              return $outputfiles;
 944          }
 945      }
 946  
 947      /**
 948       * Renders the tasks for the single phase in the user plan
 949       *
 950       * @param stdClass $tasks
 951       * @return string html code
 952       */
 953      protected function helper_user_plan_tasks(array $tasks) {
 954          $out = '';
 955          foreach ($tasks as $taskcode => $task) {
 956              $classes = '';
 957              $accessibilitytext = '';
 958              $icon = null;
 959              if ($task->completed === true) {
 960                  $classes .= ' completed';
 961                  $accessibilitytext .= get_string('taskdone', 'workshop') . ' ';
 962              } else if ($task->completed === false) {
 963                  $classes .= ' fail';
 964                  $accessibilitytext .= get_string('taskfail', 'workshop') . ' ';
 965              } else if ($task->completed === 'info') {
 966                  $classes .= ' info';
 967                  $accessibilitytext .= get_string('taskinfo', 'workshop') . ' ';
 968              } else {
 969                  $accessibilitytext .= get_string('tasktodo', 'workshop') . ' ';
 970              }
 971              if (is_null($task->link)) {
 972                  $title = html_writer::tag('span', $accessibilitytext, array('class' => 'accesshide'));
 973                  $title .= $task->title;
 974              } else {
 975                  $title = html_writer::tag('span', $accessibilitytext, array('class' => 'accesshide'));
 976                  $title .= html_writer::link($task->link, $task->title);
 977              }
 978              $title = $this->output->container($title, 'title');
 979              $details = $this->output->container($task->details, 'details');
 980              $out .= html_writer::tag('li', $title . $details, array('class' => $classes));
 981          }
 982          if ($out) {
 983              $out = html_writer::tag('ul', $out, array('class' => 'tasks'));
 984          }
 985          return $out;
 986      }
 987  
 988      /**
 989       * Renders a text with icons to sort by the given column
 990       *
 991       * This is intended for table headings.
 992       *
 993       * @param string $text    The heading text
 994       * @param string $sortid  The column id used for sorting
 995       * @param string $sortby  Currently sorted by (column id)
 996       * @param string $sorthow Currently sorted how (ASC|DESC)
 997       *
 998       * @return string
 999       */
1000      protected function helper_sortable_heading($text, $sortid=null, $sortby=null, $sorthow=null) {
1001  
1002          $out = html_writer::tag('span', $text, array('class'=>'text'));
1003  
1004          if (!is_null($sortid)) {
1005              if ($sortby !== $sortid or $sorthow !== 'ASC') {
1006                  $url = new moodle_url($this->page->url);
1007                  $url->params(array('sortby' => $sortid, 'sorthow' => 'ASC'));
1008                  $out .= $this->output->action_icon($url, new pix_icon('t/sort_asc', get_string('sortasc', 'workshop')),
1009                      null, array('class' => 'iconsort sort asc'));
1010              }
1011              if ($sortby !== $sortid or $sorthow !== 'DESC') {
1012                  $url = new moodle_url($this->page->url);
1013                  $url->params(array('sortby' => $sortid, 'sorthow' => 'DESC'));
1014                  $out .= $this->output->action_icon($url, new pix_icon('t/sort_desc', get_string('sortdesc', 'workshop')),
1015                      null, array('class' => 'iconsort sort desc'));
1016              }
1017          }
1018          return $out;
1019  }
1020  
1021      /**
1022       * @param stdClass $participant
1023       * @param array $userinfo
1024       * @return string
1025       */
1026      protected function helper_grading_report_participant(stdclass $participant, array $userinfo) {
1027          $userid = $participant->userid;
1028          $out  = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 35));
1029          $out .= html_writer::tag('span', fullname($userinfo[$userid]));
1030  
1031          return $out;
1032      }
1033  
1034      /**
1035       * @param stdClass $participant
1036       * @return string
1037       */
1038      protected function helper_grading_report_submission(stdclass $participant) {
1039          global $CFG;
1040  
1041          if (is_null($participant->submissionid)) {
1042              $out = $this->output->container(get_string('nosubmissionfound', 'workshop'), 'info');
1043          } else {
1044              $url = new moodle_url('/mod/workshop/submission.php',
1045                                    array('cmid' => $this->page->context->instanceid, 'id' => $participant->submissionid));
1046              $out = html_writer::link($url, format_string($participant->submissiontitle), array('class'=>'title'));
1047  
1048              $lastmodified = get_string('userdatemodified', 'workshop', userdate($participant->submissionmodified));
1049              $out .= html_writer::tag('div', $lastmodified, array('class' => 'lastmodified'));
1050          }
1051  
1052          return $out;
1053      }
1054  
1055      /**
1056       * @todo Highlight the nulls
1057       * @param stdClass|null $assessment
1058       * @param bool $shownames
1059       * @param string $separator between the grade and the reviewer/author
1060       * @return string
1061       */
1062      protected function helper_grading_report_assessment($assessment, $shownames, array $userinfo, $separator) {
1063          global $CFG;
1064  
1065          if (is_null($assessment)) {
1066              return get_string('nullgrade', 'workshop');
1067          }
1068          $a = new stdclass();
1069          $a->grade = is_null($assessment->grade) ? get_string('nullgrade', 'workshop') : $assessment->grade;
1070          $a->gradinggrade = is_null($assessment->gradinggrade) ? get_string('nullgrade', 'workshop') : $assessment->gradinggrade;
1071          $a->weight = $assessment->weight;
1072          // grrr the following logic should really be handled by a future language pack feature
1073          if (is_null($assessment->gradinggradeover)) {
1074              if ($a->weight == 1) {
1075                  $grade = get_string('formatpeergrade', 'workshop', $a);
1076              } else {
1077                  $grade = get_string('formatpeergradeweighted', 'workshop', $a);
1078              }
1079          } else {
1080              $a->gradinggradeover = $assessment->gradinggradeover;
1081              if ($a->weight == 1) {
1082                  $grade = get_string('formatpeergradeover', 'workshop', $a);
1083              } else {
1084                  $grade = get_string('formatpeergradeoverweighted', 'workshop', $a);
1085              }
1086          }
1087          $url = new moodle_url('/mod/workshop/assessment.php',
1088                                array('asid' => $assessment->assessmentid));
1089          $grade = html_writer::link($url, $grade, array('class'=>'grade'));
1090  
1091          if ($shownames) {
1092              $userid = $assessment->userid;
1093              $name   = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 16));
1094              $name  .= html_writer::tag('span', fullname($userinfo[$userid]), array('class' => 'fullname'));
1095              $name   = $separator . html_writer::tag('span', $name, array('class' => 'user'));
1096          } else {
1097              $name   = '';
1098          }
1099  
1100          return $this->output->container($grade . $name, 'assessmentdetails');
1101      }
1102  
1103      /**
1104       * Formats the aggreagated grades
1105       */
1106      protected function helper_grading_report_grade($grade, $over=null) {
1107          $a = new stdclass();
1108          $a->grade = is_null($grade) ? get_string('nullgrade', 'workshop') : $grade;
1109          if (is_null($over)) {
1110              $text = get_string('formataggregatedgrade', 'workshop', $a);
1111          } else {
1112              $a->over = is_null($over) ? get_string('nullgrade', 'workshop') : $over;
1113              $text = get_string('formataggregatedgradeover', 'workshop', $a);
1114          }
1115          return $text;
1116      }
1117  
1118      ////////////////////////////////////////////////////////////////////////////
1119      // Static helpers
1120      ////////////////////////////////////////////////////////////////////////////
1121  
1122      /**
1123       * Helper method dealing with the fact we can not just fetch the output of moodleforms
1124       *
1125       * @param moodleform $mform
1126       * @return string HTML
1127       */
1128      protected static function moodleform(moodleform $mform) {
1129  
1130          ob_start();
1131          $mform->display();
1132          $o = ob_get_contents();
1133          ob_end_clean();
1134  
1135          return $o;
1136      }
1137  
1138      /**
1139       * Helper function returning the n-th item of the array
1140       *
1141       * @param array $a
1142       * @param int   $n from 0 to m, where m is th number of items in the array
1143       * @return mixed the $n-th element of $a
1144       */
1145      protected static function array_nth(array $a, $n) {
1146          $keys = array_keys($a);
1147          if ($n < 0 or $n > count($keys) - 1) {
1148              return null;
1149          }
1150          $key = $keys[$n];
1151          return $a[$key];
1152      }
1153  
1154      /**
1155       * Tries to guess the fullname format set at the site
1156       *
1157       * @return string fl|lf
1158       */
1159      protected static function fullname_format() {
1160          $fake = new stdclass(); // fake user
1161          $fake->lastname = 'LLLL';
1162          $fake->firstname = 'FFFF';
1163          $fullname = get_string('fullnamedisplay', '', $fake);
1164          if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
1165              return 'lf';
1166          } else {
1167              return 'fl';
1168          }
1169      }
1170  }