[ Index ]

PHP Cross Reference of moodle-2.8

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


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1