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.
/badges/ -> action.php (source)

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

   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   * Page to handle actions associated with badges management.
  19   *
  20   * @package    core
  21   * @subpackage badges
  22   * @copyright  2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @author     Yuliya Bozhko <yuliya.bozhko@totaralms.com>
  25   */
  26  
  27  require_once(__DIR__ . '/../config.php');
  28  require_once($CFG->libdir . '/badgeslib.php');
  29  
  30  $badgeid = required_param('id', PARAM_INT);
  31  $copy = optional_param('copy', 0, PARAM_BOOL);
  32  $activate = optional_param('activate', 0, PARAM_BOOL);
  33  $deactivate = optional_param('lock', 0, PARAM_BOOL);
  34  $confirm   = optional_param('confirm', 0, PARAM_BOOL);
  35  $return = optional_param('return', 0, PARAM_LOCALURL);
  36  
  37  require_login();
  38  
  39  $badge = new badge($badgeid);
  40  $context = $badge->get_context();
  41  $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
  42  
  43  if ($badge->type == BADGE_TYPE_COURSE) {
  44      require_login($badge->courseid);
  45      $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
  46      $PAGE->set_pagelayout('standard');
  47      navigation_node::override_active_url($navurl);
  48  } else {
  49      $PAGE->set_pagelayout('admin');
  50      navigation_node::override_active_url($navurl, true);
  51  }
  52  
  53  $PAGE->set_context($context);
  54  $PAGE->set_url('/badges/action.php', array('id' => $badge->id));
  55  
  56  if ($return !== 0) {
  57      $returnurl = new moodle_url($return);
  58  } else {
  59      $returnurl = new moodle_url('/badges/overview.php', array('id' => $badge->id));
  60  }
  61  $returnurl->remove_params('awards');
  62  
  63  if ($copy) {
  64      require_sesskey();
  65      require_capability('moodle/badges:createbadge', $context);
  66  
  67      $cloneid = $badge->make_clone();
  68      // If a user can edit badge details, they will be redirected to the edit page.
  69      if (has_capability('moodle/badges:configuredetails', $context)) {
  70          redirect(new moodle_url('/badges/edit.php', array('id' => $cloneid, 'action' => 'badge')));
  71      }
  72      redirect(new moodle_url('/badges/overview.php', array('id' => $cloneid)));
  73  }
  74  
  75  if ($activate) {
  76      require_capability('moodle/badges:configurecriteria', $context);
  77  
  78      $PAGE->url->param('activate', 1);
  79      $status = ($badge->status == BADGE_STATUS_INACTIVE) ? BADGE_STATUS_ACTIVE : BADGE_STATUS_ACTIVE_LOCKED;
  80      if ($confirm == 1) {
  81          require_sesskey();
  82          $badge->set_status($status);
  83          $returnurl->param('msg', 'activatesuccess');
  84  
  85          if ($badge->type == BADGE_TYPE_SITE) {
  86              // Review on cron if there are more than 1000 users who can earn a site-level badge.
  87              $sql = 'SELECT COUNT(u.id) as num
  88                          FROM {user} u
  89                          LEFT JOIN {badge_issued} bi
  90                              ON u.id = bi.userid AND bi.badgeid = :badgeid
  91                          WHERE bi.badgeid IS NULL AND u.id != :guestid AND u.deleted = 0';
  92              $toearn = $DB->get_record_sql($sql, array('badgeid' => $badge->id, 'guestid' => $CFG->siteguest));
  93  
  94              if ($toearn->num < 1000) {
  95                  $awards = $badge->review_all_criteria();
  96                  $returnurl->param('awards', $awards);
  97              } else {
  98                  $returnurl->param('awards', 'cron');
  99              }
 100          } else {
 101              $awards = $badge->review_all_criteria();
 102              $returnurl->param('awards', $awards);
 103          }
 104          redirect($returnurl);
 105      }
 106  
 107      $strheading = get_string('reviewbadge', 'badges');
 108      $PAGE->navbar->add($strheading);
 109      $PAGE->set_title($strheading);
 110      $PAGE->set_heading($badge->name);
 111      echo $OUTPUT->header();
 112      echo $OUTPUT->heading($strheading);
 113  
 114      $params = array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(), 'confirm' => 1, 'return' => $return);
 115      $url = new moodle_url('/badges/action.php', $params);
 116  
 117      if (!$badge->has_criteria()) {
 118          redirect($returnurl, get_string('error:cannotact', 'badges') . get_string('nocriteria', 'badges'), null, \core\output\notification::NOTIFY_ERROR);
 119      } else {
 120          $message = get_string('reviewconfirm', 'badges', $badge->name);
 121          echo $OUTPUT->confirm($message, $url, $returnurl);
 122      }
 123      echo $OUTPUT->footer();
 124      die;
 125  }
 126  
 127  if ($deactivate) {
 128      require_sesskey();
 129      require_capability('moodle/badges:configurecriteria', $context);
 130  
 131      $status = ($badge->status == BADGE_STATUS_ACTIVE) ? BADGE_STATUS_INACTIVE : BADGE_STATUS_INACTIVE_LOCKED;
 132      $badge->set_status($status);
 133      redirect($returnurl);
 134  }