Merge branch 'wip-MDL-41102-m25' of git://github.com/marinaglancy/moodle into MOODLE_...
[moodle.git] / mod / choice / lib.php
blob48368cffc0d11a5eb433b94ae976b7a01654e44f
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * @package mod-choice
20 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 /** @global int $CHOICE_COLUMN_HEIGHT */
25 global $CHOICE_COLUMN_HEIGHT;
26 $CHOICE_COLUMN_HEIGHT = 300;
28 /** @global int $CHOICE_COLUMN_WIDTH */
29 global $CHOICE_COLUMN_WIDTH;
30 $CHOICE_COLUMN_WIDTH = 300;
32 define('CHOICE_PUBLISH_ANONYMOUS', '0');
33 define('CHOICE_PUBLISH_NAMES', '1');
35 define('CHOICE_SHOWRESULTS_NOT', '0');
36 define('CHOICE_SHOWRESULTS_AFTER_ANSWER', '1');
37 define('CHOICE_SHOWRESULTS_AFTER_CLOSE', '2');
38 define('CHOICE_SHOWRESULTS_ALWAYS', '3');
40 define('CHOICE_DISPLAY_HORIZONTAL', '0');
41 define('CHOICE_DISPLAY_VERTICAL', '1');
43 /** @global array $CHOICE_PUBLISH */
44 global $CHOICE_PUBLISH;
45 $CHOICE_PUBLISH = array (CHOICE_PUBLISH_ANONYMOUS => get_string('publishanonymous', 'choice'),
46 CHOICE_PUBLISH_NAMES => get_string('publishnames', 'choice'));
48 /** @global array $CHOICE_SHOWRESULTS */
49 global $CHOICE_SHOWRESULTS;
50 $CHOICE_SHOWRESULTS = array (CHOICE_SHOWRESULTS_NOT => get_string('publishnot', 'choice'),
51 CHOICE_SHOWRESULTS_AFTER_ANSWER => get_string('publishafteranswer', 'choice'),
52 CHOICE_SHOWRESULTS_AFTER_CLOSE => get_string('publishafterclose', 'choice'),
53 CHOICE_SHOWRESULTS_ALWAYS => get_string('publishalways', 'choice'));
55 /** @global array $CHOICE_DISPLAY */
56 global $CHOICE_DISPLAY;
57 $CHOICE_DISPLAY = array (CHOICE_DISPLAY_HORIZONTAL => get_string('displayhorizontal', 'choice'),
58 CHOICE_DISPLAY_VERTICAL => get_string('displayvertical','choice'));
60 /// Standard functions /////////////////////////////////////////////////////////
62 /**
63 * @global object
64 * @param object $course
65 * @param object $user
66 * @param object $mod
67 * @param object $choice
68 * @return object|null
70 function choice_user_outline($course, $user, $mod, $choice) {
71 global $DB;
72 if ($answer = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $user->id))) {
73 $result = new stdClass();
74 $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
75 $result->time = $answer->timemodified;
76 return $result;
78 return NULL;
81 /**
82 * @global object
83 * @param object $course
84 * @param object $user
85 * @param object $mod
86 * @param object $choice
87 * @return string|void
89 function choice_user_complete($course, $user, $mod, $choice) {
90 global $DB;
91 if ($answer = $DB->get_record('choice_answers', array("choiceid" => $choice->id, "userid" => $user->id))) {
92 $result = new stdClass();
93 $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
94 $result->time = $answer->timemodified;
95 echo get_string("answered", "choice").": $result->info. ".get_string("updated", '', userdate($result->time));
96 } else {
97 print_string("notanswered", "choice");
102 * Given an object containing all the necessary data,
103 * (defined by the form in mod_form.php) this function
104 * will create a new instance and return the id number
105 * of the new instance.
107 * @global object
108 * @param object $choice
109 * @return int
111 function choice_add_instance($choice) {
112 global $DB;
114 $choice->timemodified = time();
116 if (empty($choice->timerestrict)) {
117 $choice->timeopen = 0;
118 $choice->timeclose = 0;
121 //insert answers
122 $choice->id = $DB->insert_record("choice", $choice);
123 foreach ($choice->option as $key => $value) {
124 $value = trim($value);
125 if (isset($value) && $value <> '') {
126 $option = new stdClass();
127 $option->text = $value;
128 $option->choiceid = $choice->id;
129 if (isset($choice->limit[$key])) {
130 $option->maxanswers = $choice->limit[$key];
132 $option->timemodified = time();
133 $DB->insert_record("choice_options", $option);
137 return $choice->id;
141 * Given an object containing all the necessary data,
142 * (defined by the form in mod_form.php) this function
143 * will update an existing instance with new data.
145 * @global object
146 * @param object $choice
147 * @return bool
149 function choice_update_instance($choice) {
150 global $DB;
152 $choice->id = $choice->instance;
153 $choice->timemodified = time();
156 if (empty($choice->timerestrict)) {
157 $choice->timeopen = 0;
158 $choice->timeclose = 0;
161 //update, delete or insert answers
162 foreach ($choice->option as $key => $value) {
163 $value = trim($value);
164 $option = new stdClass();
165 $option->text = $value;
166 $option->choiceid = $choice->id;
167 if (isset($choice->limit[$key])) {
168 $option->maxanswers = $choice->limit[$key];
170 $option->timemodified = time();
171 if (isset($choice->optionid[$key]) && !empty($choice->optionid[$key])){//existing choice record
172 $option->id=$choice->optionid[$key];
173 if (isset($value) && $value <> '') {
174 $DB->update_record("choice_options", $option);
175 } else { //empty old option - needs to be deleted.
176 $DB->delete_records("choice_options", array("id"=>$option->id));
178 } else {
179 if (isset($value) && $value <> '') {
180 $DB->insert_record("choice_options", $option);
185 return $DB->update_record('choice', $choice);
190 * @global object
191 * @param object $choice
192 * @param object $user
193 * @param object $coursemodule
194 * @param array $allresponses
195 * @return array
197 function choice_prepare_options($choice, $user, $coursemodule, $allresponses) {
198 global $DB;
200 $cdisplay = array('options'=>array());
202 $cdisplay['limitanswers'] = true;
203 $context = context_module::instance($coursemodule->id);
205 foreach ($choice->option as $optionid => $text) {
206 if (isset($text)) { //make sure there are no dud entries in the db with blank text values.
207 $option = new stdClass;
208 $option->attributes = new stdClass;
209 $option->attributes->value = $optionid;
210 $option->text = $text;
211 $option->maxanswers = $choice->maxanswers[$optionid];
212 $option->displaylayout = $choice->display;
214 if (isset($allresponses[$optionid])) {
215 $option->countanswers = count($allresponses[$optionid]);
216 } else {
217 $option->countanswers = 0;
219 if ($DB->record_exists('choice_answers', array('choiceid' => $choice->id, 'userid' => $user->id, 'optionid' => $optionid))) {
220 $option->attributes->checked = true;
222 if ( $choice->limitanswers && ($option->countanswers >= $option->maxanswers) && empty($option->attributes->checked)) {
223 $option->attributes->disabled = true;
225 $cdisplay['options'][] = $option;
229 $cdisplay['hascapability'] = is_enrolled($context, NULL, 'mod/choice:choose'); //only enrolled users are allowed to make a choice
231 if ($choice->allowupdate && $DB->record_exists('choice_answers', array('choiceid'=> $choice->id, 'userid'=> $user->id))) {
232 $cdisplay['allowupdate'] = true;
235 return $cdisplay;
239 * @global object
240 * @param int $formanswer
241 * @param object $choice
242 * @param int $userid
243 * @param object $course Course object
244 * @param object $cm
246 function choice_user_submit_response($formanswer, $choice, $userid, $course, $cm) {
247 global $DB, $CFG;
248 require_once($CFG->libdir.'/completionlib.php');
250 $current = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $userid));
251 $context = context_module::instance($cm->id);
253 $countanswers=0;
254 if($choice->limitanswers) {
255 // Find out whether groups are being used and enabled
256 if (groups_get_activity_groupmode($cm) > 0) {
257 $currentgroup = groups_get_activity_group($cm);
258 } else {
259 $currentgroup = 0;
261 if($currentgroup) {
262 // If groups are being used, retrieve responses only for users in
263 // current group
264 global $CFG;
265 $answers = $DB->get_records_sql("
266 SELECT
267 ca.*
268 FROM
269 {choice_answers} ca
270 INNER JOIN {groups_members} gm ON ca.userid=gm.userid
271 WHERE
272 optionid=?
273 AND gm.groupid=?", array($formanswer, $currentgroup));
274 } else {
275 // Groups are not used, retrieve all answers for this option ID
276 $answers = $DB->get_records("choice_answers", array("optionid" => $formanswer));
279 if ($answers) {
280 foreach ($answers as $a) { //only return enrolled users.
281 if (is_enrolled($context, $a->userid, 'mod/choice:choose')) {
282 $countanswers++;
286 $maxans = $choice->maxanswers[$formanswer];
289 if (!($choice->limitanswers && ($countanswers >= $maxans) )) {
290 if ($current) {
292 $newanswer = $current;
293 $newanswer->optionid = $formanswer;
294 $newanswer->timemodified = time();
295 $DB->update_record("choice_answers", $newanswer);
296 add_to_log($course->id, "choice", "choose again", "view.php?id=$cm->id", $choice->id, $cm->id);
297 } else {
298 $newanswer = new stdClass();
299 $newanswer->choiceid = $choice->id;
300 $newanswer->userid = $userid;
301 $newanswer->optionid = $formanswer;
302 $newanswer->timemodified = time();
303 $DB->insert_record("choice_answers", $newanswer);
305 // Update completion state
306 $completion = new completion_info($course);
307 if ($completion->is_enabled($cm) && $choice->completionsubmit) {
308 $completion->update_state($cm, COMPLETION_COMPLETE);
310 add_to_log($course->id, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id);
312 } else {
313 if (!($current->optionid==$formanswer)) { //check to see if current choice already selected - if not display error
314 print_error('choicefull', 'choice');
320 * @param array $user
321 * @param object $cm
322 * @return void Output is echo'd
324 function choice_show_reportlink($user, $cm) {
325 $responsecount =0;
326 foreach($user as $optionid => $userlist) {
327 if ($optionid) {
328 $responsecount += count($userlist);
332 echo '<div class="reportlink">';
333 echo "<a href=\"report.php?id=$cm->id\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
334 echo '</div>';
338 * @global object
339 * @param object $choice
340 * @param object $course
341 * @param object $coursemodule
342 * @param array $allresponses
344 * * @param bool $allresponses
345 * @return object
347 function prepare_choice_show_results($choice, $course, $cm, $allresponses, $forcepublish=false) {
348 global $CFG, $CHOICE_COLUMN_HEIGHT, $FULLSCRIPT, $PAGE, $OUTPUT, $DB;
350 $display = clone($choice);
351 $display->coursemoduleid = $cm->id;
352 $display->courseid = $course->id;
354 //overwrite options value;
355 $display->options = array();
356 $totaluser = 0;
357 foreach ($choice->option as $optionid => $optiontext) {
358 $display->options[$optionid] = new stdClass;
359 $display->options[$optionid]->text = $optiontext;
360 $display->options[$optionid]->maxanswer = $choice->maxanswers[$optionid];
362 if (array_key_exists($optionid, $allresponses)) {
363 $display->options[$optionid]->user = $allresponses[$optionid];
364 $totaluser += count($allresponses[$optionid]);
367 unset($display->option);
368 unset($display->maxanswers);
370 $display->numberofuser = $totaluser;
371 $context = context_module::instance($cm->id);
372 $display->viewresponsecapability = has_capability('mod/choice:readresponses', $context);
373 $display->deleterepsonsecapability = has_capability('mod/choice:deleteresponses',$context);
374 $display->fullnamecapability = has_capability('moodle/site:viewfullnames', $context);
376 if (empty($allresponses)) {
377 echo $OUTPUT->heading(get_string("nousersyet"));
378 return false;
382 $totalresponsecount = 0;
383 foreach ($allresponses as $optionid => $userlist) {
384 if ($choice->showunanswered || $optionid) {
385 $totalresponsecount += count($userlist);
389 $hascapfullnames = has_capability('moodle/site:viewfullnames', $context);
391 $viewresponses = has_capability('mod/choice:readresponses', $context);
392 switch ($forcepublish) {
393 case CHOICE_PUBLISH_NAMES:
394 echo '<div id="tablecontainer">';
395 if ($viewresponses) {
396 echo '<form id="attemptsform" method="post" action="'.$FULLSCRIPT.'" onsubmit="var menu = document.getElementById(\'menuaction\'); return (menu.options[menu.selectedIndex].value == \'delete\' ? \''.addslashes_js(get_string('deleteattemptcheck','quiz')).'\' : true);">';
397 echo '<div>';
398 echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
399 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
400 echo '<input type="hidden" name="mode" value="overview" />';
403 echo "<table cellpadding=\"5\" cellspacing=\"10\" class=\"results names\">";
404 echo "<tr>";
406 $columncount = array(); // number of votes in each column
407 if ($choice->showunanswered) {
408 $columncount[0] = 0;
409 echo "<th class=\"col0 header\" scope=\"col\">";
410 print_string('notanswered', 'choice');
411 echo "</th>";
413 $count = 1;
414 foreach ($choice->option as $optionid => $optiontext) {
415 $columncount[$optionid] = 0; // init counters
416 echo "<th class=\"col$count header\" scope=\"col\">";
417 echo format_string($optiontext);
418 echo "</th>";
419 $count++;
421 echo "</tr><tr>";
423 if ($choice->showunanswered) {
424 echo "<td class=\"col$count data\" >";
425 // added empty row so that when the next iteration is empty,
426 // we do not get <table></table> error from w3c validator
427 // MDL-7861
428 echo "<table class=\"choiceresponse\"><tr><td></td></tr>";
429 if (!empty($allresponses[0])) {
430 foreach ($allresponses[0] as $user) {
431 echo "<tr>";
432 echo "<td class=\"picture\">";
433 echo $OUTPUT->user_picture($user, array('courseid'=>$course->id));
434 echo "</td><td class=\"fullname\">";
435 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
436 echo fullname($user, $hascapfullnames);
437 echo "</a>";
438 echo "</td></tr>";
441 echo "</table></td>";
443 $count = 1;
444 foreach ($choice->option as $optionid => $optiontext) {
445 echo '<td class="col'.$count.' data" >';
447 // added empty row so that when the next iteration is empty,
448 // we do not get <table></table> error from w3c validator
449 // MDL-7861
450 echo '<table class="choiceresponse"><tr><td></td></tr>';
451 if (isset($allresponses[$optionid])) {
452 foreach ($allresponses[$optionid] as $user) {
453 $columncount[$optionid] += 1;
454 echo '<tr><td class="attemptcell">';
455 if ($viewresponses and has_capability('mod/choice:deleteresponses',$context)) {
456 echo '<input type="checkbox" name="attemptid[]" value="'. $user->id. '" />';
458 echo '</td><td class="picture">';
459 echo $OUTPUT->user_picture($user, array('courseid'=>$course->id));
460 echo '</td><td class="fullname">';
461 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
462 echo fullname($user, $hascapfullnames);
463 echo '</a>';
464 echo '</td></tr>';
467 $count++;
468 echo '</table></td>';
470 echo "</tr><tr>";
471 $count = 1;
473 if ($choice->showunanswered) {
474 echo "<td></td>";
477 foreach ($choice->option as $optionid => $optiontext) {
478 echo "<td align=\"center\" class=\"col$count count\">";
479 if ($choice->limitanswers) {
480 echo get_string("taken", "choice").":";
481 echo $columncount[$optionid];
482 echo "<br/>";
483 echo get_string("limit", "choice").":";
484 echo $choice->maxanswers[$optionid];
485 } else {
486 if (isset($columncount[$optionid])) {
487 echo $columncount[$optionid];
490 echo "</td>";
491 $count++;
493 echo "</tr>";
495 /// Print "Select all" etc.
496 if ($viewresponses and has_capability('mod/choice:deleteresponses',$context)) {
497 echo '<tr><td></td><td>';
498 echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectall').'</a> / ';
499 echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('deselectall').'</a> ';
500 echo '&nbsp;&nbsp;';
501 echo html_writer::label(get_string('withselected', 'choice'), 'menuaction');
502 echo html_writer::select(array('delete' => get_string('delete')), 'action', '', array(''=>get_string('withselectedusers')), array('id'=>'menuaction', 'class' => 'autosubmit'));
503 $PAGE->requires->yui_module('moodle-core-formautosubmit',
504 'M.core.init_formautosubmit',
505 array(array('selectid' => 'menuaction'))
507 echo '<noscript id="noscriptmenuaction" style="display:inline">';
508 echo '<div>';
509 echo '<input type="submit" value="'.get_string('go').'" /></div></noscript>';
510 echo '</td><td></td></tr>';
513 echo "</table></div>";
514 if ($viewresponses) {
515 echo "</form></div>";
517 break;
519 return $display;
523 * @global object
524 * @param array $attemptids
525 * @param object $choice Choice main table row
526 * @param object $cm Course-module object
527 * @param object $course Course object
528 * @return bool
530 function choice_delete_responses($attemptids, $choice, $cm, $course) {
531 global $DB, $CFG;
532 require_once($CFG->libdir.'/completionlib.php');
534 if(!is_array($attemptids) || empty($attemptids)) {
535 return false;
538 foreach($attemptids as $num => $attemptid) {
539 if(empty($attemptid)) {
540 unset($attemptids[$num]);
544 $completion = new completion_info($course);
545 foreach($attemptids as $attemptid) {
546 if ($todelete = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $attemptid))) {
547 $DB->delete_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $attemptid));
548 // Update completion state
549 if ($completion->is_enabled($cm) && $choice->completionsubmit) {
550 $completion->update_state($cm, COMPLETION_INCOMPLETE, $attemptid);
554 return true;
559 * Given an ID of an instance of this module,
560 * this function will permanently delete the instance
561 * and any data that depends on it.
563 * @global object
564 * @param int $id
565 * @return bool
567 function choice_delete_instance($id) {
568 global $DB;
570 if (! $choice = $DB->get_record("choice", array("id"=>"$id"))) {
571 return false;
574 $result = true;
576 if (! $DB->delete_records("choice_answers", array("choiceid"=>"$choice->id"))) {
577 $result = false;
580 if (! $DB->delete_records("choice_options", array("choiceid"=>"$choice->id"))) {
581 $result = false;
584 if (! $DB->delete_records("choice", array("id"=>"$choice->id"))) {
585 $result = false;
588 return $result;
592 * Returns text string which is the answer that matches the id
594 * @global object
595 * @param object $choice
596 * @param int $id
597 * @return string
599 function choice_get_option_text($choice, $id) {
600 global $DB;
602 if ($result = $DB->get_record("choice_options", array("id" => $id))) {
603 return $result->text;
604 } else {
605 return get_string("notanswered", "choice");
610 * Gets a full choice record
612 * @global object
613 * @param int $choiceid
614 * @return object|bool The choice or false
616 function choice_get_choice($choiceid) {
617 global $DB;
619 if ($choice = $DB->get_record("choice", array("id" => $choiceid))) {
620 if ($options = $DB->get_records("choice_options", array("choiceid" => $choiceid), "id")) {
621 foreach ($options as $option) {
622 $choice->option[$option->id] = $option->text;
623 $choice->maxanswers[$option->id] = $option->maxanswers;
625 return $choice;
628 return false;
632 * @return array
634 function choice_get_view_actions() {
635 return array('view','view all','report');
639 * @return array
641 function choice_get_post_actions() {
642 return array('choose','choose again');
647 * Implementation of the function for printing the form elements that control
648 * whether the course reset functionality affects the choice.
650 * @param object $mform form passed by reference
652 function choice_reset_course_form_definition(&$mform) {
653 $mform->addElement('header', 'choiceheader', get_string('modulenameplural', 'choice'));
654 $mform->addElement('advcheckbox', 'reset_choice', get_string('removeresponses','choice'));
658 * Course reset form defaults.
660 * @return array
662 function choice_reset_course_form_defaults($course) {
663 return array('reset_choice'=>1);
667 * Actual implementation of the reset course functionality, delete all the
668 * choice responses for course $data->courseid.
670 * @global object
671 * @global object
672 * @param object $data the data submitted from the reset course.
673 * @return array status array
675 function choice_reset_userdata($data) {
676 global $CFG, $DB;
678 $componentstr = get_string('modulenameplural', 'choice');
679 $status = array();
681 if (!empty($data->reset_choice)) {
682 $choicessql = "SELECT ch.id
683 FROM {choice} ch
684 WHERE ch.course=?";
686 $DB->delete_records_select('choice_answers', "choiceid IN ($choicessql)", array($data->courseid));
687 $status[] = array('component'=>$componentstr, 'item'=>get_string('removeresponses', 'choice'), 'error'=>false);
690 /// updating dates - shift may be negative too
691 if ($data->timeshift) {
692 shift_course_mod_dates('choice', array('timeopen', 'timeclose'), $data->timeshift, $data->courseid);
693 $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
696 return $status;
700 * @global object
701 * @global object
702 * @global object
703 * @uses CONTEXT_MODULE
704 * @param object $choice
705 * @param object $cm
706 * @param int $groupmode
707 * @return array
709 function choice_get_response_data($choice, $cm, $groupmode) {
710 global $CFG, $USER, $DB;
712 $context = context_module::instance($cm->id);
714 /// Get the current group
715 if ($groupmode > 0) {
716 $currentgroup = groups_get_activity_group($cm);
717 } else {
718 $currentgroup = 0;
721 /// Initialise the returned array, which is a matrix: $allresponses[responseid][userid] = responseobject
722 $allresponses = array();
724 /// First get all the users who have access here
725 /// To start with we assume they are all "unanswered" then move them later
726 $allresponses[0] = get_enrolled_users($context, 'mod/choice:choose', $currentgroup, user_picture::fields('u', array('idnumber')));
728 /// Get all the recorded responses for this choice
729 $rawresponses = $DB->get_records('choice_answers', array('choiceid' => $choice->id));
731 /// Use the responses to move users into the correct column
733 if ($rawresponses) {
734 foreach ($rawresponses as $response) {
735 if (isset($allresponses[0][$response->userid])) { // This person is enrolled and in correct group
736 $allresponses[0][$response->userid]->timemodified = $response->timemodified;
737 $allresponses[$response->optionid][$response->userid] = clone($allresponses[0][$response->userid]);
738 unset($allresponses[0][$response->userid]); // Remove from unanswered column
742 return $allresponses;
746 * Returns all other caps used in module
748 * @return array
750 function choice_get_extra_capabilities() {
751 return array('moodle/site:accessallgroups');
755 * @uses FEATURE_GROUPS
756 * @uses FEATURE_GROUPINGS
757 * @uses FEATURE_GROUPMEMBERSONLY
758 * @uses FEATURE_MOD_INTRO
759 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
760 * @uses FEATURE_GRADE_HAS_GRADE
761 * @uses FEATURE_GRADE_OUTCOMES
762 * @param string $feature FEATURE_xx constant for requested feature
763 * @return mixed True if module supports feature, null if doesn't know
765 function choice_supports($feature) {
766 switch($feature) {
767 case FEATURE_GROUPS: return true;
768 case FEATURE_GROUPINGS: return true;
769 case FEATURE_GROUPMEMBERSONLY: return true;
770 case FEATURE_MOD_INTRO: return true;
771 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
772 case FEATURE_COMPLETION_HAS_RULES: return true;
773 case FEATURE_GRADE_HAS_GRADE: return false;
774 case FEATURE_GRADE_OUTCOMES: return false;
775 case FEATURE_BACKUP_MOODLE2: return true;
776 case FEATURE_SHOW_DESCRIPTION: return true;
778 default: return null;
783 * Adds module specific settings to the settings block
785 * @param settings_navigation $settings The settings navigation object
786 * @param navigation_node $choicenode The node to add module settings to
788 function choice_extend_settings_navigation(settings_navigation $settings, navigation_node $choicenode) {
789 global $PAGE;
791 if (has_capability('mod/choice:readresponses', $PAGE->cm->context)) {
793 $groupmode = groups_get_activity_groupmode($PAGE->cm);
794 if ($groupmode) {
795 groups_get_activity_group($PAGE->cm, true);
797 // We only actually need the choice id here
798 $choice = new stdClass;
799 $choice->id = $PAGE->cm->instance;
800 $allresponses = choice_get_response_data($choice, $PAGE->cm, $groupmode); // Big function, approx 6 SQL calls per user
802 $responsecount =0;
803 foreach($allresponses as $optionid => $userlist) {
804 if ($optionid) {
805 $responsecount += count($userlist);
808 $choicenode->add(get_string("viewallresponses", "choice", $responsecount), new moodle_url('/mod/choice/report.php', array('id'=>$PAGE->cm->id)));
813 * Obtains the automatic completion state for this choice based on any conditions
814 * in forum settings.
816 * @param object $course Course
817 * @param object $cm Course-module
818 * @param int $userid User ID
819 * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
820 * @return bool True if completed, false if not, $type if conditions not set.
822 function choice_get_completion_state($course, $cm, $userid, $type) {
823 global $CFG,$DB;
825 // Get choice details
826 $choice = $DB->get_record('choice', array('id'=>$cm->instance), '*',
827 MUST_EXIST);
829 // If completion option is enabled, evaluate it and return true/false
830 if($choice->completionsubmit) {
831 return $DB->record_exists('choice_answers', array(
832 'choiceid'=>$choice->id, 'userid'=>$userid));
833 } else {
834 // Completion option is not enabled so just return $type
835 return $type;
840 * Return a list of page types
841 * @param string $pagetype current page type
842 * @param stdClass $parentcontext Block's parent context
843 * @param stdClass $currentcontext Current context of block
845 function choice_page_type_list($pagetype, $parentcontext, $currentcontext) {
846 $module_pagetype = array('mod-choice-*'=>get_string('page-mod-choice-x', 'choice'));
847 return $module_pagetype;