diff --git a/mod/quiz/report/attemptsreport.php b/mod/quiz/report/attemptsreport.php index a976e529425..bd31bc0629a 100644 --- a/mod/quiz/report/attemptsreport.php +++ b/mod/quiz/report/attemptsreport.php @@ -98,31 +98,6 @@ abstract class quiz_attempt_report extends quiz_default_report { array('id' => $this->context->instanceid, 'mode' => $this->mode)); } - /** - * Should the grades be displayed in this report. That depends on the quiz - * display options, and whether the quiz is graded. - * @param object $quiz the quiz settings. - * @return bool - */ - protected function should_show_grades($quiz) { - if (!is_null($this->showgrades)) { - return $this->showgrades; - } - - if ($quiz->timeclose && time() > $quiz->timeclose) { - $when = mod_quiz_display_options::AFTER_CLOSE; - } else { - $when = mod_quiz_display_options::LATER_WHILE_OPEN; - } - $reviewoptions = mod_quiz_display_options::make_from_quiz($quiz, $when); - - $this->showgrades = quiz_has_grades($quiz) && - ($reviewoptions->marks >= question_display_options::MARK_AND_MAX || - has_capability('moodle/grade:viewhidden', $this->context)); - - return $this->showgrades; - } - /** * Get information about which students to show in the report. * @param object $cm the coures module. @@ -165,33 +140,6 @@ abstract class quiz_attempt_report extends quiz_default_report { return array($currentgroup, $students, $groupstudents, $groupstudents); } - /** - * Alters $attemptsmode and $pagesize if the current values are inappropriate. - * @param int $attemptsmode what sort of attempts to display (may be updated) - * @param int $pagesize number of records to display per page (may be updated) - * @param object $course the course settings. - * @param int $currentgroup the currently selected group. 0 for none. - */ - protected function validate_common_options(&$attemptsmode, &$pagesize, $course, $currentgroup) { - if ($currentgroup) { - // Default for when a group is selected. - if ($attemptsmode === null || $attemptsmode == self::ALL_ATTEMPTS) { - $attemptsmode = self::STUDENTS_WITH; - } - - } else if (!$currentgroup && $course->id == SITEID) { - // Force report on front page to show all, unless a group is selected. - $attemptsmode = self::ALL_ATTEMPTS; - - } else if ($attemptsmode === null) { - $attemptsmode = self::ALL_ATTEMPTS; - } - - if ($pagesize < 1) { - $pagesize = self::DEFAULT_PAGE_SIZE; - } - } - /** * Add all the user-related columns to the $columns and $headers arrays. * @param table_sql $table the table being constructed. @@ -275,7 +223,7 @@ abstract class quiz_attempt_report extends quiz_default_report { * @param bool $includefeedback whether to include the feedbacktext columns */ protected function add_grade_columns($quiz, &$columns, &$headers, $includefeedback = true) { - if ($this->should_show_grades($quiz)) { + if (quiz_report_should_show_grades($quiz)) { $columns[] = 'sumgrades'; $headers[] = get_string('grade', 'quiz') . '/' . quiz_format_grade($quiz, $quiz->grade); diff --git a/mod/quiz/report/attemptsreport_options.php b/mod/quiz/report/attemptsreport_options.php new file mode 100644 index 00000000000..12dea7e5681 --- /dev/null +++ b/mod/quiz/report/attemptsreport_options.php @@ -0,0 +1,190 @@ +. + +/** + * Base class for the options that control what is visible in an {@link quiz_attempt_report}. + * + * @package mod_quiz + * @copyright 2012 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->libdir . '/formslib.php'); + + +/** + * Base class for the options that control what is visible in an {@link quiz_attempt_report}. + * + * @copyright 2012 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_quiz_attempt_report_options { + + /** @var object the settings for the quiz being reported on. */ + public $quiz; + + /** @var object the course module objects for the quiz being reported on. */ + public $cm; + + /** @var object the course settings for the course the quiz is in. */ + public $course; + + /** + * @var string quiz_attempt_report:: constants. + */ + public $attempts = quiz_attempt_report::STUDENTS_WITH; + + /** + * @var int the currently selected group. 0 if no group is selected. + */ + public $group = 0; + + /** + * @var bool wether to show all finished attmepts, or just the one that gave + * the final grade for the user. + */ + public $onlygraded = false; + + /** + * @var int Number of attempts to show per page. + */ + public $pagesize = quiz_attempt_report::DEFAULT_PAGE_SIZE; + + /** + * @var string whether the data should be downloaded in some format, or '' to display it. + */ + public $download = ''; + + /** + * Constructor. + * @param object $quiz the settings for the quiz being reported on. + * @param object $cm the course module objects for the quiz being reported on. + */ + public function __construct($quiz, $cm, $course) { + $this->quiz = $quiz; + $this->cm = $cm; + $this->course = $course; + } + + /** + * Process the data we get when the settings form is submitted. This includes + * updating the fields of this class, and updating the user preferences + * where appropriate. + * @param object $fromform The data from $mform->get_data() from the settings form. + */ + public function process_settings_from_form($fromform) { + $this->setup_from_form_data($fromform); + $this->resolve_dependencies(); + $this->update_user_preferences(); + } + + /** + * Set up this preferences object using optional_param (using user_preferences + * to set anything not specified by the params. + */ + public function process_settings_from_params() { + $this->setup_from_user_preferences(); + $this->setup_from_params(); + $this->resolve_dependencies(); + } + + /** + * Get the current value of the settings to pass to the settings form. + */ + public function get_initial_form_data() { + $toform = new stdClass(); + $toform->attemptsmode = $this->attempts; + $toform->qmfilter = $this->onlygraded; + $toform->pagesize = $this->pagesize; + + return $toform; + } + + /** + * Set the fields of this object from the form data. + * @param object $fromform The data from $mform->get_data() from the settings form. + */ + public function setup_from_form_data($fromform) { + $this->attempts = $fromform->attemptsmode; + $this->group = groups_get_activity_group($this->cm, true); + $this->onlygraded = !empty($fromform->qmfilter); + $this->pagesize = $fromform->pagesize; + } + + /** + * Set the fields of this object from the user's preferences. + */ + public function setup_from_params() { + $this->attempts = optional_param('attemptsmode', $this->attempts, PARAM_ALPHAEXT); + $this->group = groups_get_activity_group($this->cm, true); + $this->onlygraded = optional_param('qmfilter', $this->onlygraded, PARAM_BOOL); + $this->pagesize = optional_param('pagesize', $this->pagesize, PARAM_INT); + + $this->download = optional_param('download', $this->download, PARAM_ALPHA); + } + + /** + * Set the fields of this object from the user's preferences. + * (For those settings that are backed by user-preferences). + */ + public function setup_from_user_preferences() { + $this->pagesize = get_user_preferences('quiz_report_pagesize', $this->pagesize); + } + + /** + * Update the user preferences so they match the settings in this object. + * (For those settings that are backed by user-preferences). + */ + public function update_user_preferences() { + set_user_preference('quiz_report_pagesize', $this->pagesize); + } + + /** + * Check the settings, and remove any 'impossible' combinations. + */ + public function resolve_dependencies() { + if ($this->group) { + // Default for when a group is selected. + if ($this->attempts === null || $this->attempts == quiz_attempt_report::ALL_ATTEMPTS) { + $this->attempts = quiz_attempt_report::STUDENTS_WITH; + } + + } else if (!$this->group && $this->course->id == SITEID) { + // Force report on front page to show all, unless a group is selected. + $this->attempts = quiz_attempt_report::ALL_ATTEMPTS; + + } else if ($this->attempts === null) { + $this->attempts = quiz_attempt_report::ALL_ATTEMPTS; + } + + if ($this->pagesize < 1) { + $this->pagesize = quiz_attempt_report::DEFAULT_PAGE_SIZE; + } + + if (!quiz_report_qm_filter_select($this->quiz)) { + // A grading mode like 'average' has been selected, so we cannot do + // the show the attempt that gave the final grade thing. + $this->onlygraded = false; + } + + if ($this->attempts == quiz_attempt_report::STUDENTS_WITH_NO) { + $this->onlygraded = false; + } + } +} diff --git a/mod/quiz/report/overview/overviewsettings_form.php b/mod/quiz/report/overview/overview_form.php similarity index 100% rename from mod/quiz/report/overview/overviewsettings_form.php rename to mod/quiz/report/overview/overview_form.php diff --git a/mod/quiz/report/overview/overview_options.php b/mod/quiz/report/overview/overview_options.php new file mode 100644 index 00000000000..5c3d5632a4d --- /dev/null +++ b/mod/quiz/report/overview/overview_options.php @@ -0,0 +1,86 @@ +. + +/** + * Class to store the options for a {@link quiz_overview_report}. + * + * @package quiz_overview + * @copyright 2012 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->dirroot . '/mod/quiz/report/attemptsreport_options.php'); + + +/** + * Class to store the options for a {@link quiz_overview_report}. + * + * @copyright 2012 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class quiz_overview_options extends mod_quiz_attempt_report_options { + + /** @var bool whether to show only attempt that need regrading. */ + public $onlyregraded = false; + + /** @var bool whether to show marks for each question (slot). */ + public $slotmarks = true; + + public function get_initial_form_data() { + $toform = parent::get_initial_form_data(); + $toform->regradefilter = $this->onlyregraded; + $toform->detailedmarks = $this->slotmarks; + + return $toform; + } + + public function setup_from_form_data($fromform) { + parent::setup_from_form_data($fromform); + + $this->onlyregraded = !empty($fromform->regradefilter); + $this->slotmarks = $fromform->detailedmarks; + } + + public function setup_from_params() { + parent::setup_from_params(); + + $this->onlyregraded = optional_param('regradefilter', $this->onlyregraded, PARAM_BOOL); + $this->slotmarks = optional_param('detailedmarks', $this->slotmarks, PARAM_BOOL); + } + + public function setup_from_user_preferences() { + parent::setup_from_user_preferences(); + + $this->slotmarks = get_user_preferences('quiz_report_overview_detailedmarks', $this->slotmarks); + } + + public function update_user_preferences() { + parent::update_user_preferences(); + + set_user_preference('quiz_overview_slotmarks', $this->slotmarks); + } + + public function resolve_dependencies() { + parent::resolve_dependencies(); + + if (!quiz_report_should_show_grades($this->quiz)) { + $this->slotmarks = false; + } + } +} diff --git a/mod/quiz/report/overview/report.php b/mod/quiz/report/overview/report.php index 7c8050424d7..4ce790d44ac 100644 --- a/mod/quiz/report/overview/report.php +++ b/mod/quiz/report/overview/report.php @@ -26,7 +26,8 @@ defined('MOODLE_INTERNAL') || die(); require_once($CFG->dirroot . '/mod/quiz/report/attemptsreport.php'); -require_once($CFG->dirroot . '/mod/quiz/report/overview/overviewsettings_form.php'); +require_once($CFG->dirroot . '/mod/quiz/report/overview/overview_options.php'); +require_once($CFG->dirroot . '/mod/quiz/report/overview/overview_form.php'); require_once($CFG->dirroot . '/mod/quiz/report/overview/overview_table.php'); @@ -43,58 +44,29 @@ class quiz_overview_report extends quiz_attempt_report { list($currentgroup, $students, $groupstudents, $allowed) = $this->init('overview', 'quiz_overview_settings_form', $quiz, $cm, $course); + $options = new quiz_overview_options($quiz, $cm, $course); if ($fromform = $this->form->get_data()) { - $regradeall = false; - $regradealldry = false; - $regradealldrydo = false; - $attemptsmode = $fromform->attemptsmode; - if ($this->qmsubselect) { - $qmfilter = $fromform->qmfilter; - } else { - $qmfilter = 0; - } - $regradefilter = !empty($fromform->regradefilter); - set_user_preference('quiz_report_overview_detailedmarks', $fromform->detailedmarks); - set_user_preference('quiz_report_pagesize', $fromform->pagesize); - $detailedmarks = $fromform->detailedmarks; - $pagesize = $fromform->pagesize; + $options->process_settings_from_form($fromform); } else { - $regradeall = optional_param('regradeall', 0, PARAM_BOOL); - $regradealldry = optional_param('regradealldry', 0, PARAM_BOOL); - $regradealldrydo = optional_param('regradealldrydo', 0, PARAM_BOOL); - $attemptsmode = optional_param('attemptsmode', null, PARAM_INT); - if ($this->qmsubselect) { - $qmfilter = optional_param('qmfilter', 0, PARAM_INT); - } else { - $qmfilter = 0; - } - $regradefilter = optional_param('regradefilter', 0, PARAM_INT); - $detailedmarks = get_user_preferences('quiz_report_overview_detailedmarks', 1); - $pagesize = get_user_preferences('quiz_report_pagesize', 0); + $options->process_settings_from_params(); } - $this->validate_common_options($attemptsmode, $pagesize, $course, $currentgroup); $displayoptions = array(); - $displayoptions['attemptsmode'] = $attemptsmode; - $displayoptions['qmfilter'] = $qmfilter; - $displayoptions['regradefilter'] = $regradefilter; + $displayoptions['attemptsmode'] = $options->attempts; + $displayoptions['qmfilter'] = $options->onlygraded; + $displayoptions['regradefilter'] = $options->onlyregraded; - $this->form->set_data($displayoptions + - array('detailedmarks' => $detailedmarks, 'pagesize' => $pagesize)); - - if (!$this->should_show_grades($quiz)) { - $detailedmarks = 0; - } + $this->form->set_data($options->get_initial_form_data()); // We only want to show the checkbox to delete attempts // if the user has permissions and if the report mode is showing attempts. $includecheckboxes = has_any_capability( array('mod/quiz:regrade', 'mod/quiz:deleteattempts'), $this->context) - && ($attemptsmode != self::STUDENTS_WITH_NO); + && ($options->attempts != self::STUDENTS_WITH_NO); - if ($attemptsmode == self::ALL_ATTEMPTS) { + if ($options->attempts == self::ALL_ATTEMPTS) { // This option is only available to users who can access all groups in // groups mode, so setting allowed to empty (which means all quiz attempts // are accessible, is not a security porblem. @@ -108,11 +80,11 @@ class quiz_overview_report extends quiz_attempt_report { $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id))); $table = new quiz_overview_table($quiz, $this->context, $this->qmsubselect, - $qmfilter, $attemptsmode, $groupstudents, $students, $detailedmarks, + $options->onlygraded, $options->attempts, $groupstudents, $students, $options->slotmarks, $questions, $includecheckboxes, $this->get_base_url(), $displayoptions); $filename = quiz_report_download_filename(get_string('overviewfilename', 'quiz_overview'), $courseshortname, $quiz->name); - $table->is_downloading(optional_param('download', '', PARAM_ALPHA), $filename, + $table->is_downloading($options->download, $filename, $courseshortname . ' ' . format_string($quiz->name, true)); if ($table->is_downloading()) { raise_memory_limit(MEMORY_EXTRA); @@ -136,6 +108,9 @@ class quiz_overview_report extends quiz_attempt_report { } } + $regradeall = optional_param('regradeall', 0, PARAM_BOOL); + $regradealldry = optional_param('regradealldry', 0, PARAM_BOOL); + $regradealldrydo = optional_param('regradealldrydo', 0, PARAM_BOOL); if ($regradeall && confirm_sesskey()) { require_capability('mod/quiz:regrade', $this->context); $this->regrade_attempts($quiz, false, $groupstudents); @@ -188,7 +163,7 @@ class quiz_overview_report extends quiz_attempt_report { } $hasstudents = $students && (!$currentgroup || $groupstudents); - if ($hasquestions && ($hasstudents || $attemptsmode == self::ALL_ATTEMPTS)) { + if ($hasquestions && ($hasstudents || $options->attempts == self::ALL_ATTEMPTS)) { // Construct the SQL. $fields = $DB->sql_concat('u.id', "'#'", 'COALESCE(quiza.attempt, 0)') . ' AS uniqueid, '; @@ -210,7 +185,7 @@ class quiz_overview_report extends quiz_attempt_report { FROM {quiz_overview_regrades} qqr WHERE qqr.questionusageid = quiza.uniqueid ), -1) AS regraded"; - if ($regradefilter) { + if ($options->onlyregraded) { $where .= " AND COALESCE(( SELECT MAX(qqr.regraded) FROM {quiz_overview_regrades} qqr @@ -262,7 +237,7 @@ class quiz_overview_report extends quiz_attempt_report { } // Print information on the grading method. if ($strattempthighlight = quiz_report_highlighting_grading_method( - $quiz, $this->qmsubselect, $qmfilter)) { + $quiz, $this->qmsubselect, $options->onlygraded)) { echo '