[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/grade/edit/letter/ -> index.php (source)

   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      require_login();
  52  
  53      admin_externalpage_setup('letters');
  54  
  55      $admin = true;
  56      $returnurl = "$CFG->wwwroot/grade/edit/letter/index.php";
  57      $editparam = '?edit=1';
  58  } else if ($context->contextlevel == CONTEXT_COURSE) {
  59  
  60      $PAGE->set_pagelayout('standard');//calling this here to make blocks display
  61  
  62      require_login($context->instanceid, false, $cm);
  63  
  64      $admin = false;
  65      $returnurl = $CFG->wwwroot.'/grade/edit/letter/index.php?id='.$context->id;
  66      $editparam = '&edit=1';
  67  
  68      $gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'letter', 'courseid'=>$course->id));
  69  } else {
  70      print_error('invalidcourselevel');
  71  }
  72  
  73  $strgrades = get_string('grades');
  74  $pagename  = get_string('letters', 'grades');
  75  
  76  $letters = grade_get_letters($context);
  77  $num = count($letters) + 3;
  78  
  79  $override = $DB->record_exists('grade_letters', array('contextid' => $context->id));
  80  
  81  //if were viewing the letters
  82  if (!$edit) {
  83  
  84      $data = array();
  85  
  86      $max = 100;
  87      foreach($letters as $boundary=>$letter) {
  88          $line = array();
  89          $line[] = format_float($max,2).' %';
  90          $line[] = format_float($boundary,2).' %';
  91          $line[] = format_string($letter);
  92          $data[] = $line;
  93          $max = $boundary - 0.01;
  94      }
  95  
  96      print_grade_page_head($COURSE->id, 'letter', 'view', get_string('gradeletters', 'grades'));
  97  
  98      if (!empty($override)) {
  99          echo $OUTPUT->notification(get_string('gradeletteroverridden', 'grades'), 'notifymessage');
 100      }
 101  
 102      $stredit = get_string('editgradeletters', 'grades');
 103      $editlink = html_writer::nonempty_tag('div', html_writer::link($returnurl.$editparam, $stredit), array('class'=>'mdl-align'));
 104      echo $editlink;
 105  
 106      $table = new html_table();
 107      $table->id = 'grade-letters-view';
 108      $table->head  = array(get_string('max', 'grades'), get_string('min', 'grades'), get_string('letter', 'grades'));
 109      $table->size  = array('30%', '30%', '40%');
 110      $table->align = array('left', 'left', 'left');
 111      $table->width = '30%';
 112      $table->data  = $data;
 113      $table->tablealign  = 'center';
 114      echo html_writer::table($table);
 115  
 116      echo $editlink;
 117  } else { //else we're editing
 118      require_once ('edit_form.php');
 119  
 120      $data = new stdClass();
 121      $data->id = $context->id;
 122  
 123      $i = 1;
 124      foreach ($letters as $boundary=>$letter) {
 125          $gradelettername = 'gradeletter'.$i;
 126          $gradeboundaryname = 'gradeboundary'.$i;
 127  
 128          $data->$gradelettername   = $letter;
 129          $data->$gradeboundaryname = $boundary;
 130          $i++;
 131      }
 132      $data->override = $override;
 133  
 134      $mform = new edit_letter_form($returnurl.$editparam, array('num'=>$num, 'admin'=>$admin));
 135      $mform->set_data($data);
 136  
 137      if ($mform->is_cancelled()) {
 138          redirect($returnurl);
 139  
 140      } else if ($data = $mform->get_data()) {
 141          if (!$admin and empty($data->override)) {
 142              $DB->delete_records('grade_letters', array('contextid' => $context->id));
 143              redirect($returnurl);
 144          }
 145  
 146          $letters = array();
 147          for ($i=1; $i < $num+1; $i++) {
 148              $gradelettername = 'gradeletter'.$i;
 149              $gradeboundaryname = 'gradeboundary'.$i;
 150  
 151              if (property_exists($data, $gradeboundaryname) and $data->$gradeboundaryname != -1) {
 152                  $letter = trim($data->$gradelettername);
 153                  if ($letter == '') {
 154                      continue;
 155                  }
 156  
 157                  $boundary = floatval($data->$gradeboundaryname);
 158                  if ($boundary < 0 || $boundary > 100) {
 159                      continue;    // Skip if out of range.
 160                  }
 161  
 162                  // The keys need to be strings so floats are not truncated.
 163                  $letters[number_format($boundary, 5)] = $letter;
 164              }
 165          }
 166  
 167          $pool = array();
 168          if ($records = $DB->get_records('grade_letters', array('contextid' => $context->id), 'lowerboundary ASC')) {
 169              foreach ($records as $r) {
 170                  // Will re-use the lowerboundary to avoid duplicate during the update process.
 171                  $pool[number_format($r->lowerboundary, 5)] = $r;
 172              }
 173          }
 174  
 175          foreach ($letters as $boundary => $letter) {
 176              $record = new stdClass();
 177              $record->letter        = $letter;
 178              $record->lowerboundary = $boundary;
 179              $record->contextid     = $context->id;
 180  
 181              if (isset($pool[$boundary])) {
 182                  // Re-use the existing boundary to avoid key constraint.
 183                  if ($letter != $pool[$boundary]->letter) {
 184                      // The letter has been assigned to another boundary, we update it.
 185                      $record->id = $pool[$boundary]->id;
 186                      $DB->update_record('grade_letters', $record);
 187                  }
 188                  unset($pool[$boundary]);    // Remove the letter from the pool.
 189              } else if ($candidate = array_pop($pool)) {
 190                  // The boundary is new, we update a random record from the pool.
 191                  $record->id = $candidate->id;
 192                  $DB->update_record('grade_letters', $record);
 193              } else {
 194                  // No records were found, this must be a new letter.
 195                  $DB->insert_record('grade_letters', $record);
 196              }
 197          }
 198  
 199          // Delete the unused records.
 200          foreach($pool as $leftover) {
 201              $DB->delete_records('grade_letters', array('id' => $leftover->id));
 202          }
 203  
 204          redirect($returnurl);
 205      }
 206  
 207      print_grade_page_head($COURSE->id, 'letter', 'edit', get_string('editgradeletters', 'grades'));
 208  
 209      $mform->display();
 210  }
 211  
 212  echo $OUTPUT->footer();


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1