3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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 /////////////////////////////////////////////////////////
64 * @param object $course
67 * @param object $choice
70 function choice_user_outline($course, $user, $mod, $choice) {
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
;
83 * @param object $course
86 * @param object $choice
89 function choice_user_complete($course, $user, $mod, $choice) {
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
));
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.
108 * @param object $choice
111 function choice_add_instance($choice) {
114 $choice->timemodified
= time();
116 if (empty($choice->timerestrict
)) {
117 $choice->timeopen
= 0;
118 $choice->timeclose
= 0;
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);
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.
146 * @param object $choice
149 function choice_update_instance($choice) {
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
));
179 if (isset($value) && $value <> '') {
180 $DB->insert_record("choice_options", $option);
185 return $DB->update_record('choice', $choice);
191 * @param object $choice
192 * @param object $user
193 * @param object $coursemodule
194 * @param array $allresponses
197 function choice_prepare_options($choice, $user, $coursemodule, $allresponses) {
200 $cdisplay = array('options'=>array());
202 $cdisplay['limitanswers'] = true;
203 $context = get_context_instance(CONTEXT_MODULE
, $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]);
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;
240 * @param int $formanswer
241 * @param object $choice
243 * @param object $course Course object
246 function choice_user_submit_response($formanswer, $choice, $userid, $course, $cm) {
248 require_once($CFG->libdir
.'/completionlib.php');
250 $current = $DB->get_record('choice_answers', array('choiceid' => $choice->id
, 'userid' => $userid));
251 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
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);
262 // If groups are being used, retrieve responses only for users in
265 $answers = $DB->get_records_sql("
270 INNER JOIN {groups_members} gm ON ca.userid=gm.userid
273 AND gm.groupid=?", array($formanswer, $currentgroup));
275 // Groups are not used, retrieve all answers for this option ID
276 $answers = $DB->get_records("choice_answers", array("optionid" => $formanswer));
280 foreach ($answers as $a) { //only return enrolled users.
281 if (is_enrolled($context, $a->userid
, 'mod/choice:choose')) {
286 $maxans = $choice->maxanswers
[$formanswer];
289 if (!($choice->limitanswers
&& ($countanswers >= $maxans) )) {
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
);
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
);
313 if (!($current->optionid
==$formanswer)) { //check to see if current choice already selected - if not display error
314 print_error('choicefull', 'choice');
322 * @return void Output is echo'd
324 function choice_show_reportlink($user, $cm) {
326 foreach($user as $optionid => $userlist) {
328 $responsecount +
= count($userlist);
332 echo '<div class="reportlink">';
333 echo "<a href=\"report.php?id=$cm->id\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
339 * @param object $choice
340 * @param object $course
341 * @param object $coursemodule
342 * @param array $allresponses
344 * * @param bool $allresponses
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();
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 = get_context_instance(CONTEXT_MODULE
, $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"));
382 $totalresponsecount = 0;
383 foreach ($allresponses as $optionid => $userlist) {
384 if ($choice->showunanswered ||
$optionid) {
385 $totalresponsecount +
= count($userlist);
389 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
391 $hascapfullnames = has_capability('moodle/site:viewfullnames', $context);
393 $viewresponses = has_capability('mod/choice:readresponses', $context);
394 switch ($forcepublish) {
395 case CHOICE_PUBLISH_NAMES
:
396 echo '<div id="tablecontainer">';
397 if ($viewresponses) {
398 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);">';
400 echo '<input type="hidden" name="id" value="'.$cm->id
.'" />';
401 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
402 echo '<input type="hidden" name="mode" value="overview" />';
405 echo "<table cellpadding=\"5\" cellspacing=\"10\" class=\"results names\">";
408 $columncount = array(); // number of votes in each column
409 if ($choice->showunanswered
) {
411 echo "<th class=\"col0 header\" scope=\"col\">";
412 print_string('notanswered', 'choice');
416 foreach ($choice->option
as $optionid => $optiontext) {
417 $columncount[$optionid] = 0; // init counters
418 echo "<th class=\"col$count header\" scope=\"col\">";
419 echo format_string($optiontext);
425 if ($choice->showunanswered
) {
426 echo "<td class=\"col$count data\" >";
427 // added empty row so that when the next iteration is empty,
428 // we do not get <table></table> error from w3c validator
430 echo "<table class=\"choiceresponse\"><tr><td></td></tr>";
431 if (!empty($allresponses[0])) {
432 foreach ($allresponses[0] as $user) {
434 echo "<td class=\"picture\">";
435 echo $OUTPUT->user_picture($user, array('courseid'=>$course->id
));
436 echo "</td><td class=\"fullname\">";
437 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">";
438 echo fullname($user, $hascapfullnames);
443 echo "</table></td>";
446 foreach ($choice->option
as $optionid => $optiontext) {
447 echo '<td class="col'.$count.' data" >';
449 // added empty row so that when the next iteration is empty,
450 // we do not get <table></table> error from w3c validator
452 echo '<table class="choiceresponse"><tr><td></td></tr>';
453 if (isset($allresponses[$optionid])) {
454 foreach ($allresponses[$optionid] as $user) {
455 $columncount[$optionid] +
= 1;
456 echo '<tr><td class="attemptcell">';
457 if ($viewresponses and has_capability('mod/choice:deleteresponses',$context)) {
458 echo '<input type="checkbox" name="attemptid[]" value="'. $user->id
. '" />';
460 echo '</td><td class="picture">';
461 echo $OUTPUT->user_picture($user, array('courseid'=>$course->id
));
462 echo '</td><td class="fullname">';
463 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">";
464 echo fullname($user, $hascapfullnames);
470 echo '</table></td>';
475 if ($choice->showunanswered
) {
479 foreach ($choice->option
as $optionid => $optiontext) {
480 echo "<td align=\"center\" class=\"col$count count\">";
481 if ($choice->limitanswers
) {
482 echo get_string("taken", "choice").":";
483 echo $columncount[$optionid];
485 echo get_string("limit", "choice").":";
486 echo $choice->maxanswers
[$optionid];
488 if (isset($columncount[$optionid])) {
489 echo $columncount[$optionid];
497 /// Print "Select all" etc.
498 if ($viewresponses and has_capability('mod/choice:deleteresponses',$context)) {
499 echo '<tr><td></td><td>';
500 echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectall').'</a> / ';
501 echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('deselectall').'</a> ';
503 echo html_writer
::label(get_string('withselected', 'choice'), 'menuaction');
504 echo html_writer
::select(array('delete' => get_string('delete')), 'action', '', array(''=>get_string('withselectedusers')), array('id'=>'menuaction'));
505 $PAGE->requires
->js_init_call('M.util.init_select_autosubmit', array('attemptsform', 'menuaction', ''));
506 echo '<noscript id="noscriptmenuaction" style="display:inline">';
508 echo '<input type="submit" value="'.get_string('go').'" /></div></noscript>';
509 echo '</td><td></td></tr>';
512 echo "</table></div>";
513 if ($viewresponses) {
514 echo "</form></div>";
523 * @param array $attemptids
524 * @param object $choice Choice main table row
525 * @param object $cm Course-module object
526 * @param object $course Course object
529 function choice_delete_responses($attemptids, $choice, $cm, $course) {
531 require_once($CFG->libdir
.'/completionlib.php');
533 if(!is_array($attemptids) ||
empty($attemptids)) {
537 foreach($attemptids as $num => $attemptid) {
538 if(empty($attemptid)) {
539 unset($attemptids[$num]);
543 $completion = new completion_info($course);
544 foreach($attemptids as $attemptid) {
545 if ($todelete = $DB->get_record('choice_answers', array('choiceid' => $choice->id
, 'userid' => $attemptid))) {
546 $DB->delete_records('choice_answers', array('choiceid' => $choice->id
, 'userid' => $attemptid));
547 // Update completion state
548 if ($completion->is_enabled($cm) && $choice->completionsubmit
) {
549 $completion->update_state($cm, COMPLETION_INCOMPLETE
, $attemptid);
558 * Given an ID of an instance of this module,
559 * this function will permanently delete the instance
560 * and any data that depends on it.
566 function choice_delete_instance($id) {
569 if (! $choice = $DB->get_record("choice", array("id"=>"$id"))) {
575 if (! $DB->delete_records("choice_answers", array("choiceid"=>"$choice->id"))) {
579 if (! $DB->delete_records("choice_options", array("choiceid"=>"$choice->id"))) {
583 if (! $DB->delete_records("choice", array("id"=>"$choice->id"))) {
591 * Returns the users with data in one choice
592 * (users with records in choice_responses, students)
594 * @param int $choiceid
597 function choice_get_participants($choiceid) {
601 $students = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
604 WHERE a.choiceid = ? AND
605 u.id = a.userid", array($choiceid));
607 //Return students array (it contains an array of unique users)
612 * Returns text string which is the answer that matches the id
615 * @param object $choice
619 function choice_get_option_text($choice, $id) {
622 if ($result = $DB->get_record("choice_options", array("id" => $id))) {
623 return $result->text
;
625 return get_string("notanswered", "choice");
630 * Gets a full choice record
633 * @param int $choiceid
634 * @return object|bool The choice or false
636 function choice_get_choice($choiceid) {
639 if ($choice = $DB->get_record("choice", array("id" => $choiceid))) {
640 if ($options = $DB->get_records("choice_options", array("choiceid" => $choiceid), "id")) {
641 foreach ($options as $option) {
642 $choice->option
[$option->id
] = $option->text
;
643 $choice->maxanswers
[$option->id
] = $option->maxanswers
;
654 function choice_get_view_actions() {
655 return array('view','view all','report');
661 function choice_get_post_actions() {
662 return array('choose','choose again');
667 * Implementation of the function for printing the form elements that control
668 * whether the course reset functionality affects the choice.
670 * @param object $mform form passed by reference
672 function choice_reset_course_form_definition(&$mform) {
673 $mform->addElement('header', 'choiceheader', get_string('modulenameplural', 'choice'));
674 $mform->addElement('advcheckbox', 'reset_choice', get_string('removeresponses','choice'));
678 * Course reset form defaults.
682 function choice_reset_course_form_defaults($course) {
683 return array('reset_choice'=>1);
687 * Actual implementation of the reset course functionality, delete all the
688 * choice responses for course $data->courseid.
692 * @param object $data the data submitted from the reset course.
693 * @return array status array
695 function choice_reset_userdata($data) {
698 $componentstr = get_string('modulenameplural', 'choice');
701 if (!empty($data->reset_choice
)) {
702 $choicessql = "SELECT ch.id
706 $DB->delete_records_select('choice_answers', "choiceid IN ($choicessql)", array($data->courseid
));
707 $status[] = array('component'=>$componentstr, 'item'=>get_string('removeresponses', 'choice'), 'error'=>false);
710 /// updating dates - shift may be negative too
711 if ($data->timeshift
) {
712 shift_course_mod_dates('choice', array('timeopen', 'timeclose'), $data->timeshift
, $data->courseid
);
713 $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
723 * @uses CONTEXT_MODULE
724 * @param object $choice
726 * @param int $groupmode
729 function choice_get_response_data($choice, $cm, $groupmode) {
730 global $CFG, $USER, $DB;
732 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
734 /// Get the current group
735 if ($groupmode > 0) {
736 $currentgroup = groups_get_activity_group($cm);
741 /// Initialise the returned array, which is a matrix: $allresponses[responseid][userid] = responseobject
742 $allresponses = array();
744 /// First get all the users who have access here
745 /// To start with we assume they are all "unanswered" then move them later
746 $allresponses[0] = get_enrolled_users($context, 'mod/choice:choose', $currentgroup, user_picture
::fields('u', array('idnumber')), 'u.lastname ASC,u.firstname ASC');
748 /// Get all the recorded responses for this choice
749 $rawresponses = $DB->get_records('choice_answers', array('choiceid' => $choice->id
));
751 /// Use the responses to move users into the correct column
754 foreach ($rawresponses as $response) {
755 if (isset($allresponses[0][$response->userid
])) { // This person is enrolled and in correct group
756 $allresponses[0][$response->userid
]->timemodified
= $response->timemodified
;
757 $allresponses[$response->optionid
][$response->userid
] = clone($allresponses[0][$response->userid
]);
758 unset($allresponses[0][$response->userid
]); // Remove from unanswered column
762 return $allresponses;
766 * Returns all other caps used in module
770 function choice_get_extra_capabilities() {
771 return array('moodle/site:accessallgroups');
775 * @uses FEATURE_GROUPS
776 * @uses FEATURE_GROUPINGS
777 * @uses FEATURE_GROUPMEMBERSONLY
778 * @uses FEATURE_MOD_INTRO
779 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
780 * @uses FEATURE_GRADE_HAS_GRADE
781 * @uses FEATURE_GRADE_OUTCOMES
782 * @param string $feature FEATURE_xx constant for requested feature
783 * @return mixed True if module supports feature, null if doesn't know
785 function choice_supports($feature) {
787 case FEATURE_GROUPS
: return true;
788 case FEATURE_GROUPINGS
: return true;
789 case FEATURE_GROUPMEMBERSONLY
: return true;
790 case FEATURE_MOD_INTRO
: return true;
791 case FEATURE_COMPLETION_TRACKS_VIEWS
: return true;
792 case FEATURE_COMPLETION_HAS_RULES
: return true;
793 case FEATURE_GRADE_HAS_GRADE
: return false;
794 case FEATURE_GRADE_OUTCOMES
: return false;
795 case FEATURE_BACKUP_MOODLE2
: return true;
797 default: return null;
802 * Adds module specific settings to the settings block
804 * @param settings_navigation $settings The settings navigation object
805 * @param navigation_node $choicenode The node to add module settings to
807 function choice_extend_settings_navigation(settings_navigation
$settings, navigation_node
$choicenode) {
810 if (has_capability('mod/choice:readresponses', $PAGE->cm
->context
)) {
812 $groupmode = groups_get_activity_groupmode($PAGE->cm
);
814 groups_get_activity_group($PAGE->cm
, true);
816 // We only actually need the choice id here
817 $choice = new stdClass
;
818 $choice->id
= $PAGE->cm
->instance
;
819 $allresponses = choice_get_response_data($choice, $PAGE->cm
, $groupmode); // Big function, approx 6 SQL calls per user
822 foreach($allresponses as $optionid => $userlist) {
824 $responsecount +
= count($userlist);
827 $choicenode->add(get_string("viewallresponses", "choice", $responsecount), new moodle_url('/mod/choice/report.php', array('id'=>$PAGE->cm
->id
)));
832 * Obtains the automatic completion state for this choice based on any conditions
835 * @param object $course Course
836 * @param object $cm Course-module
837 * @param int $userid User ID
838 * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
839 * @return bool True if completed, false if not, $type if conditions not set.
841 function choice_get_completion_state($course, $cm, $userid, $type) {
844 // Get choice details
845 $choice = $DB->get_record('choice', array('id'=>$cm->instance
), '*',
848 // If completion option is enabled, evaluate it and return true/false
849 if($choice->completionsubmit
) {
850 return $DB->record_exists('choice_answers', array(
851 'choiceid'=>$choice->id
, 'userid'=>$userid));
853 // Completion option is not enabled so just return $type