PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/quiz/settingslib.php

http://github.com/moodle/moodle
PHP | 197 lines | 110 code | 26 blank | 61 comment | 18 complexity | 95a8acb1618d88a16ed52ecfc4c805ed MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * This page is the entry page into the quiz UI. Displays information about the
  18. * quiz to students and teachers, and lets students see their previous attempts.
  19. *
  20. * @package mod
  21. * @subpackage quiz
  22. * @copyright 2008 Tim Hunt
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. /**
  27. * Admin settings class for the quiz review opitions.
  28. *
  29. * @copyright 2008 Tim Hunt
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class mod_quiz_admin_review_setting extends admin_setting {
  33. /**#@+
  34. * @var integer should match the constants defined in {@link mod_quiz_display_options}.
  35. * again, copied for performance reasons.
  36. */
  37. const DURING = 0x10000;
  38. const IMMEDIATELY_AFTER = 0x01000;
  39. const LATER_WHILE_OPEN = 0x00100;
  40. const AFTER_CLOSE = 0x00010;
  41. /**#@-*/
  42. /**
  43. * @var boolean|null forced checked / disabled attributes for the during time.
  44. */
  45. protected $duringstate;
  46. /**
  47. * This should match {@link mod_quiz_mod_form::$reviewfields} but copied
  48. * here because generating the admin tree needs to be fast.
  49. * @return array
  50. */
  51. public static function fields() {
  52. return array(
  53. 'attempt' => get_string('theattempt', 'quiz'),
  54. 'correctness' => get_string('whethercorrect', 'question'),
  55. 'marks' => get_string('marks', 'question'),
  56. 'specificfeedback' => get_string('specificfeedback', 'question'),
  57. 'generalfeedback' => get_string('generalfeedback', 'question'),
  58. 'rightanswer' => get_string('rightanswer', 'question'),
  59. 'overallfeedback' => get_string('overallfeedback', 'quiz'),
  60. );
  61. }
  62. public function __construct($name, $visiblename, $description,
  63. $defaultsetting, $duringstate = null) {
  64. $this->duringstate = $duringstate;
  65. parent::__construct($name, $visiblename, $description, $defaultsetting);
  66. }
  67. /**
  68. * @return int all times.
  69. */
  70. public static function all_on() {
  71. return self::DURING | self::IMMEDIATELY_AFTER | self::LATER_WHILE_OPEN |
  72. self::AFTER_CLOSE;
  73. }
  74. protected static function times() {
  75. return array(
  76. self::DURING => get_string('reviewduring', 'quiz'),
  77. self::IMMEDIATELY_AFTER => get_string('reviewimmediately', 'quiz'),
  78. self::LATER_WHILE_OPEN => get_string('reviewopen', 'quiz'),
  79. self::AFTER_CLOSE => get_string('reviewclosed', 'quiz'),
  80. );
  81. }
  82. protected function normalise_data($data) {
  83. $times = self::times();
  84. $value = 0;
  85. foreach ($times as $timemask => $name) {
  86. if ($timemask == self::DURING && !is_null($this->duringstate)) {
  87. if ($this->duringstate) {
  88. $value += $timemask;
  89. }
  90. } else if (!empty($data[$timemask])) {
  91. $value += $timemask;
  92. }
  93. }
  94. return $value;
  95. }
  96. public function get_setting() {
  97. return $this->config_read($this->name);
  98. }
  99. public function write_setting($data) {
  100. if (is_array($data) || empty($data)) {
  101. $data = $this->normalise_data($data);
  102. }
  103. $this->config_write($this->name, $data);
  104. return '';
  105. }
  106. public function output_html($data, $query = '') {
  107. if (is_array($data) || empty($data)) {
  108. $data = $this->normalise_data($data);
  109. }
  110. $return = '<div class="group"><input type="hidden" name="' .
  111. $this->get_full_name() . '[' . self::DURING . ']" value="0" />';
  112. foreach (self::times() as $timemask => $namestring) {
  113. $id = $this->get_id(). '_' . $timemask;
  114. $state = '';
  115. if ($data & $timemask) {
  116. $state = 'checked="checked" ';
  117. }
  118. if ($timemask == self::DURING && !is_null($this->duringstate)) {
  119. $state = 'disabled="disabled" ';
  120. if ($this->duringstate) {
  121. $state .= 'checked="checked" ';
  122. }
  123. }
  124. $return .= '<span><input type="checkbox" name="' .
  125. $this->get_full_name() . '[' . $timemask . ']" value="1" id="' . $id .
  126. '" ' . $state . '/> <label for="' . $id . '">' .
  127. $namestring . "</label></span>\n";
  128. }
  129. $return .= "</div>\n";
  130. return format_admin_setting($this, $this->visiblename, $return,
  131. $this->description, true, '', get_string('everythingon', 'quiz'), $query);
  132. }
  133. }
  134. /**
  135. * Admin settings class for the quiz grading method.
  136. *
  137. * Just so we can lazy-load the choices.
  138. *
  139. * @copyright 2011 The Open University
  140. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  141. */
  142. class mod_quiz_admin_setting_grademethod extends admin_setting_configselect_with_advanced {
  143. public function load_choices() {
  144. global $CFG;
  145. if (is_array($this->choices)) {
  146. return true;
  147. }
  148. require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  149. $this->choices = quiz_get_grading_options();
  150. return true;
  151. }
  152. }
  153. /**
  154. * Admin settings class for the quiz browser security option.
  155. *
  156. * Just so we can lazy-load the choices.
  157. *
  158. * @copyright 2011 The Open University
  159. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  160. */
  161. class mod_quiz_admin_setting_browsersecurity extends admin_setting_configselect_with_advanced {
  162. public function load_choices() {
  163. global $CFG;
  164. if (is_array($this->choices)) {
  165. return true;
  166. }
  167. require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  168. $this->choices = quiz_access_manager::get_browser_security_choices();
  169. return true;
  170. }
  171. }