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/ -> rate.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   * This page receives non-ajax rating submissions
  19   *
  20   * It is similar to rate_ajax.php. Unlike rate_ajax.php a return url is required.
  21   *
  22   * @package    core_rating
  23   * @category   rating
  24   * @copyright  2010 Andrew Davis
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  require_once('../config.php');
  29  require_once($CFG->dirroot.'/rating/lib.php');
  30  
  31  $contextid   = required_param('contextid', PARAM_INT);
  32  $component   = required_param('component', PARAM_COMPONENT);
  33  $ratingarea  = required_param('ratingarea', PARAM_AREA);
  34  $itemid      = required_param('itemid', PARAM_INT);
  35  $scaleid     = required_param('scaleid', PARAM_INT);
  36  $userrating  = required_param('rating', PARAM_INT);
  37  $rateduserid = required_param('rateduserid', PARAM_INT); // Which user is being rated. Required to update their grade.
  38  $returnurl   = required_param('returnurl', PARAM_LOCALURL); // Required for non-ajax requests.
  39  
  40  $result = new stdClass;
  41  
  42  list($context, $course, $cm) = get_context_info_array($contextid);
  43  require_login($course, false, $cm);
  44  
  45  $contextid = null; // Now we have a context object, throw away the id from the user.
  46  $PAGE->set_context($context);
  47  $PAGE->set_url('/rating/rate.php', array('contextid' => $context->id));
  48  
  49  if (!confirm_sesskey() || !has_capability('moodle/rating:rate', $context)) {
  50      throw new \moodle_exception('ratepermissiondenied', 'rating');
  51  }
  52  
  53  $rm = new rating_manager();
  54  
  55  // Check the module rating permissions.
  56  // Doing this check here rather than within rating_manager::get_ratings() so we can choose how to handle the error.
  57  $pluginpermissionsarray = $rm->get_plugin_permissions_array($context->id, $component, $ratingarea);
  58  
  59  if (!$pluginpermissionsarray['rate']) {
  60      throw new \moodle_exception('ratepermissiondenied', 'rating');
  61  } else {
  62      $params = array(
  63          'context'     => $context,
  64          'component'   => $component,
  65          'ratingarea'  => $ratingarea,
  66          'itemid'      => $itemid,
  67          'scaleid'     => $scaleid,
  68          'rating'      => $userrating,
  69          'rateduserid' => $rateduserid
  70      );
  71      if (!$rm->check_rating_is_valid($params)) {
  72          echo $OUTPUT->header();
  73          echo get_string('ratinginvalid', 'rating');
  74          echo $OUTPUT->footer();
  75          die();
  76      }
  77  }
  78  
  79  if ($userrating != RATING_UNSET_RATING) {
  80      $ratingoptions = new stdClass;
  81      $ratingoptions->context = $context;
  82      $ratingoptions->component = $component;
  83      $ratingoptions->ratingarea = $ratingarea;
  84      $ratingoptions->itemid  = $itemid;
  85      $ratingoptions->scaleid = $scaleid;
  86      $ratingoptions->userid  = $USER->id;
  87  
  88      $rating = new rating($ratingoptions);
  89      $rating->update_rating($userrating);
  90  } else { // Delete the rating if the user set to "Rate..."
  91      $options = new stdClass;
  92      $options->contextid = $context->id;
  93      $options->component = $component;
  94      $options->ratingarea = $ratingarea;
  95      $options->userid = $USER->id;
  96      $options->itemid = $itemid;
  97  
  98      $rm->delete_ratings($options);
  99  }
 100  
 101  if (!empty($cm) && $context->contextlevel == CONTEXT_MODULE) {
 102      // Tell the module that its grades have changed.
 103      $modinstance = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST);
 104      $modinstance->cmidnumber = $cm->id; // MDL-12961.
 105      $functionname = $cm->modname.'_update_grades';
 106      require_once($CFG->dirroot."/mod/{$cm->modname}/lib.php");
 107      if (function_exists($functionname)) {
 108          $functionname($modinstance, $rateduserid);
 109      }
 110  }
 111  
 112  redirect($returnurl);