|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00033 require_once(dirname(__FILE__) . '/../../../../config.php'); 00034 require_once($CFG->libdir . '/graphlib.php'); 00035 require_once($CFG->dirroot . '/mod/quiz/locallib.php'); 00036 require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php'); 00037 00038 00044 function graph_get_new_colour() { 00045 static $colourindex = -1; 00046 $colours = array('red', 'green', 'yellow', 'orange', 'purple', 'black', 00047 'maroon', 'blue', 'ltgreen', 'navy', 'ltred', 'ltltgreen', 'ltltorange', 00048 'olive', 'gray', 'ltltred', 'ltorange', 'lime', 'ltblue', 'ltltblue'); 00049 00050 $colourindex = ($colourindex + 1) % count($colours); 00051 00052 return $colours[$colourindex]; 00053 } 00054 00055 // Get the parameters. 00056 $quizstatisticsid = required_param('id', PARAM_INT); 00057 00058 // Load enough data to check permissions. 00059 $quizstatistics = $DB->get_record('quiz_statistics', array('id' => $quizstatisticsid)); 00060 $quiz = $DB->get_record('quiz', array('id' => $quizstatistics->quizid), '*', MUST_EXIST); 00061 $cm = get_coursemodule_from_instance('quiz', $quiz->id); 00062 00063 // Check access. 00064 require_login($quiz->course, false, $cm); 00065 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); 00066 require_capability('quiz/statistics:view', $modcontext); 00067 00068 if (groups_get_activity_groupmode($cm)) { 00069 $groups = groups_get_activity_allowed_groups($cm); 00070 } else { 00071 $groups = array(); 00072 } 00073 if ($quizstatistics->groupid && !in_array($quizstatistics->groupid, array_keys($groups))) { 00074 print_error('groupnotamember', 'group'); 00075 } 00076 00077 // Load the rest of the required data. 00078 $questions = quiz_report_get_significant_questions($quiz); 00079 $questionstatistics = $DB->get_records_select('quiz_question_statistics', 00080 'quizstatisticsid = ? AND slot IS NOT NULL', array($quizstatistics->id)); 00081 00082 // Create the graph, and set the basic options. 00083 $graph = new graph(800, 600); 00084 $graph->parameter['title'] = ''; 00085 00086 $graph->parameter['y_label_left'] = '%'; 00087 $graph->parameter['x_label'] = get_string('position', 'quiz_statistics'); 00088 $graph->parameter['y_label_angle'] = 90; 00089 $graph->parameter['x_label_angle'] = 0; 00090 $graph->parameter['x_axis_angle'] = 60; 00091 00092 $graph->parameter['legend'] = 'outside-right'; 00093 $graph->parameter['legend_border'] = 'black'; 00094 $graph->parameter['legend_offset'] = 4; 00095 00096 $graph->parameter['bar_size'] = 1; 00097 00098 $graph->parameter['zero_axis'] = 'grayEE'; 00099 00100 // Configure what to display. 00101 $fieldstoplot = array( 00102 'facility' => get_string('facility', 'quiz_statistics'), 00103 'discriminativeefficiency' => get_string('discriminative_efficiency', 'quiz_statistics') 00104 ); 00105 $fieldstoplotfactor = array('facility' => 100, 'discriminativeefficiency' => 1); 00106 00107 // Prepare the arrays to hold the data. 00108 $xdata = array(); 00109 foreach (array_keys($fieldstoplot) as $fieldtoplot) { 00110 $ydata[$fieldtoplot] = array(); 00111 $graph->y_format[$fieldtoplot] = array( 00112 'colour' => graph_get_new_colour(), 00113 'bar' => 'fill', 00114 'shadow_offset' => 1, 00115 'legend' => $fieldstoplot[$fieldtoplot] 00116 ); 00117 } 00118 00119 // Fill in the data for each question. 00120 foreach ($questionstatistics as $questionstatistic) { 00121 $number = $questions[$questionstatistic->slot]->number; 00122 $xdata[$number] = $number; 00123 00124 foreach ($fieldstoplot as $fieldtoplot => $notused) { 00125 $value = $questionstatistic->$fieldtoplot; 00126 if (is_null($value)) { 00127 $value = 0; 00128 } 00129 $value *= $fieldstoplotfactor[$fieldtoplot]; 00130 00131 $ydata[$fieldtoplot][$number] = $value; 00132 } 00133 } 00134 00135 // Sort the fields into order. 00136 sort($xdata); 00137 $graph->x_data = array_values($xdata); 00138 00139 foreach ($fieldstoplot as $fieldtoplot => $notused) { 00140 ksort($ydata[$fieldtoplot]); 00141 $graph->y_data[$fieldtoplot] = array_values($ydata[$fieldtoplot]); 00142 } 00143 $graph->y_order = array_keys($fieldstoplot); 00144 00145 // Find appropriate axis limits. 00146 $max = 0; 00147 $min = 0; 00148 foreach ($fieldstoplot as $fieldtoplot => $notused) { 00149 $max = max($max, max($graph->y_data[$fieldtoplot])); 00150 $min = min($min, min($graph->y_data[$fieldtoplot])); 00151 } 00152 00153 // Set the remaining graph options that depend on the data. 00154 $gridresolution = 10; 00155 $max = ceil($max / $gridresolution) * $gridresolution; 00156 $min = floor($min / $gridresolution) * $gridresolution; 00157 $gridlines = ceil(($max - $min) / $gridresolution) + 1; 00158 00159 $graph->parameter['y_axis_gridlines'] = $gridlines; 00160 00161 $graph->parameter['y_min_left'] = $min; 00162 $graph->parameter['y_max_left'] = $max; 00163 $graph->parameter['y_decimal_left'] = 0; 00164 00165 // Output the graph. 00166 $graph->draw();