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.

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   * List of grade letters.
  19   *
  20   * @package   core_grades
  21   * @copyright 2008 Nicolas Connault
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once '../../../config.php';
  26  require_once $CFG->dirroot.'/grade/lib.php';
  27  require_once $CFG->libdir.'/gradelib.php';
  28  
  29  $contextid = optional_param('id', SYSCONTEXTID, PARAM_INT);
  30  $action   = optional_param('action', '', PARAM_ALPHA);
  31  $edit     = optional_param('edit', false, PARAM_BOOL); //are we editing?
  32  
  33  $url = new moodle_url('/grade/edit/letter/index.php', array('id' => $contextid));
  34  
  35  list($context, $course, $cm) = get_context_info_array($contextid);
  36  $contextid = null;//now we have a context object throw away the $contextid from the params
  37  
  38  //if viewing
  39  if (!$edit) {
  40      if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:manageletters', $context)) {
  41          throw new \moodle_exception('nopermissiontoviewletergrade');
  42      }
  43  } else {//else we're editing
  44      require_capability('moodle/grade:manageletters', $context);
  45      navigation_node::override_active_url($url);
  46      $url->param('edit', 1);
  47      $PAGE->navbar->add(get_string('editgradeletters', 'grades'), $url);
  48  }
  49  $PAGE->set_url($url);
  50  
  51  $returnurl = null;
  52  $editparam = null;
  53  if ($context->contextlevel == CONTEXT_SYSTEM or $context->contextlevel == CONTEXT_COURSECAT) {
  54      require_once $CFG->libdir.'/adminlib.php';
  55  
  56      admin_externalpage_setup('letters');
  57  
  58      $admin = true;
  59      $returnurl = "$CFG->wwwroot/grade/edit/letter/index.php";
  60      $editparam = '?edit=1';
  61      $PAGE->set_primary_active_tab('siteadminnode');
  62  } else if ($context->contextlevel == CONTEXT_COURSE) {
  63  
  64      $PAGE->set_pagelayout('standard');//calling this here to make blocks display
  65  
  66      require_login($context->instanceid, false, $cm);
  67  
  68      $admin = false;
  69      $returnurl = $CFG->wwwroot.'/grade/edit/letter/index.php?id='.$context->id;
  70      $editparam = '&edit=1';
  71  
  72      $gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'letter', 'courseid'=>$course->id));
  73  } else {
  74      throw new \moodle_exception('invalidcourselevel');
  75  }
  76  
  77  $strgrades = get_string('grades');
  78  $pagename  = get_string('letters', 'grades');
  79  
  80  $letters = grade_get_letters($context);
  81  
  82  $override = $DB->record_exists('grade_letters', array('contextid' => $context->id));
  83  
  84  //if were viewing the letters
  85  if (!$edit) {
  86      $heading = get_string('gradeletters', 'grades');
  87      $actionbar = new \core_grades\output\grade_letters_action_bar($context);
  88  
  89      if ($admin) {
  90          echo $OUTPUT->header();
  91          $renderer = $PAGE->get_renderer('core_grades');
  92          echo $renderer->render_action_bar($actionbar);
  93          echo $OUTPUT->heading($heading);
  94      } else {
  95          print_grade_page_head($course->id, 'letter', 'view', false, false, false,
  96              true, null, null, null, $actionbar);
  97      }
  98  
  99      $data = array();
 100  
 101      $max = 100;
 102      foreach($letters as $boundary=>$letter) {
 103          $line = array();
 104          $line[] = format_float($max,2).' %';
 105          $line[] = format_float($boundary,2).' %';
 106          $line[] = format_string($letter);
 107          $data[] = $line;
 108          $max = $boundary - 0.01;
 109      }
 110  
 111      if (!empty($override)) {
 112          echo $OUTPUT->notification(get_string('gradeletteroverridden', 'grades'), 'notifymessage');
 113      }
 114  
 115      $table = new html_table();
 116      $table->id = 'grade-letters-view';
 117      $table->head  = array(get_string('max', 'grades'), get_string('min', 'grades'), get_string('letter', 'grades'));
 118      $table->size  = array('30%', '30%', '40%');
 119      $table->align = array('left', 'left', 'left');
 120      $table->width = '30%';
 121      $table->data  = $data;
 122      $table->tablealign  = 'center';
 123      echo html_writer::table($table);
 124  
 125  } else { //else we're editing
 126      require_once ('edit_form.php');
 127  
 128      $data = new stdClass();
 129      $data->id = $context->id;
 130  
 131      $i = 0;
 132      foreach ($letters as $boundary=>$letter) {
 133          $data->gradeletter[$i] = $letter;
 134          $data->gradeboundary[$i] = $boundary;
 135          $i++;
 136      }
 137      $data->override = $override;
 138  
 139      // Count number of letters, used to build the repeated elements of the form.
 140      $lettercount = count($letters);
 141  
 142      $mform = new edit_letter_form($returnurl.$editparam, ['lettercount' => $lettercount, 'admin' => $admin]);
 143      $mform->set_data($data);
 144  
 145      if ($mform->is_cancelled()) {
 146          redirect($returnurl);
 147  
 148      } else if ($data = $mform->get_data()) {
 149  
 150          // Make sure we are updating the cache.
 151          $cache = cache::make('core', 'grade_letters');
 152  
 153          if (!$admin and empty($data->override)) {
 154              $records = $DB->get_records('grade_letters', array('contextid' => $context->id));
 155              foreach ($records as $record) {
 156                  $DB->delete_records('grade_letters', array('id' => $record->id));
 157                  // Trigger the letter grade deleted event.
 158                  $event = \core\event\grade_letter_deleted::create(array(
 159                      'objectid' => $record->id,
 160                      'context' => $context,
 161                  ));
 162                  $event->trigger();
 163              }
 164  
 165              // Make sure we clear the cache for this context.
 166              $cache->delete($context->id);
 167              redirect($returnurl);
 168          }
 169  
 170          $letters = array();
 171          for ($i = 0; $i < $data->gradeentrycount; $i++) {
 172              $letter = $data->gradeletter[$i];
 173              if ($letter === '') {
 174                  continue;
 175              }
 176  
 177              $boundary = floatval($data->gradeboundary[$i]);
 178              if ($boundary < 0 || $boundary > 100) {
 179                  continue;    // Skip if out of range.
 180              }
 181  
 182              // The keys need to be strings so floats are not truncated.
 183              $letters[number_format($boundary, 5)] = $letter;
 184          }
 185  
 186          $pool = array();
 187          if ($records = $DB->get_records('grade_letters', array('contextid' => $context->id), 'lowerboundary ASC')) {
 188              foreach ($records as $r) {
 189                  // Will re-use the lowerboundary to avoid duplicate during the update process.
 190                  $pool[number_format($r->lowerboundary, 5)] = $r;
 191              }
 192          }
 193  
 194          foreach ($letters as $boundary => $letter) {
 195              $record = new stdClass();
 196              $record->letter        = $letter;
 197              $record->lowerboundary = $boundary;
 198              $record->contextid     = $context->id;
 199  
 200              if (isset($pool[$boundary])) {
 201                  // Re-use the existing boundary to avoid key constraint.
 202                  if ($letter != $pool[$boundary]->letter) {
 203                      // The letter has been assigned to another boundary, we update it.
 204                      $record->id = $pool[$boundary]->id;
 205                      $DB->update_record('grade_letters', $record);
 206                      // Trigger the letter grade updated event.
 207                      $event = \core\event\grade_letter_updated::create(array(
 208                          'objectid' => $record->id,
 209                          'context' => $context,
 210                      ));
 211                      $event->trigger();
 212                  }
 213                  unset($pool[$boundary]);    // Remove the letter from the pool.
 214              } else if ($candidate = array_pop($pool)) {
 215                  // The boundary is new, we update a random record from the pool.
 216                  $record->id = $candidate->id;
 217                  $DB->update_record('grade_letters', $record);
 218                  // Trigger the letter grade updated event.
 219                  $event = \core\event\grade_letter_updated::create(array(
 220                      'objectid' => $record->id,
 221                      'context' => $context,
 222                  ));
 223                  $event->trigger();
 224              } else {
 225                  // No records were found, this must be a new letter.
 226                  $newid = $DB->insert_record('grade_letters', $record);
 227                  // Trigger the letter grade added event.
 228                  $event = \core\event\grade_letter_created::create(array(
 229                      'objectid' => $newid,
 230                      'context' => $context,
 231                  ));
 232                  $event->trigger();
 233              }
 234          }
 235  
 236          // Cache the changed letters.
 237          if (!empty($letters)) {
 238  
 239              // For some reason, the cache saves it in the order in which they were entered
 240              // but we really want to order them in descending order so we sort it here.
 241              krsort($letters);
 242              $cache->set($context->id, $letters);
 243          }
 244  
 245          // Delete the unused records.
 246          foreach($pool as $leftover) {
 247              $DB->delete_records('grade_letters', array('id' => $leftover->id));
 248              // Trigger the letter grade deleted event.
 249              $event = \core\event\grade_letter_deleted::create(array(
 250                  'objectid' => $leftover->id,
 251                  'context' => $context,
 252              ));
 253              $event->trigger();
 254          }
 255  
 256          redirect($returnurl);
 257      }
 258  
 259      print_grade_page_head($COURSE->id, 'letter', 'edit', get_string('editgradeletters', 'grades'),
 260          false, false, false);
 261  
 262      $mform->display();
 263  }
 264  
 265  echo $OUTPUT->footer();