diff --git a/mod/quiz/edit.php b/mod/quiz/edit.php index c5e75e1dcc2..3f483c04a2f 100644 --- a/mod/quiz/edit.php +++ b/mod/quiz/edit.php @@ -522,8 +522,7 @@ if (!$quiz_reordertool) { $notifystrings = array(); if ($quizhasattempts) { - $reviewlink = '' . - quiz_num_attempt_summary($quiz, $cm) . ''; + $reviewlink = quiz_attempt_summary_link_to_reports($quiz, $cm, $contexts->lowest()); $notifystrings[] = get_string('cannoteditafterattempts', 'quiz', $reviewlink); } if ($quiz->shufflequestions) { diff --git a/mod/quiz/index.php b/mod/quiz/index.php index 125eac406d9..351b5051596 100644 --- a/mod/quiz/index.php +++ b/mod/quiz/index.php @@ -138,14 +138,9 @@ if ($showing == 'stats') { // The $quiz objects returned by get_all_instances_in_course have the necessary $cm // fields set to make the following call work. - $attemptcount = quiz_num_attempt_summary($quiz, $quiz); - if ($attemptcount) { - $data[] = "coursemodule\">$attemptcount"; - } else { - $data[] = ''; - } - } else if ($showing == 'scores') { + $data[] = quiz_attempt_summary_link_to_reports($quiz, $cm, $context); + } else if ($showing == 'scores') { // Grade and feedback. $attempts = quiz_get_user_attempts($quiz->id, $USER->id, 'all'); list($someoptions, $alloptions) = quiz_get_combined_reviewoptions($quiz, $attempts, $context); diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index b713abc788d..23810ce267e 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -1510,9 +1510,6 @@ function quiz_print_overview($courses, &$htmlarray) { * Return a textual summary of the number of attemtps that have been made at a particular quiz, * returns '' if no attemtps have been made yet, unless $returnzero is passed as true. * - * @global stdClass - * @global object - * @global object * @param object $quiz the quiz object. Only $quiz->id is used at the moment. * @param object $cm the cm object. Only $cm->course, $cm->groupmode and $cm->groupingid fields are used at the moment. * @param boolean $returnzero if false (default), when no attempts have been made '' is returned instead of 'Attempts: 0'. @@ -1522,7 +1519,7 @@ function quiz_print_overview($courses, &$htmlarray) { * "Attemtps 123 (45 from this group)". */ function quiz_num_attempt_summary($quiz, $cm, $returnzero = false, $currentgroup = 0) { - global $CFG, $USER, $DB; + global $DB, $USER; $numattempts = $DB->count_records('quiz_attempts', array('quiz'=> $quiz->id, 'preview'=>0)); if ($numattempts || $returnzero) { if (groups_get_activity_groupmode($cm)) { @@ -1549,13 +1546,31 @@ function quiz_num_attempt_summary($quiz, $cm, $returnzero = false, $currentgroup } /** - * @uses FEATURE_GROUPS - * @uses FEATURE_GROUPINGS - * @uses FEATURE_GROUPMEMBERSONLY - * @uses FEATURE_MOD_INTRO - * @uses FEATURE_COMPLETION_TRACKS_VIEWS - * @uses FEATURE_GRADE_HAS_GRADE - * @uses FEATURE_GRADE_OUTCOMES + * Returns the same as {@link quiz_num_attempt_summary()} but wrapped in a link + * to the quiz reports. + * + * @param object $quiz the quiz object. Only $quiz->id is used at the moment. + * @param object $cm the cm object. Only $cm->course, $cm->groupmode and $cm->groupingid fields are used at the moment. + * @param object $context the quiz context. + * @param boolean $returnzero if false (default), when no attempts have been made '' is returned instead of 'Attempts: 0'. + * @param int $currentgroup if there is a concept of current group where this method is being called + * (e.g. a report) pass it in here. Default 0 which means no current group. + * @return string HTML fragment for the link. + */ +function quiz_attempt_summary_link_to_reports($quiz, $cm, $context, $returnzero = false, $currentgroup = 0) { + global $CFG; + $summary = quiz_num_attempt_summary($quiz, $cm, $returnzero, $currentgroup); + if (!$summary) { + return ''; + } + + require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php'); + $url = new moodle_url('/mod/quiz/report.php', array( + 'id' => $cm->id, 'mode' => quiz_report_default_report($context))); + return html_writer::link($url, $summary); +} + +/** * @param string $feature FEATURE_xx constant for requested feature * @return bool True if quiz supports feature */ @@ -1606,22 +1621,22 @@ function quiz_extend_navigation($quiznode, $course, $module, $cm) { if (has_capability('mod/quiz:view', $context)) { $url = new moodle_url('/mod/quiz/view.php', array('id'=>$cm->id)); - $quiznode->add(get_string('info', 'quiz'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/info', '')); + $quiznode->add(get_string('info', 'quiz'), $url, navigation_node::TYPE_SETTING, + null, null, new pix_icon('i/info', '')); } if (has_capability('mod/quiz:viewreports', $context)) { - $url = new moodle_url('/mod/quiz/report.php', array('q'=>$cm->instance)); - $reportnode = $quiznode->add(get_string('results', 'quiz'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/report', '')); - require_once($CFG->dirroot.'/mod/quiz/report/reportlib.php'); $reportlist = quiz_report_list($context); + + $url = new moodle_url('/mod/quiz/report.php', array('id' => $cm->id, 'mode' => reset($reportlist))); + $reportnode = $quiznode->add(get_string('results', 'quiz'), $url, navigation_node::TYPE_SETTING, + null, null, new pix_icon('i/report', '')); + foreach ($reportlist as $report) { - if ($report != 'overview') { - $url = new moodle_url('/mod/quiz/report.php', array('q'=>$cm->instance, 'mode'=>$report)); - } else { - $url = new moodle_url('/mod/quiz/report.php', array('q'=>$cm->instance)); - } - $reportnode->add(get_string($report, 'quiz_'.$report), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/item', '')); + $url = new moodle_url('/mod/quiz/report.php', array('id' => $cm->id, 'mode' => $report)); + $reportnode->add(get_string($report, 'quiz_'.$report), $url, navigation_node::TYPE_SETTING, + null, null, new pix_icon('i/item', '')); } } } diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index 42a6f66a01c..85ea4bf02d5 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -1118,11 +1118,11 @@ function quiz_send_notification_emails($course, $quiz, $attempt, $context, $cm) $a->courseshortname = $course->shortname; // quiz info $a->quizname = $quiz->name; - $a->quizreporturl = $CFG->wwwroot . '/mod/quiz/report.php?q=' . $quiz->id; + $a->quizreporturl = $CFG->wwwroot . '/mod/quiz/report.php?id=' . $cm->id; $a->quizreportlink = '' . format_string($quiz->name) . ' report'; $a->quizreviewurl = $CFG->wwwroot . '/mod/quiz/review.php?attempt=' . $attempt->id; $a->quizreviewlink = '' . format_string($quiz->name) . ' review'; - $a->quizurl = $CFG->wwwroot . '/mod/quiz/view.php?q=' . $quiz->id; + $a->quizurl = $CFG->wwwroot . '/mod/quiz/view.php?id=' . $cm->id; $a->quizlink = '' . format_string($quiz->name) . ''; // attempt info $a->submissiontime = userdate($attempt->timefinish); diff --git a/mod/quiz/report.php b/mod/quiz/report.php index c406a305770..33ff1c585b4 100644 --- a/mod/quiz/report.php +++ b/mod/quiz/report.php @@ -36,12 +36,7 @@ } } - $url = new moodle_url('/mod/quiz/report.php'); - if ($id !== 0) { - $url->param('id', $id); - } else { - $url->param('q', $q); - } + $url = new moodle_url('/mod/quiz/report.php', array('id' => $cm->id)); if ($mode !== '') { $url->param('mode', $mode); } @@ -54,15 +49,17 @@ if (count($reportlist)==0){ print_error('erroraccessingreport', 'quiz'); } - if ($mode == ''){ - $mode = reset($reportlist);//first element in array - } elseif (!in_array($mode, $reportlist)){ + if ($mode == '') { + // Default to first accessible report and redirect. + $url->param('mode', reset($reportlist)); + redirect($url); + } else if (!in_array($mode, $reportlist)){ print_error('erroraccessingreport', 'quiz'); } // if no questions have been set up yet redirect to edit.php if (!$quiz->questions and has_capability('mod/quiz:manage', $context)) { - redirect('edit.php?cmid='.$cm->id); + redirect('edit.php?cmid=' . $cm->id); } // Upgrade any attempts that have not yet been upgraded to the @@ -79,17 +76,17 @@ /// Open the selected quiz report and display it - if (! is_readable("report/$mode/report.php")) { + if (!is_readable("report/$mode/report.php")) { print_error('reportnotfound', 'quiz', '', $mode); } - include("report/default.php"); // Parent class + include("report/default.php"); // Parent class include("report/$mode/report.php"); $reportclassname = "quiz_{$mode}_report"; $report = new $reportclassname(); - if (! $report->display($quiz, $cm, $course)) { // Run the report! + if (!$report->display($quiz, $cm, $course)) { // Run the report! print_error("preprocesserror", 'quiz'); } diff --git a/mod/quiz/report/overview/report.php b/mod/quiz/report/overview/report.php index 6790a9155e0..568d06e141b 100644 --- a/mod/quiz/report/overview/report.php +++ b/mod/quiz/report/overview/report.php @@ -56,7 +56,6 @@ class quiz_overview_report extends quiz_default_report { $pageoptions = array(); $pageoptions['id'] = $cm->id; - $pageoptions['q'] = $quiz->id; $pageoptions['mode'] = 'overview'; $reporturl = new moodle_url('/mod/quiz/report.php', $pageoptions); diff --git a/mod/quiz/report/reportlib.php b/mod/quiz/report/reportlib.php index 0534c54a93f..e40201ed813 100644 --- a/mod/quiz/report/reportlib.php +++ b/mod/quiz/report/reportlib.php @@ -347,14 +347,14 @@ function quiz_report_scale_sumgrades_as_percentage($rawgrade, $quiz, $round = tr * Returns an array of reports to which the current user has access to. * Reports are ordered as they should be for display in tabs. */ -function quiz_report_list($context){ +function quiz_report_list($context) { global $DB; static $reportlist = null; if (!is_null($reportlist)){ return $reportlist; } $reports = $DB->get_records('quiz_report', null, 'displayorder DESC', 'name, capability'); - $reportdirs = get_plugin_list("quiz"); + $reportdirs = get_plugin_list('quiz'); // Order the reports tab in descending order of displayorder $reportcaps = array(); @@ -382,4 +382,10 @@ function quiz_report_list($context){ return $reportlist; } - +/** + * Get the default report for the current user. + * @param object $context the quiz context. + */ +function quiz_report_default_report($context) { + return reset(quiz_report_list($context)); +} diff --git a/mod/quiz/report/responses/report.php b/mod/quiz/report/responses/report.php index 126ff50bb2f..b2171254ec0 100644 --- a/mod/quiz/report/responses/report.php +++ b/mod/quiz/report/responses/report.php @@ -35,7 +35,6 @@ class quiz_responses_report extends quiz_default_report { $pageoptions = array(); $pageoptions['id'] = $cm->id; - $pageoptions['q'] = $quiz->id; $pageoptions['mode'] = 'responses'; $reporturl = new moodle_url('/mod/quiz/report.php', $pageoptions); diff --git a/mod/quiz/report/statistics/report.php b/mod/quiz/report/statistics/report.php index 1c809d5c629..4cb3dfaf0d5 100644 --- a/mod/quiz/report/statistics/report.php +++ b/mod/quiz/report/statistics/report.php @@ -33,7 +33,6 @@ class quiz_statistics_report extends quiz_default_report { $qid = optional_param('qid', 0, PARAM_INT); $pageoptions = array(); $pageoptions['id'] = $cm->id; - $pageoptions['q'] = $quiz->id; $pageoptions['mode'] = 'statistics'; $questions = quiz_report_load_questions($quiz); @@ -42,7 +41,6 @@ class quiz_statistics_report extends quiz_default_report { print_error('cannotloadquestion', 'question'); } - $reporturl = new moodle_url('/mod/quiz/report.php', $pageoptions); $mform = new mod_quiz_report_statistics($reporturl); diff --git a/mod/quiz/view.php b/mod/quiz/view.php index 97065818341..920d8af490f 100644 --- a/mod/quiz/view.php +++ b/mod/quiz/view.php @@ -102,9 +102,8 @@ /// Show number of attempts summary to those who can view reports. if (has_capability('mod/quiz:viewreports', $context)) { - if ($strattemptnum = quiz_num_attempt_summary($quiz, $cm)) { - echo '
' . $strattemptnum . "
\n"; + if ($strattemptnum = quiz_attempt_summary_link_to_reports($quiz, $cm, $context)) { + echo '
' . $strattemptnum . "
\n"; } }