Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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   * Editing badge details, criteria, messages
  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->libdir . '/filelib.php');
  30  
  31  $badgeid = required_param('id', PARAM_INT);
  32  $action = optional_param('action', 'badge', PARAM_TEXT);
  33  
  34  require_login();
  35  
  36  if (empty($CFG->enablebadges)) {
  37      throw new \moodle_exception('badgesdisabled', 'badges');
  38  }
  39  
  40  $badge = new badge($badgeid);
  41  $context = $badge->get_context();
  42  $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
  43  
  44  if ($action == 'message') {
  45      require_capability('moodle/badges:configuremessages', $context);
  46  } else {
  47      require_capability('moodle/badges:configuredetails', $context);
  48  }
  49  
  50  if ($badge->type == BADGE_TYPE_COURSE) {
  51      if (empty($CFG->badges_allowcoursebadges)) {
  52          throw new \moodle_exception('coursebadgesdisabled', 'badges');
  53      }
  54      require_login($badge->courseid);
  55      $course = get_course($badge->courseid);
  56      $heading = format_string($course->fullname, true, ['context' => $context]);
  57      $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
  58      $PAGE->set_pagelayout('incourse');
  59      navigation_node::override_active_url($navurl);
  60  } else {
  61      $PAGE->set_pagelayout('admin');
  62      $heading = get_string('administrationsite');
  63      navigation_node::override_active_url($navurl, true);
  64  }
  65  
  66  $currenturl = new moodle_url('/badges/edit.php', array('id' => $badge->id, 'action' => $action));
  67  
  68  $PAGE->set_context($context);
  69  $PAGE->set_url($currenturl);
  70  $PAGE->set_heading($heading);
  71  $PAGE->set_title($badge->name);
  72  $PAGE->add_body_class('limitedwidth');
  73  $PAGE->navbar->add($badge->name);
  74  
  75  $output = $PAGE->get_renderer('core', 'badges');
  76  $statusmsg = '';
  77  $errormsg  = '';
  78  
  79  $badge->message = clean_text($badge->message, FORMAT_HTML);
  80  $editoroptions = array(
  81          'subdirs' => 0,
  82          'maxbytes' => 0,
  83          'maxfiles' => 0,
  84          'changeformat' => 0,
  85          'context' => $context,
  86          'noclean' => false,
  87          'trusttext' => false
  88          );
  89  $badge = file_prepare_standard_editor($badge, 'message', $editoroptions, $context);
  90  
  91  $formclass = '\core_badges\form' . '\\' . $action;
  92  $form = new $formclass($currenturl, array('badge' => $badge, 'action' => $action, 'editoroptions' => $editoroptions));
  93  
  94  if ($form->is_cancelled()) {
  95      redirect(new moodle_url('/badges/overview.php', array('id' => $badgeid)));
  96  } else if ($form->is_submitted() && $form->is_validated() && ($data = $form->get_data())) {
  97      if ($action == 'badge') {
  98          $badge->name = $data->name;
  99          $badge->version = trim($data->version);
 100          $badge->language = $data->language;
 101          $badge->description = $data->description;
 102          $badge->imageauthorname = $data->imageauthorname;
 103          $badge->imageauthoremail = $data->imageauthoremail;
 104          $badge->imageauthorurl = $data->imageauthorurl;
 105          $badge->imagecaption = $data->imagecaption;
 106          $badge->usermodified = $USER->id;
 107          if (badges_open_badges_backpack_api() == OPEN_BADGES_V1) {
 108              $badge->issuername = $data->issuername;
 109              $badge->issuerurl = $data->issuerurl;
 110              $badge->issuercontact = $data->issuercontact;
 111          }
 112          $badge->expiredate = ($data->expiry == 1) ? $data->expiredate : null;
 113          $badge->expireperiod = ($data->expiry == 2) ? $data->expireperiod : null;
 114  
 115          // Need to unset message_editor options to avoid errors on form edit.
 116          unset($badge->messageformat);
 117          unset($badge->message_editor);
 118  
 119          if ($badge->save()) {
 120              core_tag_tag::set_item_tags('core_badges', 'badge', $badge->id, $context, $data->tags);
 121              badges_process_badge_image($badge, $form->save_temp_file('image'));
 122              $form->set_data($badge);
 123              $statusmsg = get_string('changessaved');
 124          } else {
 125              $errormsg = get_string('error:save', 'badges');
 126          }
 127      } else if ($action == 'message') {
 128          // Calculate next message cron if form data is different from original badge data.
 129          if ($data->notification != $badge->notification) {
 130              if ($data->notification > BADGE_MESSAGE_ALWAYS) {
 131                  $badge->nextcron = badges_calculate_message_schedule($data->notification);
 132              } else {
 133                  $badge->nextcron = null;
 134              }
 135          }
 136  
 137          $badge->message = clean_text($data->message_editor['text'], FORMAT_HTML);
 138          $badge->messagesubject = $data->messagesubject;
 139          $badge->notification = $data->notification;
 140          $badge->attachment = $data->attachment;
 141  
 142          unset($badge->messageformat);
 143          unset($badge->message_editor);
 144          if ($badge->save()) {
 145              $statusmsg = get_string('changessaved');
 146          } else {
 147              $errormsg = get_string('error:save', 'badges');
 148          }
 149      }
 150  }
 151  
 152  echo $OUTPUT->header();
 153  $actionbar = new \core_badges\output\manage_badge_action_bar($badge, $PAGE);
 154  echo $output->render_tertiary_navigation($actionbar);
 155  
 156  echo $OUTPUT->heading(print_badge_image($badge, $context, 'small') . ' ' . $badge->name);
 157  
 158  if ($errormsg !== '') {
 159      echo $OUTPUT->notification($errormsg);
 160  
 161  } else if ($statusmsg !== '') {
 162      echo $OUTPUT->notification($statusmsg, 'notifysuccess');
 163  }
 164  echo $output->print_badge_status_box($badge);
 165  
 166  $form->display();
 167  
 168  echo $OUTPUT->footer();