Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
/badges/ -> award.php (source)

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