Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Grade export key edit page.
  20   *
  21   * @package   moodlecore
  22   * @copyright 2008 Petr Skoda
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require_once('../../config.php');
  27  require_once ('key_form.php');
  28  require_once($CFG->dirroot.'/grade/lib.php');
  29  
  30  /// get url variables
  31  $courseid = optional_param('courseid', 0, PARAM_INT);
  32  $id       = optional_param('id', 0, PARAM_INT); // The key's id
  33  $delete   = optional_param('delete', 0, PARAM_BOOL);
  34  $confirm  = optional_param('confirm', 0, PARAM_BOOL);
  35  
  36  $PAGE->set_url('/grade/export/key.php', array('id' => $id, 'courseid' => $courseid));
  37  
  38  if ($id) {
  39      if (!$key = $DB->get_record('user_private_key', array('id' => $id))) {
  40          throw new \moodle_exception('invalidgroupid');
  41      }
  42      if (empty($courseid)) {
  43          $courseid = $key->instance;
  44  
  45      } else if ($courseid != $key->instance) {
  46          throw new \moodle_exception('invalidcourseid');
  47      }
  48  
  49      if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
  50          throw new \moodle_exception('invalidcourseid');
  51      }
  52  
  53  } else {
  54      if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
  55          throw new \moodle_exception('invalidcourseid');
  56      }
  57      $key = new stdClass();
  58  }
  59  
  60  $key->courseid = $course->id;
  61  
  62  require_login($course);
  63  $context = context_course::instance($course->id);
  64  require_capability('moodle/grade:export', $context);
  65  
  66  // Check if the user has at least one grade publishing capability.
  67  $plugins = grade_helper::get_plugins_export($course->id);
  68  if (!isset($plugins['keymanager'])) {
  69      throw new \moodle_exception('nopermissions');
  70  }
  71  
  72  // extra security check
  73  if (!empty($key->userid) and $USER->id != $key->userid) {
  74      throw new \moodle_exception('notownerofkey');
  75  }
  76  
  77  $returnurl = $CFG->wwwroot.'/grade/export/keymanager.php?id='.$course->id;
  78  
  79  $strkeys   = get_string('keymanager', 'userkey');
  80  $strexportgrades = get_string('export', 'grades');
  81  $PAGE->navbar->add($strexportgrades, new moodle_url('/grade/export/index.php', ['id' => $courseid]));
  82  $PAGE->navbar->add($strkeys, new moodle_url('/grade/export/keymanager.php', ['id' => $courseid]));
  83  
  84  if ($id and $delete) {
  85      if (!$confirm) {
  86          $PAGE->set_title(get_string('deleteselectedkey'));
  87          $PAGE->set_heading($course->fullname);
  88          $PAGE->set_secondary_active_tab('grades');
  89          $PAGE->navbar->add(get_string('deleteuserkey', 'userkey'));
  90  
  91          echo $OUTPUT->header();
  92          $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
  93          $optionsno  = array('id'=>$courseid);
  94          $formcontinue = new single_button(new moodle_url('key.php', $optionsyes), get_string('yes'), 'get');
  95          $formcancel = new single_button(new moodle_url('keymanager.php', $optionsno), get_string('no'), 'get');
  96          echo $OUTPUT->confirm(get_string('deletekeyconfirm', 'userkey', $key->value), $formcontinue, $formcancel);
  97          echo $OUTPUT->footer();
  98          die;
  99  
 100      } else if (confirm_sesskey()){
 101          $DB->delete_records('user_private_key', array('id' => $id));
 102          redirect('keymanager.php?id='.$course->id);
 103      }
 104  }
 105  
 106  /// First create the form
 107  $editform = new key_form();
 108  $editform->set_data($key);
 109  
 110  if ($editform->is_cancelled()) {
 111      redirect($returnurl);
 112  
 113  } elseif ($data = $editform->get_data()) {
 114  
 115      if ($data->id) {
 116          $record = new stdClass();
 117          $record->id            = $data->id;
 118          $record->iprestriction = $data->iprestriction;
 119          $record->validuntil    = $data->validuntil;
 120          $DB->update_record('user_private_key', $record);
 121      } else {
 122          create_user_key('grade/export', $USER->id, $course->id, $data->iprestriction, $data->validuntil);
 123      }
 124  
 125      redirect($returnurl);
 126  }
 127  
 128  if ($id) {
 129      $strheading = get_string('edituserkey', 'userkey');
 130  } else {
 131      $strheading = get_string('createuserkey', 'userkey');
 132  }
 133  
 134  $PAGE->navbar->add($strheading);
 135  
 136  /// Print header
 137  $PAGE->set_title($strkeys);
 138  $PAGE->set_heading($course->fullname);
 139  $PAGE->set_secondary_active_tab('grades');
 140  echo $OUTPUT->header();
 141  echo $OUTPUT->heading($strheading);
 142  
 143  $editform->display();
 144  echo $OUTPUT->footer();
 145