Search moodle.org's
Developer Documentation

   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   * Rest endpoint for ajax editing of quiz structure.
  19   *
  20   * @package   mod_quiz
  21   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  if (!defined('AJAX_SCRIPT')) {
  26      define('AJAX_SCRIPT', true);
  27  }
  28  
  29  require_once(__DIR__ . '/../../config.php');
  30  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  31  
  32  // Initialise ALL the incoming parameters here, up front.
  33  $quizid     = required_param('quizid', PARAM_INT);
  34  $class      = required_param('class', PARAM_ALPHA);
  35  $field      = optional_param('field', '', PARAM_ALPHA);
  36  $instanceid = optional_param('instanceId', 0, PARAM_INT);
  37  $sectionid  = optional_param('sectionId', 0, PARAM_INT);
  38  $previousid = optional_param('previousid', 0, PARAM_INT);
  39  $value      = optional_param('value', 0, PARAM_INT);
  40  $column     = optional_param('column', 0, PARAM_ALPHA);
  41  $id         = optional_param('id', 0, PARAM_INT);
  42  $summary    = optional_param('summary', '', PARAM_RAW);
  43  $sequence   = optional_param('sequence', '', PARAM_SEQUENCE);
  44  $visible    = optional_param('visible', 0, PARAM_INT);
  45  $pageaction = optional_param('action', '', PARAM_ALPHA); // Used to simulate a DELETE command.
  46  $maxmark    = optional_param('maxmark', '', PARAM_FLOAT);
  47  $page       = optional_param('page', '', PARAM_INT);
  48  $PAGE->set_url('/mod/quiz/edit-rest.php',
  49          array('quizid' => $quizid, 'class' => $class));
  50  
  51  require_sesskey();
  52  $quiz = $DB->get_record('quiz', array('id' => $quizid), '*', MUST_EXIST);
  53  $cm = get_coursemodule_from_instance('quiz', $quiz->id, $quiz->course);
  54  $course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST);
  55  require_login($course, false, $cm);
  56  
  57  $quizobj = new quiz($quiz, $cm, $course);
  58  $structure = $quizobj->get_structure();
  59  $modcontext = context_module::instance($cm->id);
  60  
  61  echo $OUTPUT->header(); // Send headers.
  62  
  63  // OK, now let's process the parameters and do stuff
  64  // MDL-10221 the DELETE method is not allowed on some web servers,
  65  // so we simulate it with the action URL param.
  66  $requestmethod = $_SERVER['REQUEST_METHOD'];
  67  if ($pageaction == 'DELETE') {
  68      $requestmethod = 'DELETE';
  69  }
  70  
  71  switch($requestmethod) {
  72      case 'POST':
  73      case 'GET': // For debugging.
  74  
  75          switch ($class) {
  76              case 'section':
  77                  break;
  78  
  79              case 'resource':
  80                  switch ($field) {
  81                      case 'move':
  82                          require_capability('mod/quiz:manage', $modcontext);
  83                          $structure->move_slot($id, $previousid, $page);
  84                          quiz_delete_previews($quiz);
  85                          echo json_encode(array('visible' => true));
  86                          break;
  87  
  88                      case 'getmaxmark':
  89                          require_capability('mod/quiz:manage', $modcontext);
  90                          $slot = $DB->get_record('quiz_slots', array('id' => $id), '*', MUST_EXIST);
  91                          echo json_encode(array('instancemaxmark' =>
  92                                  quiz_format_question_grade($quiz, $slot->maxmark)));
  93                          break;
  94  
  95                      case 'updatemaxmark':
  96                          require_capability('mod/quiz:manage', $modcontext);
  97                          $slot = $structure->get_slot_by_id($id);
  98                          if ($structure->update_slot_maxmark($slot, $maxmark)) {
  99                              // Grade has really changed.
 100                              quiz_delete_previews($quiz);
 101                              quiz_update_sumgrades($quiz);
 102                              quiz_update_all_attempt_sumgrades($quiz);
 103                              quiz_update_all_final_grades($quiz);
 104                              quiz_update_grades($quiz, 0, true);
 105                          }
 106                          echo json_encode(array('instancemaxmark' => quiz_format_question_grade($quiz, $maxmark),
 107                                  'newsummarks' => quiz_format_grade($quiz, $quiz->sumgrades)));
 108                          break;
 109                      case 'updatepagebreak':
 110                          require_capability('mod/quiz:manage', $modcontext);
 111                          $slots = $structure->update_page_break($quiz, $id, $value);
 112                          $json = array();
 113                          foreach ($slots as $slot) {
 114                              $json[$slot->slot] = array('id' => $slot->id, 'slot' => $slot->slot,
 115                                                              'page' => $slot->page);
 116                          }
 117                          echo json_encode(array('slots' => $json));
 118                          break;
 119                  }
 120                  break;
 121  
 122              case 'course':
 123                  break;
 124          }
 125          break;
 126  
 127      case 'DELETE':
 128          switch ($class) {
 129              case 'resource':
 130                  require_capability('mod/quiz:manage', $modcontext);
 131                  if (!$slot = $DB->get_record('quiz_slots', array('quizid' => $quiz->id, 'id' => $id))) {
 132                      throw new moodle_exception('AJAX commands.php: Bad slot ID '.$id);
 133                  }
 134                  $structure->remove_slot($quiz, $slot->slot);
 135                  quiz_delete_previews($quiz);
 136                  quiz_update_sumgrades($quiz);
 137                  echo json_encode(array('newsummarks' => quiz_format_grade($quiz, $quiz->sumgrades),
 138                              'deleted' => true, 'newnumquestions' => $structure->get_question_count()));
 139                  break;
 140          }
 141          break;
 142  }

Search This Site: