|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 require_once("../../config.php"); 00004 require_once("$CFG->libdir/graphlib.php"); 00005 require_once("lib.php"); 00006 00007 $id = required_param('id', PARAM_INT); // Course Module ID 00008 $type = required_param('type', PARAM_FILE); // Graph Type 00009 $group = optional_param('group', 0, PARAM_INT); // Group ID 00010 $sid = optional_param('sid', false, PARAM_INT); // Student ID 00011 $qid = optional_param('qid', 0, PARAM_INT); // Group ID 00012 00013 $url = new moodle_url('/mod/survey/graph.php', array('id'=>$id, 'type'=>$type)); 00014 if ($group !== 0) { 00015 $url->param('group', $group); 00016 } 00017 if ($sid !== false) { 00018 $url->param('sid', $sid); 00019 } 00020 if ($qid !== 0) { 00021 $url->param('qid', $qid); 00022 } 00023 $PAGE->set_url($url); 00024 00025 if (! $cm = get_coursemodule_from_id('survey', $id)) { 00026 print_error('invalidcoursemodule'); 00027 } 00028 00029 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) { 00030 print_error('coursemisconf'); 00031 } 00032 00033 if ($sid) { 00034 if (!$user = $DB->get_record("user", array("id"=>$sid))) { 00035 print_error('invaliduserid'); 00036 } 00037 } 00038 00039 require_login($course->id, false, $cm); 00040 00041 $groupmode = groups_get_activity_groupmode($cm); // Groups are being used 00042 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00043 00044 if (!has_capability('mod/survey:readresponses', $context)) { 00045 if ($type != "student.png" or $sid != $USER->id ) { 00046 print_error('nopermissiontoshow'); 00047 } else if ($groupmode and !groups_is_member($group)) { 00048 print_error('nopermissiontoshow'); 00049 } 00050 } 00051 00052 if (! $survey = $DB->get_record("survey", array("id"=>$cm->instance))) { 00053 print_error('invalidsurveyid', 'survey'); 00054 } 00055 00057 if ($group) { 00058 $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $group, null, false); 00059 } else if (!empty($cm->groupingid)) { 00060 $groups = groups_get_all_groups($courseid, 0, $cm->groupingid); 00061 $groups = array_keys($groups); 00062 $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $groups, null, false); 00063 } else { 00064 $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', '', null, false); 00065 $group = false; 00066 } 00067 00068 $stractual = get_string("actual", "survey"); 00069 $stractualclass = get_string("actualclass", "survey"); 00070 00071 $strpreferred = get_string("preferred", "survey"); 00072 $strpreferredclass = get_string("preferredclass", "survey"); 00073 00074 if ($sid || isset($user)) { 00075 $stractualstudent = get_string("actualstudent", "survey", fullname($user)); 00076 $strpreferredstudent = get_string("preferredstudent", "survey", fullname($user)); 00077 } 00078 00079 $virtualscales = false; //set default value for case clauses 00080 00081 switch ($type) { 00082 00083 case "question.png": 00084 00085 $question = $DB->get_record("survey_questions", array("id"=>$qid)); 00086 $question->text = get_string($question->text, "survey"); 00087 $question->options = get_string($question->options, "survey"); 00088 00089 $options = explode(",",$question->options); 00090 00091 while (list($key,) = each($options)) { 00092 $buckets1[$key] = 0; 00093 $buckets2[$key] = 0; 00094 } 00095 00096 if ($aaa = $DB->get_records('survey_answers', array('survey'=>$cm->instance, 'question'=>$qid))) { 00097 foreach ($aaa as $aa) { 00098 if (!$group or isset($users[$aa->userid])) { 00099 if ($a1 = $aa->answer1) { 00100 $buckets1[$a1 - 1]++; 00101 } 00102 if ($a2 = $aa->answer2) { 00103 $buckets2[$a2 - 1]++; 00104 } 00105 } 00106 } 00107 } 00108 00109 00110 $maxbuckets1 = max($buckets1); 00111 $maxbuckets2 = max($buckets2); 00112 $maxbuckets = ($maxbuckets1 > $maxbuckets2) ? $maxbuckets1 : $maxbuckets2; 00113 00114 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT); 00115 $graph->parameter['title'] = "$question->text"; 00116 00117 $graph->x_data = $options; 00118 00119 $graph->y_data['answers1'] = $buckets1; 00120 $graph->y_format['answers1'] = array('colour' => 'ltblue','bar' => 'fill','legend' =>$stractual,'bar_size' => 0.4); 00121 $graph->y_data['answers2'] = $buckets2; 00122 $graph->y_format['answers2'] = array('colour' =>'ltorange','bar' => 'fill','legend' =>$strpreferred,'bar_size' => 0.2); 00123 00124 $graph->parameter['legend'] = 'outside-top'; 00125 $graph->parameter['legend_border'] = 'black'; 00126 $graph->parameter['legend_offset'] = 4; 00127 00128 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) { 00129 $graph->y_order = array('answers1', 'answers2'); 00130 } else if ($maxbuckets1 > 0.0) { 00131 $graph->y_order = array('answers1'); 00132 } else { 00133 $graph->y_order = array('answers2'); 00134 } 00135 00136 $graph->parameter['y_axis_gridlines']= $maxbuckets + 1; 00137 $graph->parameter['y_resolution_left']= 1; 00138 $graph->parameter['y_decimal_left'] = 0; 00139 $graph->parameter['x_axis_angle'] = 20; 00140 $graph->parameter['shadow'] = 'none'; 00141 00142 $graph->y_tick_labels = null; 00143 $graph->offset_relation = null; 00144 00145 $graph->draw_stack(); 00146 00147 break; 00148 00149 00150 00151 case "multiquestion.png": 00152 00153 $question = $DB->get_record("survey_questions", array("id"=>$qid)); 00154 $question->text = get_string($question->text, "survey"); 00155 $question->options = get_string($question->options, "survey"); 00156 00157 $options = explode(",",$question->options); 00158 $questionorder = explode( ",", $question->multi); 00159 00160 $qqq = $DB->get_records_list("survey_questions", "id", explode(',',$question->multi)); 00161 00162 foreach ($questionorder as $i => $val) { 00163 $names[$i] = get_string($qqq["$val"]->shorttext, "survey"); 00164 $buckets1[$i] = 0; 00165 $buckets2[$i] = 0; 00166 $count1[$i] = 0; 00167 $count2[$i] = 0; 00168 $indexof[$val] = $i; 00169 $stdev1[$i] = 0; 00170 $stdev2[$i] = 0; 00171 } 00172 00173 $aaa = $DB->get_records_select("survey_answers", "((survey = ?) AND (question in ($question->multi)))", array($cm->instance)); 00174 00175 if ($aaa) { 00176 foreach ($aaa as $a) { 00177 if (!$group or isset($users[$a->userid])) { 00178 $index = $indexof[$a->question]; 00179 if ($a->answer1) { 00180 $buckets1[$index] += $a->answer1; 00181 $count1[$index]++; 00182 } 00183 if ($a->answer2) { 00184 $buckets2[$index] += $a->answer2; 00185 $count2[$index]++; 00186 } 00187 } 00188 } 00189 } 00190 00191 foreach ($questionorder as $i => $val) { 00192 if ($count1[$i]) { 00193 $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i]; 00194 } 00195 if ($count2[$i]) { 00196 $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i]; 00197 } 00198 } 00199 00200 if ($aaa) { 00201 foreach ($aaa as $a) { 00202 if (!$group or isset($users[$a->userid])) { 00203 $index = $indexof[$a->question]; 00204 if ($a->answer1) { 00205 $difference = (float) ($a->answer1 - $buckets1[$index]); 00206 $stdev1[$index] += ($difference * $difference); 00207 } 00208 if ($a->answer2) { 00209 $difference = (float) ($a->answer2 - $buckets2[$index]); 00210 $stdev2[$index] += ($difference * $difference); 00211 } 00212 } 00213 } 00214 } 00215 00216 foreach ($questionorder as $i => $val) { 00217 if ($count1[$i]) { 00218 $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i])); 00219 } 00220 if ($count2[$i]) { 00221 $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i])); 00222 } 00223 $buckets1[$i] = $buckets1[$i] - 1; 00224 $buckets2[$i] = $buckets2[$i] - 1; 00225 } 00226 00227 00228 00229 $maxbuckets1 = max($buckets1); 00230 $maxbuckets2 = max($buckets2); 00231 00232 00233 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT); 00234 $graph->parameter['title'] = "$question->text"; 00235 00236 $graph->x_data = $names; 00237 $graph->y_data['answers1'] = $buckets1; 00238 $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square', 00239 'shadow_offset' => 4, 'legend' => $stractual); 00240 $graph->y_data['answers2'] = $buckets2; 00241 $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square', 00242 'shadow_offset' => 4, 'legend' => $strpreferred); 00243 $graph->y_data['stdev1'] = $stdev1; 00244 $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill', 00245 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.3); 00246 $graph->y_data['stdev2'] = $stdev2; 00247 $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill', 00248 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.2); 00249 $graph->offset_relation['stdev1'] = 'answers1'; 00250 $graph->offset_relation['stdev2'] = 'answers2'; 00251 00252 $graph->parameter['bar_size'] = 0.15; 00253 00254 $graph->parameter['legend'] = 'outside-top'; 00255 $graph->parameter['legend_border'] = 'black'; 00256 $graph->parameter['legend_offset'] = 4; 00257 00258 $graph->y_tick_labels = $options; 00259 00260 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) { 00261 $graph->y_order = array('stdev1', 'answers1', 'stdev2', 'answers2'); 00262 } else if ($maxbuckets1 > 0.0) { 00263 $graph->y_order = array('stdev1', 'answers1'); 00264 } else { 00265 $graph->y_order = array('stdev2', 'answers2'); 00266 } 00267 00268 $graph->parameter['y_max_left']= count($options) - 1; 00269 $graph->parameter['y_axis_gridlines']= count($options); 00270 $graph->parameter['y_resolution_left']= 1; 00271 $graph->parameter['y_decimal_left']= 1; 00272 $graph->parameter['x_axis_angle'] = 20; 00273 00274 $graph->draw(); 00275 00276 break; 00277 00278 00279 00280 case "overall.png": 00281 00282 $qqq = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions)); 00283 00284 00285 foreach ($qqq as $key => $qq) { 00286 if ($qq->multi) { 00287 $qqq[$key]->text = get_string($qq->text, "survey"); 00288 $qqq[$key]->options = get_string($qq->options, "survey"); 00289 if ($qq->type < 0) { 00290 $virtualscales = true; 00291 } 00292 } 00293 } 00294 foreach ($qqq as $qq) { // if any virtual, then use JUST virtual, else use JUST nonvirtual 00295 if ($qq->multi) { 00296 if ($virtualscales && $qq->type < 0) { 00297 $question[] = $qq; 00298 } else if (!$virtualscales && $qq->type > 0) { 00299 $question[] = $qq; 00300 } 00301 } 00302 } 00303 $numquestions = count($question); 00304 00305 $options = explode(",",$question[0]->options); 00306 $numoptions = count($options); 00307 00308 for ($i=0; $i<$numquestions; $i++) { 00309 $names[$i] = $question[$i]->text; 00310 $buckets1[$i] = 0.0; 00311 $buckets2[$i] = 0.0; 00312 $stdev1[$i] = 0.0; 00313 $stdev2[$i] = 0.0; 00314 $count1[$i] = 0; 00315 $count2[$i] = 0; 00316 $subquestions = $question[$i]->multi; // otherwise next line doesn't work 00317 $aaa = $DB->get_records_select("survey_answers", "((survey = ?) AND (question in ($subquestions)))", array($cm->instance)); 00318 00319 if ($aaa) { 00320 foreach ($aaa as $a) { 00321 if (!$group or isset($users[$a->userid])) { 00322 if ($a->answer1) { 00323 $buckets1[$i] += $a->answer1; 00324 $count1[$i]++; 00325 } 00326 if ($a->answer2) { 00327 $buckets2[$i] += $a->answer2; 00328 $count2[$i]++; 00329 } 00330 } 00331 } 00332 } 00333 00334 if ($count1[$i]) { 00335 $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i]; 00336 } 00337 if ($count2[$i]) { 00338 $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i]; 00339 } 00340 00341 // Calculate the standard devaiations 00342 if ($aaa) { 00343 foreach ($aaa as $a) { 00344 if (!$group or isset($users[$a->userid])) { 00345 if ($a->answer1) { 00346 $difference = (float) ($a->answer1 - $buckets1[$i]); 00347 $stdev1[$i] += ($difference * $difference); 00348 } 00349 if ($a->answer2) { 00350 $difference = (float) ($a->answer2 - $buckets2[$i]); 00351 $stdev2[$i] += ($difference * $difference); 00352 } 00353 } 00354 } 00355 } 00356 00357 if ($count1[$i]) { 00358 $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i])); 00359 } 00360 if ($count2[$i]) { 00361 $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i])); 00362 } 00363 00364 $buckets1[$i] = $buckets1[$i] - 1; // Hack because there should not be ANY 0 values in the data. 00365 $buckets2[$i] = $buckets2[$i] - 1; 00366 00367 } 00368 00369 $maxbuckets1 = max($buckets1); 00370 $maxbuckets2 = max($buckets2); 00371 00372 00373 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT); 00374 $graph->parameter['title'] = strip_tags(format_string($survey->name,true)); 00375 00376 $graph->x_data = $names; 00377 00378 $graph->y_data['answers1'] = $buckets1; 00379 $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square', 00380 'shadow_offset' => 4, 'legend' => $stractual); 00381 $graph->y_data['answers2'] = $buckets2; 00382 $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square', 00383 'shadow_offset' => 4, 'legend' => $strpreferred); 00384 00385 $graph->y_data['stdev1'] = $stdev1; 00386 $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill', 00387 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.3); 00388 $graph->y_data['stdev2'] = $stdev2; 00389 $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill', 00390 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.2); 00391 $graph->offset_relation['stdev1'] = 'answers1'; 00392 $graph->offset_relation['stdev2'] = 'answers2'; 00393 00394 $graph->parameter['legend'] = 'outside-top'; 00395 $graph->parameter['legend_border'] = 'black'; 00396 $graph->parameter['legend_offset'] = 4; 00397 00398 $graph->y_tick_labels = $options; 00399 00400 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) { 00401 $graph->y_order = array('stdev1', 'answers1', 'stdev2', 'answers2'); 00402 } else if ($maxbuckets1 > 0.0) { 00403 $graph->y_order = array('stdev1', 'answers1'); 00404 } else { 00405 $graph->y_order = array('stdev2', 'answers2'); 00406 } 00407 00408 $graph->parameter['y_max_left']= $numoptions - 1; 00409 $graph->parameter['y_axis_gridlines']= $numoptions; 00410 $graph->parameter['y_resolution_left']= 1; 00411 $graph->parameter['y_decimal_left']= 1; 00412 $graph->parameter['x_axis_angle'] = 0; 00413 $graph->parameter['x_inner_padding'] = 6; 00414 00415 $graph->draw(); 00416 00417 break; 00418 00419 00420 00421 case "student.png": 00422 00423 $qqq = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions)); 00424 00425 foreach ($qqq as $key => $qq) { 00426 if ($qq->multi) { 00427 $qqq[$key]->text = get_string($qq->text, "survey"); 00428 $qqq[$key]->options = get_string($qq->options, "survey"); 00429 if ($qq->type < 0) { 00430 $virtualscales = true; 00431 } 00432 } 00433 } 00434 foreach ($qqq as $qq) { // if any virtual, then use JUST virtual, else use JUST nonvirtual 00435 if ($qq->multi) { 00436 if ($virtualscales && $qq->type < 0) { 00437 $question[] = $qq; 00438 } else if (!$virtualscales && $qq->type > 0) { 00439 $question[] = $qq; 00440 } 00441 } 00442 } 00443 $numquestions= count($question); 00444 00445 $options = explode(",",$question[0]->options); 00446 $numoptions = count($options); 00447 00448 for ($i=0; $i<$numquestions; $i++) { 00449 $names[$i] = $question[$i]->text; 00450 $buckets1[$i] = 0.0; 00451 $buckets2[$i] = 0.0; 00452 $count1[$i] = 0; 00453 $count2[$i] = 0; 00454 $studbuckets1[$i] = 0.0; 00455 $studbuckets2[$i] = 0.0; 00456 $studcount1[$i] = 0; 00457 $studcount2[$i] = 0; 00458 $stdev1[$i] = 0.0; 00459 $stdev2[$i] = 0.0; 00460 00461 $subquestions = $question[$i]->multi; // otherwise next line doesn't work 00462 $aaa = $DB->get_records_select("survey_answers","((survey = ?) AND (question in ($subquestions)))", array($cm->instance)); 00463 00464 if ($aaa) { 00465 foreach ($aaa as $a) { 00466 if (!$group or isset($users[$a->userid])) { 00467 if ($a->userid == $sid) { 00468 if ($a->answer1) { 00469 $studbuckets1[$i] += $a->answer1; 00470 $studcount1[$i]++; 00471 } 00472 if ($a->answer2) { 00473 $studbuckets2[$i] += $a->answer2; 00474 $studcount2[$i]++; 00475 } 00476 } 00477 if ($a->answer1) { 00478 $buckets1[$i] += $a->answer1; 00479 $count1[$i]++; 00480 } 00481 if ($a->answer2) { 00482 $buckets2[$i] += $a->answer2; 00483 $count2[$i]++; 00484 } 00485 } 00486 } 00487 } 00488 00489 if ($count1[$i]) { 00490 $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i]; 00491 } 00492 if ($count2[$i]) { 00493 $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i]; 00494 } 00495 if ($studcount1[$i]) { 00496 $studbuckets1[$i] = (float)$studbuckets1[$i] / (float)$studcount1[$i]; 00497 } 00498 if ($studcount2[$i]) { 00499 $studbuckets2[$i] = (float)$studbuckets2[$i] / (float)$studcount2[$i]; 00500 } 00501 00502 // Calculate the standard devaiations 00503 foreach ($aaa as $a) { 00504 if (!$group or isset($users[$a->userid])) { 00505 if ($a->answer1) { 00506 $difference = (float) ($a->answer1 - $buckets1[$i]); 00507 $stdev1[$i] += ($difference * $difference); 00508 } 00509 if ($a->answer2) { 00510 $difference = (float) ($a->answer2 - $buckets2[$i]); 00511 $stdev2[$i] += ($difference * $difference); 00512 } 00513 } 00514 } 00515 00516 if ($count1[$i]) { 00517 $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i])); 00518 } 00519 if ($count2[$i]) { 00520 $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i])); 00521 } 00522 00523 $buckets1[$i] = $buckets1[$i] - 1; // Hack because there should not be ANY 0 values in the data. 00524 $buckets2[$i] = $buckets2[$i] - 1; 00525 $studbuckets1[$i] = $studbuckets1[$i] - 1; 00526 $studbuckets2[$i] = $studbuckets2[$i] - 1; 00527 00528 } 00529 00530 $maxbuckets1 = max($buckets1); 00531 $maxbuckets2 = max($buckets2); 00532 00533 00534 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT); 00535 $graph->parameter['title'] = strip_tags(format_string($survey->name,true)); 00536 00537 $graph->x_data = $names; 00538 00539 $graph->y_data['answers1'] = $buckets1; 00540 $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square', 00541 'shadow_offset' => 0.1, 'legend' => $stractualclass); 00542 $graph->y_data['answers2'] = $buckets2; 00543 $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square', 00544 'shadow_offset' => 0.1, 'legend' => $strpreferredclass); 00545 $graph->y_data['studanswers1'] = $studbuckets1; 00546 $graph->y_format['studanswers1'] = array('colour' => 'blue', 'line' => 'line', 'point' => 'square', 00547 'shadow_offset' => 4, 'legend' => $stractualstudent); 00548 $graph->y_data['studanswers2'] = $studbuckets2; 00549 $graph->y_format['studanswers2'] = array('colour' => 'orange', 'line' => 'line', 'point' => 'square', 00550 'shadow_offset' => 4, 'legend' => $strpreferredstudent); 00551 $graph->y_data['stdev1'] = $stdev1; 00552 $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill', 00553 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.3); 00554 $graph->y_data['stdev2'] = $stdev2; 00555 $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill', 00556 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.2); 00557 $graph->offset_relation['stdev1'] = 'answers1'; 00558 $graph->offset_relation['stdev2'] = 'answers2'; 00559 00560 $graph->y_tick_labels = $options; 00561 00562 $graph->parameter['bar_size'] = 0.15; 00563 00564 $graph->parameter['legend'] = 'outside-top'; 00565 $graph->parameter['legend_border'] = 'black'; 00566 $graph->parameter['legend_offset'] = 4; 00567 00568 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) { 00569 $graph->y_order = array('stdev1', 'stdev2', 'answers1', 'answers2', 'studanswers1', 'studanswers2'); 00570 } else if ($maxbuckets1 > 0.0) { 00571 $graph->y_order = array('stdev1', 'answers1', 'studanswers1'); 00572 } else { 00573 $graph->y_order = array('stdev2', 'answers2', 'studanswers2'); 00574 } 00575 00576 $graph->parameter['y_max_left']= $numoptions - 1; 00577 $graph->parameter['y_axis_gridlines']= $numoptions; 00578 $graph->parameter['y_resolution_left']= 1; 00579 $graph->parameter['y_decimal_left']= 1; 00580 $graph->parameter['x_axis_angle'] = 20; 00581 00582 $graph->draw(); 00583 break; 00584 00585 00586 00587 case "studentmultiquestion.png": 00588 00589 $question = $DB->get_record("survey_questions", array("id"=>$qid)); 00590 $question->text = get_string($question->text, "survey"); 00591 $question->options = get_string($question->options, "survey"); 00592 00593 $options = explode(",",$question->options); 00594 $questionorder = explode( ",", $question->multi); 00595 00596 $qqq = $DB->get_records_list("survey_questions", "id", explode(',', $question->multi)); 00597 00598 foreach ($questionorder as $i => $val) { 00599 $names[$i] = get_string($qqq[$val]->shorttext, "survey"); 00600 $buckets1[$i] = 0; 00601 $buckets2[$i] = 0; 00602 $count1[$i] = 0; 00603 $count2[$i] = 0; 00604 $indexof[$val] = $i; 00605 $studbuckets1[$i] = 0.0; 00606 $studbuckets2[$i] = 0.0; 00607 $studcount1[$i] = 0; 00608 $studcount2[$i] = 0; 00609 $stdev1[$i] = 0.0; 00610 $stdev2[$i] = 0.0; 00611 } 00612 00613 $aaa = $DB->get_records_select("survey_answers", "((survey = ?) AND (question in ($question->multi)))", array($cm->instance)); 00614 00615 if ($aaa) { 00616 foreach ($aaa as $a) { 00617 if (!$group or isset($users[$a->userid])) { 00618 $index = $indexof[$a->question]; 00619 if ($a->userid == $sid) { 00620 if ($a->answer1) { 00621 $studbuckets1[$index] += $a->answer1; 00622 $studcount1[$index]++; 00623 } 00624 if ($a->answer2) { 00625 $studbuckets2[$index] += $a->answer2; 00626 $studcount2[$index]++; 00627 } 00628 } 00629 if ($a->answer1) { 00630 $buckets1[$index] += $a->answer1; 00631 $count1[$index]++; 00632 } 00633 if ($a->answer2) { 00634 $buckets2[$index] += $a->answer2; 00635 $count2[$index]++; 00636 } 00637 } 00638 } 00639 } 00640 00641 foreach ($questionorder as $i => $val) { 00642 if ($count1[$i]) { 00643 $buckets1[$i] = (float)$buckets1[$i] / (float)$count1[$i]; 00644 } 00645 if ($count2[$i]) { 00646 $buckets2[$i] = (float)$buckets2[$i] / (float)$count2[$i]; 00647 } 00648 if ($studcount1[$i]) { 00649 $studbuckets1[$i] = (float)$studbuckets1[$i] / (float)$studcount1[$i]; 00650 } 00651 if ($studcount2[$i]) { 00652 $studbuckets2[$i] = (float)$studbuckets2[$i] / (float)$studcount2[$i]; 00653 } 00654 } 00655 00656 foreach ($aaa as $a) { 00657 if (!$group or isset($users[$a->userid])) { 00658 $index = $indexof[$a->question]; 00659 if ($a->answer1) { 00660 $difference = (float) ($a->answer1 - $buckets1[$index]); 00661 $stdev1[$index] += ($difference * $difference); 00662 } 00663 if ($a->answer2) { 00664 $difference = (float) ($a->answer2 - $buckets2[$index]); 00665 $stdev2[$index] += ($difference * $difference); 00666 } 00667 } 00668 } 00669 00670 foreach ($questionorder as $i => $val) { 00671 if ($count1[$i]) { 00672 $stdev1[$i] = sqrt( (float)$stdev1[$i] / ((float)$count1[$i])); 00673 } 00674 if ($count2[$i]) { 00675 $stdev2[$i] = sqrt( (float)$stdev2[$i] / ((float)$count2[$i])); 00676 } 00677 $buckets1[$i] = $buckets1[$i] - 1; // Hack because there should not be ANY 0 values in the data. 00678 $buckets2[$i] = $buckets2[$i] - 1; 00679 $studbuckets1[$i] = $studbuckets1[$i] - 1; 00680 $studbuckets2[$i] = $studbuckets2[$i] - 1; 00681 } 00682 00683 00684 00685 $maxbuckets1 = max($buckets1); 00686 $maxbuckets2 = max($buckets2); 00687 00688 00689 $graph = new graph($SURVEY_GWIDTH,$SURVEY_GHEIGHT); 00690 $graph->parameter['title'] = "$question->text"; 00691 00692 $graph->x_data = $names; 00693 $graph->y_data['answers1'] = $buckets1; 00694 $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square', 00695 'shadow_offset' => 0.1, 'legend' => $stractualclass); 00696 $graph->y_data['answers2'] = $buckets2; 00697 $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square', 00698 'shadow_offset' => 0.1, 'legend' => $strpreferredclass); 00699 $graph->y_data['studanswers1'] = $studbuckets1; 00700 $graph->y_format['studanswers1'] = array('colour' => 'blue', 'line' => 'line', 'point' => 'square', 00701 'shadow_offset' => 4, 'legend' => $stractualstudent); 00702 $graph->y_data['studanswers2'] = $studbuckets2; 00703 $graph->y_format['studanswers2'] = array('colour' => 'orange', 'line' => 'line', 'point' => 'square', 00704 'shadow_offset' => 4, 'legend' => $strpreferredstudent); 00705 $graph->y_data['stdev1'] = $stdev1; 00706 $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill', 00707 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.3); 00708 $graph->y_data['stdev2'] = $stdev2; 00709 $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill', 00710 'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.2); 00711 $graph->offset_relation['stdev1'] = 'answers1'; 00712 $graph->offset_relation['stdev2'] = 'answers2'; 00713 00714 $graph->parameter['bar_size'] = 0.15; 00715 00716 $graph->parameter['legend'] = 'outside-top'; 00717 $graph->parameter['legend_border'] = 'black'; 00718 $graph->parameter['legend_offset'] = 4; 00719 00720 $graph->y_tick_labels = $options; 00721 00722 if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) { 00723 $graph->y_order = array('stdev1', 'stdev2', 'answers1', 'answers2', 'studanswers1', 'studanswers2'); 00724 } else if ($maxbuckets1 > 0.0) { 00725 $graph->y_order = array('stdev1', 'answers1', 'studanswers1'); 00726 } else { 00727 $graph->y_order = array('stdev2', 'answers2', 'studanswers2'); 00728 } 00729 00730 $graph->parameter['y_max_left']= count($options)-1; 00731 $graph->parameter['y_axis_gridlines']= count($options); 00732 $graph->parameter['y_resolution_left']= 1; 00733 $graph->parameter['y_decimal_left']= 1; 00734 $graph->parameter['x_axis_angle'] = 20; 00735 00736 $graph->draw(); 00737 00738 break; 00739 00740 default: 00741 break; 00742 } 00743 00744 exit; 00745 00746 00747