Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
/rating/ -> index.php (source)

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

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * A page to display a list of ratings for a given item (forum post etc)
  19   *
  20   * @package    core_rating
  21   * @category   rating
  22   * @copyright  2010 Andrew Davis
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require_once("../config.php");
  27  require_once ("lib.php");
  28  
  29  $contextid  = required_param('contextid', PARAM_INT);
  30  $component  = required_param('component', PARAM_COMPONENT);
  31  $ratingarea = required_param('ratingarea', PARAM_AREA);
  32  $itemid     = required_param('itemid', PARAM_INT);
  33  $scaleid    = required_param('scaleid', PARAM_INT);
  34  $sort       = optional_param('sort', '', PARAM_ALPHA);
  35  $popup      = optional_param('popup', 0, PARAM_INT); // Any non-zero value if in a popup window.
  36  
  37  list($context, $course, $cm) = get_context_info_array($contextid);
  38  require_login($course, false, $cm);
  39  
  40  $url = new moodle_url('/rating/index.php', array('contextid' => $contextid,
  41                                                   'component' => $component,
  42                                                   'ratingarea' => $ratingarea,
  43                                                   'itemid' => $itemid,
  44                                                   'scaleid' => $scaleid));
  45  if (!empty($sort)) {
  46      $url->param('sort', $sort);
  47  }
  48  if (!empty($popup)) {
  49      $url->param('popup', $popup);
  50  }
  51  $PAGE->set_url($url);
  52  $PAGE->set_context($context);
  53  
  54  if ($popup) {
  55      $PAGE->set_pagelayout('popup');
  56  }
  57  
  58  $params = array('contextid' => $contextid,
  59                  'component' => $component,
  60                  'ratingarea' => $ratingarea,
  61                  'itemid' => $itemid,
  62                  'scaleid' => $scaleid);
  63  if (!has_capability('moodle/rating:view', $context) ||
  64          !component_callback($component, 'rating_can_see_item_ratings', array($params), true)) {
  65      throw new \moodle_exception('noviewrate', 'rating');
  66  }
  67  
  68  $canviewallratings = has_capability('moodle/rating:viewall', $context);
  69  
  70  switch ($sort) {
  71      case 'firstname':
  72          $sqlsort = "u.firstname ASC";
  73          break;
  74      case 'rating':
  75          $sqlsort = "r.rating ASC";
  76          break;
  77      default:
  78          $sqlsort = "r.timemodified ASC";
  79  }
  80  
  81  $scalemenu = make_grades_menu($scaleid);
  82  
  83  $strrating  = get_string('rating', 'rating');
  84  $strname    = get_string('name');
  85  $strtime    = get_string('time');
  86  
  87  $PAGE->set_title(get_string('allratingsforitem', 'rating'));
  88  echo $OUTPUT->header();
  89  
  90  $ratingoptions = new stdClass;
  91  $ratingoptions->context = $context;
  92  $ratingoptions->component = $component;
  93  $ratingoptions->ratingarea = $ratingarea;
  94  $ratingoptions->itemid = $itemid;
  95  $ratingoptions->sort = $sqlsort;
  96  
  97  $rm = new rating_manager();
  98  $ratings = $rm->get_all_ratings_for_item($ratingoptions);
  99  if (!$ratings) {
 100      $msg = get_string('noratings', 'rating');
 101      echo html_writer::tag('div', $msg, array('class' => 'mdl-align'));
 102  } else {
 103      // To get the sort URL, copy the current URL and remove any previous sort.
 104      $sorturl = new moodle_url($url);
 105      $sorturl->remove_params('sort');
 106  
 107      $table = new html_table;
 108      $table->cellpadding = 3;
 109      $table->cellspacing = 3;
 110      $table->attributes['class'] = 'generalbox ratingtable';
 111      $table->head = array(
 112          '',
 113          html_writer::link(new moodle_url($sorturl, array('sort' => 'firstname')), $strname),
 114          html_writer::link(new moodle_url($sorturl, array('sort' => 'rating')), $strrating),
 115          html_writer::link(new moodle_url($sorturl, array('sort' => 'time')), $strtime)
 116      );
 117      $table->colclasses = array('', 'firstname', 'rating', 'time');
 118      $table->data = array();
 119  
 120      // If the scale was changed after ratings were submitted some ratings may have a value above the current maximum.
 121      // We can't just do count($scalemenu) - 1 as custom scales start at index 1, not 0.
 122      $maxrating = max(array_keys($scalemenu));
 123  
 124      foreach ($ratings as $rating) {
 125          if (!$canviewallratings and $USER->id != $rating->userid) {
 126              continue;
 127          }
 128  
 129          // Undo the aliasing of the user id column from fields::get_sql().
 130          // We could clone the rating object or preserve the rating id if we needed it again
 131          // but we don't.
 132          $rating->id = $rating->userid;
 133  
 134          $row = new html_table_row();
 135          $row->attributes['class'] = 'ratingitemheader';
 136          if ($course && $course->id) {
 137              $row->cells[] = $OUTPUT->user_picture($rating, array('courseid' => $course->id));
 138          } else {
 139              $row->cells[] = $OUTPUT->user_picture($rating);
 140          }
 141          $row->cells[] = fullname($rating);
 142          if ($rating->rating > $maxrating) {
 143              $rating->rating = $maxrating;
 144          }
 145          $row->cells[] = $scalemenu[$rating->rating];
 146          $row->cells[] = userdate($rating->timemodified);
 147          $table->data[] = $row;
 148      }
 149      echo html_writer::table($table);
 150  }
 151  if ($popup) {
 152      echo $OUTPUT->close_window_button();
 153  }
 154  echo $OUTPUT->footer();