Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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.

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