Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
/badges/ -> award.php (source)

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

   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   * Handle manual badge award.
  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  require_once($CFG->dirroot . '/badges/lib/awardlib.php');
  30  
  31  $badgeid = required_param('id', PARAM_INT);
  32  $role = optional_param('role', 0, PARAM_INT);
  33  $award = optional_param('award', false, PARAM_BOOL);
  34  $revoke = optional_param('revoke', false, PARAM_BOOL);
  35  
  36  require_login();
  37  
  38  if (empty($CFG->enablebadges)) {
  39      throw new \moodle_exception('badgesdisabled', 'badges');
  40  }
  41  
  42  $badge = new badge($badgeid);
  43  $context = $badge->get_context();
  44  $isadmin = is_siteadmin($USER);
  45  
  46  $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
  47  
  48  if ($badge->type == BADGE_TYPE_COURSE) {
  49      if (empty($CFG->badges_allowcoursebadges)) {
  50          throw new \moodle_exception('coursebadgesdisabled', 'badges');
  51      }
  52      require_login($badge->courseid);
  53      $course = get_course($badge->courseid);
  54      $heading = format_string($course->fullname, true, ['context' => $context]);
  55      $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
  56      $PAGE->set_pagelayout('standard');
  57      navigation_node::override_active_url($navurl);
  58  } else {
  59      $PAGE->set_pagelayout('admin');
  60      $heading = get_string('administrationsite');
  61      navigation_node::override_active_url($navurl, true);
  62  }
  63  
  64  require_capability('moodle/badges:awardbadge', $context);
  65  
  66  $url = new moodle_url('/badges/award.php', array('id' => $badgeid, 'role' => $role));
  67  $PAGE->set_url($url);
  68  $PAGE->set_context($context);
  69  
  70  // Set up navigation and breadcrumbs.
  71  $strrecipients = get_string('recipients', 'badges');
  72  $PAGE->navbar->add($badge->name, new moodle_url('overview.php', array('id' => $badge->id)))
  73      ->add($strrecipients, new moodle_url('recipients.php', array('id' => $badge->id)))
  74      ->add(get_string('award', 'badges'));
  75  $PAGE->set_title($strrecipients);
  76  $PAGE->set_heading($heading);
  77  
  78  if (!$badge->is_active()) {
  79      echo $OUTPUT->header();
  80      echo $OUTPUT->notification(get_string('donotaward', 'badges'));
  81      echo $OUTPUT->footer();
  82      die();
  83  }
  84  
  85  $returnurl = new moodle_url('recipients.php', array('id' => $badge->id));
  86  $returnlink = html_writer::link($returnurl, $strrecipients);
  87  $actionbar = new \core_badges\output\standard_action_bar($PAGE, $badge->type, false, false, $returnurl);
  88  $output = $PAGE->get_renderer('core', 'badges');
  89  $tertiarynav = $output->render_tertiary_navigation($actionbar);
  90  
  91  // Roles that can award this badge.
  92  $acceptedroles = array_keys($badge->criteria[BADGE_CRITERIA_TYPE_MANUAL]->params);
  93  
  94  if (empty($acceptedroles)) {
  95      echo $OUTPUT->header();
  96      echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $returnlink));
  97      echo $OUTPUT->footer();
  98      die();
  99  }
 100  
 101  // Get groupmode and currentgroup before going further.
 102  $groupmode = groups_get_course_groupmode($COURSE);  // Groups are being used.
 103  $currentgroup = groups_get_course_group($COURSE, true); // Get active group.
 104  
 105  // Check groupmode (SEPARATEGROUPS), currentgroup and capability (or admin).
 106  if ($groupmode == SEPARATEGROUPS && empty($currentgroup) &&
 107      !has_capability('moodle/site:accessallgroups', $context) && !is_siteadmin() ) {
 108      echo $OUTPUT->header();
 109      echo $OUTPUT->heading(get_string("notingroup"));
 110      echo $OUTPUT->footer();
 111      die();
 112  }
 113  
 114  if (count($acceptedroles) > 1) {
 115      // If there is more than one role that can award a badge, prompt user to make a selection.
 116      // If it is an admin, include all accepted roles, otherwise only the ones that current user has in this context.
 117      if ($isadmin) {
 118          $selection = $acceptedroles;
 119      } else {
 120          // Get all the roles that user has and use the ones required by this badge.
 121          $roles = get_user_roles($context, $USER->id);
 122          $roleids = array_map(function($o) {
 123              return $o->roleid;
 124          }, $roles);
 125          $selection = array_intersect($acceptedroles, $roleids);
 126      }
 127  
 128      if (!empty($selection)) {
 129          list($usertest, $userparams) = $DB->get_in_or_equal($selection, SQL_PARAMS_NAMED, 'existing', true);
 130          $options = $DB->get_records_sql('SELECT * FROM {role} WHERE id ' . $usertest, $userparams);
 131          foreach ($options as $p) {
 132              $select[$p->id] = role_get_name($p);
 133          }
 134          if (!$role) {
 135              $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
 136              echo $OUTPUT->header();
 137              echo $tertiarynav;
 138              echo $OUTPUT->box($OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, '', array('' => 'choosedots'),
 139                  null, array('label' => get_string('selectaward', 'badges'))));
 140              echo $OUTPUT->footer();
 141              die();
 142          } else {
 143              $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
 144              $issuerrole = new stdClass();
 145              $issuerrole->roleid = $role;
 146              $roleselect = $OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, $role, null, null,
 147                  array('label' => get_string('selectaward', 'badges')));
 148          }
 149      } else {
 150          echo $OUTPUT->header();
 151          echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $returnlink));
 152          echo $OUTPUT->footer();
 153          die();
 154      }
 155  } else {
 156      // User has to be an admin or the one with the required role.
 157      $users = get_role_users($acceptedroles[0], $context, true, 'u.id', 'u.id ASC');
 158      $usersids = array_keys($users);
 159      if (!$isadmin && !in_array($USER->id, $usersids)) {
 160          echo $OUTPUT->header();
 161          echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $returnlink));
 162          echo $OUTPUT->footer();
 163          die();
 164      } else {
 165          $issuerrole = new stdClass();
 166          $issuerrole->roleid = $acceptedroles[0];
 167      }
 168  }
 169  
 170  $options = array(
 171          'badgeid' => $badge->id,
 172          'context' => $context,
 173          'issuerid' => $USER->id,
 174          'issuerrole' => $issuerrole->roleid,
 175          'currentgroup' => $currentgroup,
 176          'url' => $url,
 177      );
 178  $existingselector = new badge_existing_users_selector('existingrecipients', $options);
 179  $recipientselector = new badge_potential_users_selector('potentialrecipients', $options);
 180  $recipientselector->set_existing_recipients($existingselector->find_users(''));
 181  
 182  if ($award && data_submitted() && has_capability('moodle/badges:awardbadge', $context)) {
 183      require_sesskey();
 184      $users = $recipientselector->get_selected_users();
 185      foreach ($users as $user) {
 186          if (process_manual_award($user->id, $USER->id, $issuerrole->roleid, $badgeid)) {
 187              // If badge was successfully awarded, review manual badge criteria.
 188              $data = new stdClass();
 189              $data->crit = $badge->criteria[BADGE_CRITERIA_TYPE_MANUAL];
 190              $data->userid = $user->id;
 191              badges_award_handle_manual_criteria_review($data);
 192          } else {
 193              echo $OUTPUT->error_text(get_string('error:cannotawardbadge', 'badges'));
 194          }
 195      }
 196  
 197      $recipientselector->invalidate_selected_users();
 198      $existingselector->invalidate_selected_users();
 199      $recipientselector->set_existing_recipients($existingselector->find_users(''));
 200  } else if ($revoke && data_submitted() && has_capability('moodle/badges:revokebadge', $context)) {
 201      require_sesskey();
 202      $users = $existingselector->get_selected_users();
 203  
 204      foreach ($users as $user) {
 205          if (!process_manual_revoke($user->id, $USER->id, $issuerrole->roleid, $badgeid)) {
 206              echo $OUTPUT->error_text(get_string('error:cannotrevokebadge', 'badges'));
 207          }
 208      }
 209  
 210      $recipientselector->invalidate_selected_users();
 211      $existingselector->invalidate_selected_users();
 212      $recipientselector->set_existing_recipients($existingselector->find_users(''));
 213  }
 214  
 215  echo $OUTPUT->header();
 216  echo $tertiarynav;
 217  echo $OUTPUT->heading($strrecipients);
 218  
 219  // Print group selector/dropdown menu (find out current groups mode).
 220  groups_print_course_menu($COURSE, $url);
 221  
 222  if (count($acceptedroles) > 1) {
 223      echo $OUTPUT->box($roleselect);
 224  }
 225  
 226  echo $output->recipients_selection_form($existingselector, $recipientselector);
 227  echo $OUTPUT->footer();