[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/blocks/activity_results/ -> block_activity_results.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Classes to enforce the various access rules that can apply to a activity.
  19   *
  20   * @package    block_activity_results
  21   * @copyright  2009 Tim Hunt
  22   * @copyright  2015 Stephen Bourget
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->dirroot . '/lib/grade/constants.php');
  29  
  30  define('B_ACTIVITYRESULTS_NAME_FORMAT_FULL', 1);
  31  define('B_ACTIVITYRESULTS_NAME_FORMAT_ID',   2);
  32  define('B_ACTIVITYRESULTS_NAME_FORMAT_ANON', 3);
  33  define('B_ACTIVITYRESULTS_GRADE_FORMAT_PCT', 1);
  34  define('B_ACTIVITYRESULTS_GRADE_FORMAT_FRA', 2);
  35  define('B_ACTIVITYRESULTS_GRADE_FORMAT_ABS', 3);
  36  define('B_ACTIVITYRESULTS_GRADE_FORMAT_SCALE', 4);
  37  
  38  /**
  39   * Block activity_results class definition.
  40   *
  41   * This block can be added to a course page or a activity page to display of list of
  42   * the best/worst students/groups in a particular activity.
  43   *
  44   * @package    block_activity_results
  45   * @copyright  2009 Tim Hunt
  46   * @copyright  2015 Stephen Bourget
  47   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  48   */
  49  class block_activity_results extends block_base {
  50  
  51      /**
  52       * Core function used to initialize the block.
  53       */
  54      public function init() {
  55          $this->title = get_string('pluginname', 'block_activity_results');
  56      }
  57  
  58      /**
  59       * Allow the block to have a configuration page
  60       *
  61       * @return boolean
  62       */
  63      public function has_config() {
  64          return true;
  65      }
  66  
  67      /**
  68       * Core function, specifies where the block can be used.
  69       * @return array
  70       */
  71      public function applicable_formats() {
  72          return array('course-view' => true, 'mod' => true);
  73      }
  74  
  75      /**
  76       * If this block belongs to a activity context, then return that activity's id.
  77       * Otherwise, return 0.
  78       * @return stdclass the activity record.
  79       */
  80      public function get_owning_activity() {
  81          global $DB;
  82  
  83          // Set some defaults.
  84          $result = new stdClass();
  85          $result->id = 0;
  86  
  87          if (empty($this->instance->parentcontextid)) {
  88              return $result;
  89          }
  90          $parentcontext = context::instance_by_id($this->instance->parentcontextid);
  91          if ($parentcontext->contextlevel != CONTEXT_MODULE) {
  92              return $result;
  93          }
  94          $cm = get_coursemodule_from_id($this->page->cm->modname, $parentcontext->instanceid);
  95          if (!$cm) {
  96              return $result;
  97          }
  98          // Get the grade_items id.
  99          $rec = $DB->get_record('grade_items', array('iteminstance' => $cm->instance, 'itemmodule' => $this->page->cm->modname));
 100          if (!$rec) {
 101              return $result;
 102          }
 103          // See if it is a gradable activity.
 104          if (($rec->gradetype != GRADE_TYPE_VALUE) && ($rec->gradetype != GRADE_TYPE_SCALE)) {
 105              return $result;
 106          }
 107          return $rec;
 108      }
 109  
 110      /**
 111       * Used to save the form config data
 112       * @param stdclass $data
 113       * @param bool $nolongerused
 114       */
 115      public function instance_config_save($data, $nolongerused = false) {
 116          global $DB;
 117          if (empty($data->activitygradeitemid)) {
 118              // Figure out info about parent module.
 119              $info = $this->get_owning_activity();
 120              $data->activitygradeitemid = $info->id;
 121              if ($info->id < 1) {
 122                  // No activity was selected.
 123                  $info->itemmodule = '';
 124                  $info->iteminstance = '';
 125              } else {
 126                  $data->activityparent = $info->itemmodule;
 127                  $data->activityparentid = $info->iteminstance;
 128              }
 129          } else {
 130              // Lookup info about the parent module (we have the id from mdl_grade_items.
 131              $info = $DB->get_record('grade_items', array('id' => $data->activitygradeitemid));
 132              $data->activityparent = $info->itemmodule;
 133              $data->activityparentid = $info->iteminstance;
 134          }
 135          parent::instance_config_save($data);
 136      }
 137  
 138      /**
 139       * Used to generate the content for the block.
 140       * @return string
 141       */
 142      public function get_content() {
 143          global $USER, $CFG, $DB;
 144  
 145          if ($this->content !== null) {
 146              return $this->content;
 147          }
 148  
 149          $this->content = new stdClass;
 150          $this->content->text = '';
 151          $this->content->footer = '';
 152  
 153          if (empty($this->instance)) {
 154              return $this->content;
 155          }
 156  
 157          // We are configured so use the configuration.
 158          if (!empty($this->config->activitygradeitemid)) {
 159              // We are configured.
 160              $activitygradeitemid = $this->config->activitygradeitemid;
 161  
 162              // Lookup the module in the grade_items table.
 163              $activity = $DB->get_record('grade_items', array('id' => $activitygradeitemid));
 164              if (empty($activity)) {
 165                  // Activity does not exist.
 166                  $this->content->text = get_string('error_emptyactivityrecord', 'block_activity_results');
 167                  return $this->content;
 168              }
 169              $courseid = $activity->courseid;
 170              $inactivity = false;
 171          } else {
 172              // Not configured.
 173              $activitygradeitemid = 0;
 174          }
 175  
 176          // Check to see if we are in the moule we are displaying results for.
 177          if (!empty($this->config->activitygradeitemid)) {
 178              if ($this->get_owning_activity()->id == $this->config->activitygradeitemid) {
 179                  $inactivity = true;
 180              } else {
 181                  $inactivity = false;
 182              }
 183          }
 184  
 185          // Activity ID is missing.
 186          if (empty($activitygradeitemid)) {
 187              $this->content->text = get_string('error_emptyactivityid', 'block_activity_results');
 188              return $this->content;
 189          }
 190  
 191          // Check to see if we are configured.
 192          if (empty($this->config->showbest) && empty($this->config->showworst)) {
 193              $this->content->text = get_string('configuredtoshownothing', 'block_activity_results');
 194              return $this->content;
 195          }
 196  
 197          // Check to see if it is a supported grade type.
 198          if (empty($activity->gradetype) || ($activity->gradetype != GRADE_TYPE_VALUE && $activity->gradetype != GRADE_TYPE_SCALE)) {
 199              $this->content->text = get_string('error_unsupportedgradetype', 'block_activity_results');
 200              return $this->content;
 201          }
 202  
 203          // Get the grades for this activity.
 204          $sql = 'SELECT * FROM {grade_grades}
 205                   WHERE itemid = ? AND finalgrade is not NULL
 206                   ORDER BY finalgrade, timemodified DESC';
 207  
 208          $grades = $DB->get_records_sql($sql, array( $activitygradeitemid));
 209  
 210          if (empty($grades) || $activity->hidden) {
 211              // No grades available, The block will hide itself in this case.
 212              return $this->content;
 213          }
 214  
 215          // Set up results.
 216          $groupmode = NOGROUPS;
 217          $best      = array();
 218          $worst     = array();
 219  
 220          if (!empty($this->config->nameformat)) {
 221              $nameformat = $this->config->nameformat;
 222          } else {
 223              $nameformat = B_ACTIVITYRESULTS_NAME_FORMAT_FULL;
 224          }
 225  
 226          // Get $cm and context.
 227          if ($inactivity) {
 228              $cm = $this->page->cm;
 229              $context = $this->page->context;
 230          } else {
 231              $cm = get_coursemodule_from_instance($activity->itemmodule, $activity->iteminstance, $courseid);
 232              $context = context_module::instance($cm->id);
 233          }
 234  
 235          if (!empty($this->config->usegroups)) {
 236              $groupmode = groups_get_activity_groupmode($cm);
 237  
 238              if ($groupmode == SEPARATEGROUPS && has_capability('moodle/site:accessallgroups', $context)) {
 239                  // If you have the ability to see all groups then lets show them.
 240                  $groupmode = VISIBLEGROUPS;
 241              }
 242          }
 243  
 244          switch ($groupmode) {
 245              case VISIBLEGROUPS:
 246                  // Display group-mode results.
 247                  $groups = groups_get_all_groups($courseid);
 248  
 249                  if (empty($groups)) {
 250                      // No groups exist, sorry.
 251                      $this->content->text = get_string('error_nogroupsexist', 'block_activity_results');
 252                      return $this->content;
 253                  }
 254  
 255                  // Find out all the userids which have a submitted grade.
 256                  $userids = array();
 257                  $gradeforuser = array();
 258                  foreach ($grades as $grade) {
 259                      $userids[] = $grade->userid;
 260                      $gradeforuser[$grade->userid] = (float)$grade->finalgrade;
 261                  }
 262  
 263                  // Now find which groups these users belong in.
 264                  list($usertest, $params) = $DB->get_in_or_equal($userids);
 265                  $params[] = $courseid;
 266                  $usergroups = $DB->get_records_sql('
 267                          SELECT gm.id, gm.userid, gm.groupid, g.name
 268                          FROM {groups} g
 269                          LEFT JOIN {groups_members} gm ON g.id = gm.groupid
 270                          WHERE gm.userid ' . $usertest . ' AND g.courseid = ?', $params);
 271  
 272                  // Now, iterate the grades again and sum them up for each group.
 273                  $groupgrades = array();
 274                  foreach ($usergroups as $usergroup) {
 275                      if (!isset($groupgrades[$usergroup->groupid])) {
 276                          $groupgrades[$usergroup->groupid] = array(
 277                                  'sum' => (float)$gradeforuser[$usergroup->userid],
 278                                  'number' => 1,
 279                                  'group' => $usergroup->name);
 280                      } else {
 281                          $groupgrades[$usergroup->groupid]['sum'] += $gradeforuser[$usergroup->userid];
 282                          $groupgrades[$usergroup->groupid]['number'] += 1;
 283                      }
 284                  }
 285  
 286                  foreach ($groupgrades as $groupid => $groupgrade) {
 287                      $groupgrades[$groupid]['average'] = $groupgrades[$groupid]['sum'] / $groupgrades[$groupid]['number'];
 288                  }
 289  
 290                  // Sort groupgrades according to average grade, ascending.
 291                  uasort($groupgrades, create_function('$a, $b',
 292                          'if($a["average"] == $b["average"]) return 0; return ($a["average"] > $b["average"] ? 1 : -1);'));
 293  
 294                  // How many groups do we have with graded member submissions to show?
 295                  $numbest  = empty($this->config->showbest) ? 0 : min($this->config->showbest, count($groupgrades));
 296                  $numworst = empty($this->config->showworst) ? 0 : min($this->config->showworst, count($groupgrades) - $numbest);
 297  
 298                  // Collect all the group results we are going to use in $best and $worst.
 299                  $remaining = $numbest;
 300                  $groupgrade = end($groupgrades);
 301                  while ($remaining--) {
 302                      $best[key($groupgrades)] = $groupgrade['average'];
 303                      $groupgrade = prev($groupgrades);
 304                  }
 305  
 306                  $remaining = $numworst;
 307                  $groupgrade = reset($groupgrades);
 308                  while ($remaining--) {
 309                      $worst[key($groupgrades)] = $groupgrade['average'];
 310                      $groupgrade = next($groupgrades);
 311                  }
 312  
 313                  // Ready for output!
 314                  if ($activity->gradetype == GRADE_TYPE_SCALE) {
 315                      // We must display the results using scales.
 316                      $gradeformat = B_ACTIVITYRESULTS_GRADE_FORMAT_SCALE;
 317                      // Preload the scale.
 318                      $scale = $this->get_scale($activity->scaleid);
 319                  } else if (intval(empty($this->config->gradeformat))) {
 320                      $gradeformat = B_ACTIVITYRESULTS_GRADE_FORMAT_PCT;
 321                  } else {
 322                      $gradeformat = $this->config->gradeformat;
 323                  }
 324  
 325                  // Generate the header.
 326                  $this->content->text .= $this->activity_link($activity, $cm);
 327  
 328                  if ($nameformat == B_ACTIVITYRESULTS_NAME_FORMAT_FULL) {
 329                      if (has_capability('moodle/course:managegroups', $context)) {
 330                          $grouplink = $CFG->wwwroot.'/group/overview.php?id='.$courseid.'&amp;group=';
 331                      } else if (has_capability('moodle/course:viewparticipants', $context)) {
 332                          $grouplink = $CFG->wwwroot.'/user/index.php?id='.$courseid.'&amp;group=';
 333                      } else {
 334                          $grouplink = '';
 335                      }
 336                  }
 337  
 338                  $rank = 0;
 339                  if (!empty($best)) {
 340                      $this->content->text .= '<table class="grades"><caption>';
 341                      if ($numbest == 1) {
 342                          $this->content->text .= get_string('bestgroupgrade', 'block_activity_results');
 343                      } else {
 344                          $this->content->text .= get_string('bestgroupgrades', 'block_activity_results', $numbest);
 345                      }
 346                      $this->content->text .= '</caption><colgroup class="number" />';
 347                      $this->content->text .= '<colgroup class="name" /><colgroup class="grade" /><tbody>';
 348                      foreach ($best as $groupid => $averagegrade) {
 349                          switch ($nameformat) {
 350                              case B_ACTIVITYRESULTS_NAME_FORMAT_ANON:
 351                              case B_ACTIVITYRESULTS_NAME_FORMAT_ID:
 352                                  $thisname = get_string('group');
 353                              break;
 354                              default:
 355                              case B_ACTIVITYRESULTS_NAME_FORMAT_FULL:
 356                                  if ($grouplink) {
 357                                      $thisname = '<a href="'.$grouplink.$groupid.'">'.$groupgrades[$groupid]['group'].'</a>';
 358                                  } else {
 359                                      $thisname = $groupgrades[$groupid]['group'];
 360                                  }
 361                              break;
 362                          }
 363                          $this->content->text .= '<tr><td>'.(++$rank).'.</td><td>'.$thisname.'</td><td>';
 364                          switch ($gradeformat) {
 365                              case B_ACTIVITYRESULTS_GRADE_FORMAT_SCALE:
 366                                  // Round answer up and locate appropriate scale.
 367                                  $answer = (round($averagegrade, 0, PHP_ROUND_HALF_UP) - 1);
 368                                  if (isset($scale[$answer])) {
 369                                      $this->content->text .= $scale[$answer];
 370                                  } else {
 371                                      // Value is not in the scale.
 372                                      $this->content->text .= get_string('unknown', 'block_activity_results');
 373                                  }
 374                              break;
 375                              case B_ACTIVITYRESULTS_GRADE_FORMAT_FRA:
 376                                  $this->content->text .= $this->activity_format_grade($averagegrade)
 377                                      . '/' . $this->activity_format_grade($activity->grademax);
 378                              break;
 379                              case B_ACTIVITYRESULTS_GRADE_FORMAT_ABS:
 380                                  $this->content->text .= $this->activity_format_grade($averagegrade);
 381                              break;
 382                              default:
 383                              case B_ACTIVITYRESULTS_GRADE_FORMAT_PCT:
 384                                  $this->content->text .= $this->activity_format_grade((float)$averagegrade /
 385                                          (float)$activity->grademax * 100).'%';
 386                              break;
 387                          }
 388                          $this->content->text .= '</td></tr>';
 389                      }
 390                      $this->content->text .= '</tbody></table>';
 391                  }
 392  
 393                  $rank = 0;
 394                  if (!empty($worst)) {
 395                      $worst = array_reverse($worst, true);
 396                      $this->content->text .= '<table class="grades"><caption>';
 397                      if ($numworst == 1) {
 398                          $this->content->text .= get_string('worstgroupgrade', 'block_activity_results');
 399                      } else {
 400                          $this->content->text .= get_string('worstgroupgrades', 'block_activity_results', $numworst);
 401                      }
 402                      $this->content->text .= '</caption><colgroup class="number" />';
 403                      $this->content->text .= '<colgroup class="name" /><colgroup class="grade" /><tbody>';
 404                      foreach ($worst as $groupid => $averagegrade) {
 405                          switch ($nameformat) {
 406                              case B_ACTIVITYRESULTS_NAME_FORMAT_ANON:
 407                              case B_ACTIVITYRESULTS_NAME_FORMAT_ID:
 408                                  $thisname = get_string('group');
 409                              break;
 410                              default:
 411                              case B_ACTIVITYRESULTS_NAME_FORMAT_FULL:
 412                                  if ($grouplink) {
 413                                      $thisname = '<a href="'.$grouplink.$groupid.'">'.$groupgrades[$groupid]['group'].'</a>';
 414                                  } else {
 415                                      $thisname = $groupgrades[$groupid]['group'];
 416                                  }
 417                              break;
 418                          }
 419                          $this->content->text .= '<tr><td>'.(++$rank).'.</td><td>'.$thisname.'</td><td>';
 420                          switch ($gradeformat) {
 421                              case B_ACTIVITYRESULTS_GRADE_FORMAT_SCALE:
 422                                  // Round answer up and locate appropriate scale.
 423                                  $answer = (round($averagegrade, 0, PHP_ROUND_HALF_UP) - 1);
 424                                  if (isset($scale[$answer])) {
 425                                      $this->content->text .= $scale[$answer];
 426                                  } else {
 427                                      // Value is not in the scale.
 428                                      $this->content->text .= get_string('unknown', 'block_activity_results');
 429                                  }
 430                              break;
 431                              case B_ACTIVITYRESULTS_GRADE_FORMAT_FRA:
 432                                  $this->content->text .= $this->activity_format_grade($averagegrade)
 433                                      . '/' . $this->activity_format_grade($activity->grademax);
 434                              break;
 435                              case B_ACTIVITYRESULTS_GRADE_FORMAT_ABS:
 436                                  $this->content->text .= $this->activity_format_grade($averagegrade);
 437                              break;
 438                              default:
 439                              case B_ACTIVITYRESULTS_GRADE_FORMAT_PCT:
 440                                  $this->content->text .= $this->activity_format_grade((float)$averagegrade /
 441                                          (float)$activity->grademax * 100).'%';
 442                              break;
 443                          }
 444                          $this->content->text .= '</td></tr>';
 445                      }
 446                      $this->content->text .= '</tbody></table>';
 447                  }
 448              break;
 449  
 450              case SEPARATEGROUPS:
 451                  // This is going to be just like no-groups mode, only we 'll filter
 452                  // out the grades from people not in our group.
 453                  if (!isloggedin()) {
 454                      // Not logged in, so show nothing.
 455                      return $this->content;
 456                  }
 457  
 458                  $mygroups = groups_get_all_groups($courseid, $USER->id);
 459                  if (empty($mygroups)) {
 460                      // Not member of a group, show nothing.
 461                      return $this->content;
 462                  }
 463  
 464                  // Get users from the same groups as me.
 465                  list($grouptest, $params) = $DB->get_in_or_equal(array_keys($mygroups));
 466                  $mygroupsusers = $DB->get_records_sql_menu(
 467                          'SELECT DISTINCT userid, 1 FROM {groups_members} WHERE groupid ' . $grouptest,
 468                          $params);
 469  
 470                  // Filter out the grades belonging to other users, and proceed as if there were no groups.
 471                  foreach ($grades as $key => $grade) {
 472                      if (!isset($mygroupsusers[$grade->userid])) {
 473                          unset($grades[$key]);
 474                      }
 475                  }
 476  
 477                  // No break, fall through to the default case now we have filtered the $grades array.
 478              default:
 479              case NOGROUPS:
 480                  // Single user mode.
 481                  $numbest  = empty($this->config->showbest) ? 0 : min($this->config->showbest, count($grades));
 482                  $numworst = empty($this->config->showworst) ? 0 : min($this->config->showworst, count($grades) - $numbest);
 483  
 484                  // Collect all the usernames we are going to need.
 485                  $remaining = $numbest;
 486                  $grade = end($grades);
 487                  while ($remaining--) {
 488                      $best[$grade->userid] = $grade->id;
 489                      $grade = prev($grades);
 490                  }
 491  
 492                  $remaining = $numworst;
 493                  $grade = reset($grades);
 494                  while ($remaining--) {
 495                      $worst[$grade->userid] = $grade->id;
 496                      $grade = next($grades);
 497                  }
 498  
 499                  if (empty($best) && empty($worst)) {
 500                      // Nothing to show, for some reason...
 501                      return $this->content;
 502                  }
 503  
 504                  // Now grab all the users from the database.
 505                  $userids = array_merge(array_keys($best), array_keys($worst));
 506                  $fields = array_merge(array('id', 'idnumber'), get_all_user_name_fields());
 507                  $fields = implode(',', $fields);
 508                  $users = $DB->get_records_list('user', 'id', $userids, '', $fields);
 509  
 510                  // Ready for output!
 511                  if ($activity->gradetype == GRADE_TYPE_SCALE) {
 512                      // We must display the results using scales.
 513                      $gradeformat = B_ACTIVITYRESULTS_GRADE_FORMAT_SCALE;
 514                      // Preload the scale.
 515                      $scale = $this->get_scale($activity->scaleid);
 516                  } else if (intval(empty($this->config->gradeformat))) {
 517                      $gradeformat = B_ACTIVITYRESULTS_GRADE_FORMAT_PCT;
 518                  } else {
 519                      $gradeformat = $this->config->gradeformat;
 520                  }
 521  
 522                  // Generate the header.
 523                  $this->content->text .= $this->activity_link($activity, $cm);
 524  
 525                  $rank = 0;
 526                  if (!empty($best)) {
 527                      $this->content->text .= '<table class="grades"><caption>';
 528                      if ($numbest == 1) {
 529                          $this->content->text .= get_string('bestgrade', 'block_activity_results');
 530                      } else {
 531                          $this->content->text .= get_string('bestgrades', 'block_activity_results', $numbest);
 532                      }
 533                      $this->content->text .= '</caption><colgroup class="number" />';
 534                      $this->content->text .= '<colgroup class="name" /><colgroup class="grade" /><tbody>';
 535                      foreach ($best as $userid => $gradeid) {
 536                          switch ($nameformat) {
 537                              case B_ACTIVITYRESULTS_NAME_FORMAT_ID:
 538                                  $thisname = get_string('user').' '.$users[$userid]->idnumber;
 539                              break;
 540                              case B_ACTIVITYRESULTS_NAME_FORMAT_ANON:
 541                                  $thisname = get_string('user');
 542                              break;
 543                              default:
 544                              case B_ACTIVITYRESULTS_NAME_FORMAT_FULL:
 545                                  if (has_capability('moodle/user:viewdetails', $context)) {
 546                                      $thisname = html_writer::link(new moodle_url('/user/view.php',
 547                                          array('id' => $userid, 'course' => $courseid)), fullname($users[$userid]));
 548                                  } else {
 549                                      $thisname = fullname($users[$userid]);
 550                                  }
 551                              break;
 552                          }
 553                          $this->content->text .= '<tr><td>'.(++$rank).'.</td><td>'.$thisname.'</td><td>';
 554                          switch ($gradeformat) {
 555                              case B_ACTIVITYRESULTS_GRADE_FORMAT_SCALE:
 556                                  // Round answer up and locate appropriate scale.
 557                                  $answer = (round($grades[$gradeid]->finalgrade, 0, PHP_ROUND_HALF_UP) - 1);
 558                                  if (isset($scale[$answer])) {
 559                                      $this->content->text .= $scale[$answer];
 560                                  } else {
 561                                      // Value is not in the scale.
 562                                      $this->content->text .= get_string('unknown', 'block_activity_results');
 563                                  }
 564                              break;
 565                              case B_ACTIVITYRESULTS_GRADE_FORMAT_FRA:
 566                                  $this->content->text .= $this->activity_format_grade($grades[$gradeid]->finalgrade);
 567                                  $this->content->text .= '/'.$this->activity_format_grade($activity->grademax);
 568                              break;
 569                              case B_ACTIVITYRESULTS_GRADE_FORMAT_ABS:
 570                                  $this->content->text .= $this->activity_format_grade($grades[$gradeid]->finalgrade);
 571                              break;
 572                              default:
 573                              case B_ACTIVITYRESULTS_GRADE_FORMAT_PCT:
 574                                  if ($activity->grademax) {
 575                                      $this->content->text .= $this->activity_format_grade((float)$grades[$gradeid]->finalgrade /
 576                                              (float)$activity->grademax * 100).'%';
 577                                  } else {
 578                                      $this->content->text .= '--%';
 579                                  }
 580                              break;
 581                          }
 582                          $this->content->text .= '</td></tr>';
 583                      }
 584                      $this->content->text .= '</tbody></table>';
 585                  }
 586  
 587                  $rank = 0;
 588                  if (!empty($worst)) {
 589                      $worst = array_reverse($worst, true);
 590                      $this->content->text .= '<table class="grades"><caption>';
 591                      if ($numbest == 1) {
 592                          $this->content->text .= get_string('worstgrade', 'block_activity_results');
 593                      } else {
 594                          $this->content->text .= get_string('worstgrades', 'block_activity_results', $numworst);
 595                      }
 596                      $this->content->text .= '</caption><colgroup class="number" />';
 597                      $this->content->text .= '<colgroup class="name" /><colgroup class="grade" /><tbody>';
 598                      foreach ($worst as $userid => $gradeid) {
 599                          switch ($nameformat) {
 600                              case B_ACTIVITYRESULTS_NAME_FORMAT_ID:
 601                                  $thisname = get_string('user').' '.$users[$userid]->idnumber;
 602                              break;
 603                              case B_ACTIVITYRESULTS_NAME_FORMAT_ANON:
 604                                  $thisname = get_string('user');
 605                              break;
 606                              default:
 607                              case B_ACTIVITYRESULTS_NAME_FORMAT_FULL:
 608                                  if (has_capability('moodle/user:viewdetails', $context)) {
 609                                      $thisname = html_writer::link(new moodle_url('/user/view.php',
 610                                          array('id' => $userid, 'course' => $courseid)), fullname($users[$userid]));
 611                                  } else {
 612                                      $thisname = fullname($users[$userid]);
 613                                  }
 614                              break;
 615                          }
 616                          $this->content->text .= '<tr><td>'.(++$rank).'.</td><td>'.$thisname.'</td><td>';
 617                          switch ($gradeformat) {
 618                              case B_ACTIVITYRESULTS_GRADE_FORMAT_SCALE:
 619                                  // Round answer up and locate appropriate scale.
 620                                  $answer = (round($grades[$gradeid]->finalgrade, 0, PHP_ROUND_HALF_UP) - 1);
 621                                  if (isset($scale[$answer])) {
 622                                      $this->content->text .= $scale[$answer];
 623                                  } else {
 624                                      // Value is not in the scale.
 625                                      $this->content->text .= get_string('unknown', 'block_activity_results');
 626                                  }
 627                              break;
 628                              case B_ACTIVITYRESULTS_GRADE_FORMAT_FRA:
 629                                  $this->content->text .= $this->activity_format_grade($grades[$gradeid]->finalgrade);
 630                                  $this->content->text .= '/'.$this->activity_format_grade($activity->grademax);
 631                              break;
 632                              case B_ACTIVITYRESULTS_GRADE_FORMAT_ABS:
 633                                  $this->content->text .= $this->activity_format_grade($grades[$gradeid]->finalgrade);
 634                              break;
 635                              default:
 636                              case B_ACTIVITYRESULTS_GRADE_FORMAT_PCT:
 637                                  if ($activity->grademax) {
 638                                      $this->content->text .= $this->activity_format_grade((float)$grades[$gradeid]->finalgrade /
 639                                              (float)$activity->grademax * 100).'%';
 640                                  } else {
 641                                      $this->content->text .= '--%';
 642                                  }
 643                              break;
 644                          }
 645                          $this->content->text .= '</td></tr>';
 646                      }
 647                      $this->content->text .= '</tbody></table>';
 648                  }
 649              break;
 650          }
 651  
 652          return $this->content;
 653      }
 654  
 655      /**
 656       * Allows the block to be added multiple times to a single page
 657       * @return boolean
 658       */
 659      public function instance_allow_multiple() {
 660          return true;
 661      }
 662  
 663      /**
 664       * Formats the grade to the specified decimal points
 665       * @param float $grade
 666       * @return string
 667       */
 668      private function activity_format_grade($grade) {
 669          if (is_null($grade)) {
 670              return get_string('notyetgraded', 'block_activity_results');
 671          }
 672          return format_float($grade, $this->config->decimalpoints);
 673      }
 674  
 675      /**
 676       * Generates the Link to the activity module when displaed outside of the module
 677       * @param stdclass $activity
 678       * @param stdclass $cm
 679       * @return string
 680       */
 681      private function activity_link($activity, $cm) {
 682  
 683          $o = html_writer::start_tag('h3');
 684          $o .= html_writer::link(new moodle_url('/mod/'.$activity->itemmodule.'/view.php',
 685          array('id' => $cm->id)), $activity->itemname);
 686          $o .= html_writer::end_tag('h3');
 687          return $o;
 688      }
 689  
 690      /**
 691       * Generates a numeric array of scale entries
 692       * @param int $scaleid
 693       * @return array
 694       */
 695      private function get_scale($scaleid) {
 696          global $DB;
 697          $scaletext = $DB->get_field('scale', 'scale', array('id' => $scaleid), IGNORE_MISSING);
 698          $scale = explode ( ',', $scaletext);
 699          return $scale;
 700  
 701      }
 702  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1