Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  
   3      require_once("../../config.php");
   4      require_once ("lib.php");
   5  
   6      $id = required_param('id',PARAM_INT);   // course
   7  
   8      $PAGE->set_url('/mod/choice/index.php', array('id'=>$id));
   9  
  10      if (!$course = $DB->get_record('course', array('id'=>$id))) {
  11          throw new \moodle_exception('invalidcourseid');
  12      }
  13  
  14      require_course_login($course);
  15      $PAGE->set_pagelayout('incourse');
  16  
  17      $eventdata = array('context' => context_course::instance($id));
  18      $event = \mod_choice\event\course_module_instance_list_viewed::create($eventdata);
  19      $event->add_record_snapshot('course', $course);
  20      $event->trigger();
  21  
  22      $strchoice = get_string("modulename", "choice");
  23      $strchoices = get_string("modulenameplural", "choice");
  24      $PAGE->set_title($strchoices);
  25      $PAGE->set_heading($course->fullname);
  26      $PAGE->navbar->add($strchoices);
  27      echo $OUTPUT->header();
  28  
  29      if (! $choices = get_all_instances_in_course("choice", $course)) {
  30          notice(get_string('thereareno', 'moodle', $strchoices), "../../course/view.php?id=$course->id");
  31      }
  32  
  33      $usesections = course_format_uses_sections($course->format);
  34  
  35      $sql = "SELECT cha.*
  36                FROM {choice} ch, {choice_answers} cha
  37               WHERE cha.choiceid = ch.id AND
  38                     ch.course = ? AND cha.userid = ?";
  39  
  40      $answers = array () ;
  41      if (isloggedin() and !isguestuser() and $allanswers = $DB->get_records_sql($sql, array($course->id, $USER->id))) {
  42          foreach ($allanswers as $aa) {
  43              $answers[$aa->choiceid] = $aa;
  44          }
  45          unset($allanswers);
  46      }
  47  
  48  
  49      $timenow = time();
  50  
  51      $table = new html_table();
  52  
  53      if ($usesections) {
  54          $strsectionname = get_string('sectionname', 'format_'.$course->format);
  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");
  60      }
  61  
  62      $currentsection = "";
  63  
  64      foreach ($choices as $choice) {
  65          if (!empty($answers[$choice->id])) {
  66              $answer = $answers[$choice->id];
  67          } else {
  68              $answer = "";
  69          }
  70          if (!empty($answer->optionid)) {
  71              $aa = format_string(choice_get_option_text($choice, $answer->optionid));
  72          } else {
  73              $aa = "";
  74          }
  75          if ($usesections) {
  76              $printsection = "";
  77              if ($choice->section !== $currentsection) {
  78                  if ($choice->section) {
  79                      $printsection = get_section_name($course, $choice->section);
  80                  }
  81                  if ($currentsection !== "") {
  82                      $table->data[] = 'hr';
  83                  }
  84                  $currentsection = $choice->section;
  85              }
  86          }
  87  
  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>";
  95          }
  96          if ($usesections) {
  97              $table->data[] = array ($printsection, $tt_href, $aa);
  98          } else {
  99              $table->data[] = array ($tt_href, $aa);
 100          }
 101      }
 102      echo "<br />";
 103      echo html_writer::table($table);
 104  
 105      echo $OUTPUT->footer();
 106  
 107