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   * Import key management.
  20   *
  21   * @package   moodlecore
  22   * @copyright 2008 Nicolas Connault
  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);
  33  $delete   = optional_param('delete', 0, PARAM_BOOL);
  34  $confirm  = optional_param('confirm', 0, PARAM_BOOL);
  35  
  36  $url = new moodle_url('/grade/import/key.php', ['courseid' => $courseid, 'id' => $id]);
  37  $PAGE->set_url($url);
  38  
  39  if ($id) {
  40      if (!$key = $DB->get_record('user_private_key', array('id' => $id))) {
  41          throw new \moodle_exception('invalidgroupid');
  42      }
  43      if (empty($courseid)) {
  44          $courseid = $key->instance;
  45  
  46      } else if ($courseid != $key->instance) {
  47          throw new \moodle_exception('invalidcourseid');
  48      }
  49  
  50      if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  51          throw new \moodle_exception('invalidcourseid');
  52      }
  53  
  54  } else {
  55      if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  56          throw new \moodle_exception('invalidcourseid');
  57      }
  58      $key = new stdClass();
  59  }
  60  
  61  $key->courseid = $course->id;
  62  
  63  require_login($course);
  64  $context = context_course::instance($course->id);
  65  require_capability('moodle/grade:import', $context);
  66  
  67  // Check if the user has at least one grade publishing capability.
  68  $plugins = grade_helper::get_plugins_import($course->id);
  69  if (!isset($plugins['keymanager'])) {
  70      throw new \moodle_exception('nopermissions');
  71  }
  72  
  73  // extra security check
  74  if (!empty($key->userid) and $USER->id != $key->userid) {
  75      throw new \moodle_exception('notownerofkey');
  76  }
  77  
  78  $returnurl = $CFG->wwwroot.'/grade/import/keymanager.php?id='.$course->id;
  79  
  80  $strkeys   = get_string('keymanager', 'userkey');
  81  $strimportgrades = get_string('import', 'grades');
  82  $PAGE->navbar->add($strimportgrades, new moodle_url(new moodle_url('/grade/import/index.php', ['id' => $courseid])));
  83  $PAGE->navbar->add($strkeys, new moodle_url('/grade/import/keymanager.php', ['id' => $courseid]));
  84  
  85  if ($id and $delete) {
  86      if (!$confirm) {
  87          $PAGE->set_title(get_string('deleteselectedkey'));
  88          $PAGE->set_heading($course->fullname);
  89          $PAGE->set_secondary_active_tab('grades');
  90          $PAGE->navbar->add(get_string('deleteuserkey', 'userkey'));
  91  
  92          echo $OUTPUT->header();
  93          $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
  94          $optionsno  = array('id'=>$courseid);
  95          $formcontinue = new single_button(new moodle_url('key.php', $optionsyes), get_string('yes'), 'get');
  96          $formcancel = new single_button(new moodle_url('keymanager.php', $optionsno), get_string('no'), 'get');
  97          echo $OUTPUT->confirm(get_string('deletekeyconfirm', 'userkey', $key->value), $formcontinue, $formcancel);
  98          echo $OUTPUT->footer();
  99          die;
 100  
 101      } else if (confirm_sesskey()){
 102          $DB->delete_records('user_private_key', array('id' => $id));
 103          redirect('keymanager.php?id='.$course->id);
 104      }
 105  }
 106  
 107  /// First create the form
 108  $editform = new key_form();
 109  $editform->set_data($key);
 110  
 111  if ($editform->is_cancelled()) {
 112      redirect($returnurl);
 113  
 114  } elseif ($data = $editform->get_data()) {
 115  
 116      if ($data->id) {
 117          $record = new stdClass();
 118          $record->id            = $data->id;
 119          $record->iprestriction = $data->iprestriction;
 120          $record->validuntil    = $data->validuntil;
 121          $DB->update_record('user_private_key', $record);
 122      } else {
 123          create_user_key('grade/import', $USER->id, $course->id, $data->iprestriction, $data->validuntil);
 124      }
 125  
 126      redirect($returnurl);
 127  }
 128  
 129  if ($id) {
 130      $strheading = get_string('edituserkey', 'userkey');
 131  } else {
 132      $strheading = get_string('createuserkey', 'userkey');
 133  }
 134  
 135  $PAGE->navbar->add($strheading);
 136  
 137  /// Print header
 138  $PAGE->set_title($strkeys);
 139  $PAGE->set_heading($course->fullname);
 140  $PAGE->set_secondary_active_tab('grades');
 141  echo $OUTPUT->header();
 142  echo $OUTPUT->heading($strheading);
 143  
 144  $editform->display();
 145  echo $OUTPUT->footer();