Merge branch 'm21_MDL-28017' of git://github.com/danmarsden/moodle into MOODLE_21_STABLE
[moodle.git] / mod / choice / index.php
blobe63b876359317f3ed9e5117fe750a784130cef8a
1 <?php
3 require_once("../../config.php");
4 require_once("lib.php");
6 $id = required_param('id',PARAM_INT); // course
8 $PAGE->set_url('/mod/choice/index.php', array('id'=>$id));
10 if (!$course = $DB->get_record('course', array('id'=>$id))) {
11 print_error('invalidcourseid');
14 require_course_login($course);
15 $PAGE->set_pagelayout('incourse');
17 add_to_log($course->id, "choice", "view all", "index.php?id=$course->id", "");
19 $strchoice = get_string("modulename", "choice");
20 $strchoices = get_string("modulenameplural", "choice");
21 $strsectionname = get_string('sectionname', 'format_'.$course->format);
22 $PAGE->set_title($strchoices);
23 $PAGE->set_heading($course->fullname);
24 $PAGE->navbar->add($strchoices);
25 echo $OUTPUT->header();
27 if (! $choices = get_all_instances_in_course("choice", $course)) {
28 notice(get_string('thereareno', 'moodle', $strchoices), "../../course/view.php?id=$course->id");
31 $usesections = course_format_uses_sections($course->format);
32 if ($usesections) {
33 $sections = get_all_sections($course->id);
36 $sql = "SELECT cha.*
37 FROM {choice} ch, {choice_answers} cha
38 WHERE cha.choiceid = ch.id AND
39 ch.course = ? AND cha.userid = ?";
41 $answers = array () ;
42 if (isloggedin() and !isguestuser() and $allanswers = $DB->get_records_sql($sql, array($course->id, $USER->id))) {
43 foreach ($allanswers as $aa) {
44 $answers[$aa->choiceid] = $aa;
46 unset($allanswers);
50 $timenow = time();
52 $table = new html_table();
54 if ($usesections) {
55 $table->head = array ($strsectionname, get_string("question"), get_string("answer"));
56 $table->align = array ("center", "left", "left");
57 } else {
58 $table->head = array (get_string("question"), get_string("answer"));
59 $table->align = array ("left", "left");
62 $currentsection = "";
64 foreach ($choices as $choice) {
65 if (!empty($answers[$choice->id])) {
66 $answer = $answers[$choice->id];
67 } else {
68 $answer = "";
70 if (!empty($answer->optionid)) {
71 $aa = format_string(choice_get_option_text($choice, $answer->optionid));
72 } else {
73 $aa = "";
75 if ($usesections) {
76 $printsection = "";
77 if ($choice->section !== $currentsection) {
78 if ($choice->section) {
79 $printsection = get_section_name($course, $sections[$choice->section]);
81 if ($currentsection !== "") {
82 $table->data[] = 'hr';
84 $currentsection = $choice->section;
88 //Calculate the href
89 if (!$choice->visible) {
90 //Show dimmed if the mod is hidden
91 $tt_href = "<a class=\"dimmed\" href=\"view.php?id=$choice->coursemodule\">".format_string($choice->name,true)."</a>";
92 } else {
93 //Show normal if the mod is visible
94 $tt_href = "<a href=\"view.php?id=$choice->coursemodule\">".format_string($choice->name,true)."</a>";
96 if ($usesections) {
97 $table->data[] = array ($printsection, $tt_href, $aa);
98 } else {
99 $table->data[] = array ($tt_href, $aa);
102 echo "<br />";
103 echo html_writer::table($table);
105 echo $OUTPUT->footer();