Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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 ajax rating submissions
  19   *
  20   * It is similar to rate.php. Unlike rate.php a return url is NOT 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  define('AJAX_SCRIPT', true);
  29  
  30  require_once('../config.php');
  31  require_once($CFG->dirroot.'/rating/lib.php');
  32  
  33  $contextid         = required_param('contextid', PARAM_INT);
  34  $component         = required_param('component', PARAM_COMPONENT);
  35  $ratingarea        = required_param('ratingarea', PARAM_AREA);
  36  $itemid            = required_param('itemid', PARAM_INT);
  37  $scaleid           = required_param('scaleid', PARAM_INT);
  38  $userrating        = required_param('rating', PARAM_INT);
  39  $rateduserid       = required_param('rateduserid', PARAM_INT); // The user being rated. Required to update their grade.
  40  $aggregationmethod = optional_param('aggregation', RATING_AGGREGATE_NONE, PARAM_INT); // Used to calculate the aggregate to return.
  41  
  42  $result = new stdClass;
  43  
  44  // If session has expired and its an ajax request so we cant do a page redirect.
  45  if (!isloggedin()) {
  46      $result->error = get_string('sessionerroruser', 'error');
  47      echo json_encode($result);
  48      die();
  49  }
  50  
  51  list($context, $course, $cm) = get_context_info_array($contextid);
  52  require_login($course, false, $cm);
  53  
  54  $contextid = null; // Now we have a context object, throw away the id from the user.
  55  $PAGE->set_context($context);
  56  $PAGE->set_url('/rating/rate_ajax.php', array('contextid' => $context->id));
  57  
  58  if (!confirm_sesskey() || !has_capability('moodle/rating:rate', $context)) {
  59      echo $OUTPUT->header();
  60      echo get_string('ratepermissiondenied', 'rating');
  61      echo $OUTPUT->footer();
  62      die();
  63  }
  64  
  65  $rm = new rating_manager();
  66  $result = $rm->add_rating($cm, $context, $component, $ratingarea, $itemid, $scaleid, $userrating, $rateduserid, $aggregationmethod);
  67  
  68  // Return translated error.
  69  if (!empty($result->error)) {
  70      $result->error = get_string($result->error, 'rating');
  71  }
  72  
  73  echo json_encode($result);