[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/choice/ -> lib.php (source)

   1  <?php
   2  
   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.
  14  //
  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/>.
  17  
  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
  22   */
  23  
  24  /** @global int $CHOICE_COLUMN_HEIGHT */
  25  global $CHOICE_COLUMN_HEIGHT;
  26  $CHOICE_COLUMN_HEIGHT = 300;
  27  
  28  /** @global int $CHOICE_COLUMN_WIDTH */
  29  global $CHOICE_COLUMN_WIDTH;
  30  $CHOICE_COLUMN_WIDTH = 300;
  31  
  32  define('CHOICE_PUBLISH_ANONYMOUS', '0');
  33  define('CHOICE_PUBLISH_NAMES',     '1');
  34  
  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');
  39  
  40  define('CHOICE_DISPLAY_HORIZONTAL',  '0');
  41  define('CHOICE_DISPLAY_VERTICAL',    '1');
  42  
  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'));
  47  
  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'));
  54  
  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'));
  59  
  60  /// Standard functions /////////////////////////////////////////////////////////
  61  
  62  /**
  63   * @global object
  64   * @param object $course
  65   * @param object $user
  66   * @param object $mod
  67   * @param object $choice
  68   * @return object|null
  69   */
  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;
  77      }
  78      return NULL;
  79  }
  80  
  81  /**
  82   * @global object
  83   * @param object $course
  84   * @param object $user
  85   * @param object $mod
  86   * @param object $choice
  87   * @return string|void
  88   */
  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");
  98      }
  99  }
 100  
 101  /**
 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.
 106   *
 107   * @global object
 108   * @param object $choice
 109   * @return int
 110   */
 111  function choice_add_instance($choice) {
 112      global $DB;
 113  
 114      $choice->timemodified = time();
 115  
 116      if (empty($choice->timerestrict)) {
 117          $choice->timeopen = 0;
 118          $choice->timeclose = 0;
 119      }
 120  
 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];
 131              }
 132              $option->timemodified = time();
 133              $DB->insert_record("choice_options", $option);
 134          }
 135      }
 136  
 137      return $choice->id;
 138  }
 139  
 140  /**
 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.
 144   *
 145   * @global object
 146   * @param object $choice
 147   * @return bool
 148   */
 149  function choice_update_instance($choice) {
 150      global $DB;
 151  
 152      $choice->id = $choice->instance;
 153      $choice->timemodified = time();
 154  
 155  
 156      if (empty($choice->timerestrict)) {
 157          $choice->timeopen = 0;
 158          $choice->timeclose = 0;
 159      }
 160  
 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];
 169          }
 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));
 177              }
 178          } else {
 179              if (isset($value) && $value <> '') {
 180                  $DB->insert_record("choice_options", $option);
 181              }
 182          }
 183      }
 184  
 185      return $DB->update_record('choice', $choice);
 186  
 187  }
 188  
 189  /**
 190   * @global object
 191   * @param object $choice
 192   * @param object $user
 193   * @param object $coursemodule
 194   * @param array $allresponses
 195   * @return array
 196   */
 197  function choice_prepare_options($choice, $user, $coursemodule, $allresponses) {
 198      global $DB;
 199  
 200      $cdisplay = array('options'=>array());
 201  
 202      $cdisplay['limitanswers'] = true;
 203      $context = context_module::instance($coursemodule->id);
 204  
 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 = format_string($text);
 211              $option->maxanswers = $choice->maxanswers[$optionid];
 212              $option->displaylayout = $choice->display;
 213  
 214              if (isset($allresponses[$optionid])) {
 215                  $option->countanswers = count($allresponses[$optionid]);
 216              } else {
 217                  $option->countanswers = 0;
 218              }
 219              if ($DB->record_exists('choice_answers', array('choiceid' => $choice->id, 'userid' => $user->id, 'optionid' => $optionid))) {
 220                  $option->attributes->checked = true;
 221              }
 222              if ( $choice->limitanswers && ($option->countanswers >= $option->maxanswers) && empty($option->attributes->checked)) {
 223                  $option->attributes->disabled = true;
 224              }
 225              $cdisplay['options'][] = $option;
 226          }
 227      }
 228  
 229      $cdisplay['hascapability'] = is_enrolled($context, NULL, 'mod/choice:choose'); //only enrolled users are allowed to make a choice
 230  
 231      if ($choice->allowupdate && $DB->record_exists('choice_answers', array('choiceid'=> $choice->id, 'userid'=> $user->id))) {
 232          $cdisplay['allowupdate'] = true;
 233      }
 234  
 235      return $cdisplay;
 236  }
 237  
 238  /**
 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
 245   */
 246  function choice_user_submit_response($formanswer, $choice, $userid, $course, $cm) {
 247      global $DB, $CFG;
 248      require_once($CFG->libdir.'/completionlib.php');
 249  
 250      if (empty($formanswer)) {
 251          print_error('atleastoneoption', 'choice');
 252      }
 253  
 254      if (is_array($formanswer)) {
 255          if (!$choice->allowmultiple) {
 256              print_error('multiplenotallowederror', 'choice');
 257          }
 258          $formanswers = $formanswer;
 259      } else {
 260          $formanswers = array($formanswer);
 261      }
 262  
 263      $current = $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $userid));
 264      $context = context_module::instance($cm->id);
 265  
 266      $choicesexceeded = false;
 267      $countanswers = array();
 268      foreach ($formanswers as $val) {
 269          $countanswers[$val] = 0;
 270      }
 271      if($choice->limitanswers) {
 272          // Find out whether groups are being used and enabled
 273          if (groups_get_activity_groupmode($cm) > 0) {
 274              $currentgroup = groups_get_activity_group($cm);
 275          } else {
 276              $currentgroup = 0;
 277          }
 278  
 279          list ($insql, $params) = $DB->get_in_or_equal($formanswers, SQL_PARAMS_NAMED);
 280  
 281          if($currentgroup) {
 282              // If groups are being used, retrieve responses only for users in
 283              // current group
 284              global $CFG;
 285  
 286              $params['groupid'] = $currentgroup;
 287              $sql = "SELECT ca.*
 288                        FROM {choice_answers} ca
 289                  INNER JOIN {groups_members} gm ON ca.userid=gm.userid
 290                       WHERE optionid $insql
 291                         AND gm.groupid= :groupid";
 292          } else {
 293              // Groups are not used, retrieve all answers for this option ID
 294              $sql = "SELECT ca.*
 295                        FROM {choice_answers} ca
 296                       WHERE optionid $insql";
 297          }
 298  
 299          $answers = $DB->get_records_sql($sql, $params);
 300          if ($answers) {
 301              foreach ($answers as $a) { //only return enrolled users.
 302                  if (is_enrolled($context, $a->userid, 'mod/choice:choose')) {
 303                      $countanswers[$a->optionid]++;
 304                  }
 305              }
 306          }
 307          foreach ($countanswers as $opt => $count) {
 308              if ($count > $choice->maxanswers[$opt]) {
 309                  $choicesexceeded = true;
 310                  break;
 311              }
 312          }
 313      }
 314  
 315      // Check the user hasn't exceeded the maximum selections for the choice(s) they have selected.
 316      if (!($choice->limitanswers && $choicesexceeded)) {
 317          $answersnapshots = array();
 318          if ($current) {
 319  
 320              $existingchoices = array();
 321              foreach ($current as $c) {
 322                  if (in_array($c->optionid, $formanswers)) {
 323                      $existingchoices[] = $c->optionid;
 324                      $DB->set_field('choice_answers', 'timemodified', time(), array('id' => $c->id));
 325                      $answersnapshots[] = $c;
 326                  } else {
 327                      $DB->delete_records('choice_answers', array('id' => $c->id));
 328                  }
 329              }
 330  
 331              // Add new ones.
 332              foreach ($formanswers as $f) {
 333                  if (!in_array($f, $existingchoices)) {
 334                      $newanswer = new stdClass();
 335                      $newanswer->optionid = $f;
 336                      $newanswer->choiceid = $choice->id;
 337                      $newanswer->userid = $userid;
 338                      $newanswer->timemodified = time();
 339                      $newanswer->id = $DB->insert_record("choice_answers", $newanswer);
 340                      $answersnapshots[] = $newanswer;
 341                  }
 342              }
 343  
 344              $eventdata = array();
 345              $eventdata['context'] = $context;
 346              $eventdata['objectid'] = $choice->id;
 347              $eventdata['userid'] = $userid;
 348              $eventdata['courseid'] = $course->id;
 349              $eventdata['other'] = array();
 350              $eventdata['other']['choiceid'] = $choice->id;
 351              $eventdata['other']['optionid'] = $formanswer;
 352  
 353              $event = \mod_choice\event\answer_updated::create($eventdata);
 354              $event->add_record_snapshot('course', $course);
 355              $event->add_record_snapshot('course_modules', $cm);
 356              $event->add_record_snapshot('choice', $choice);
 357              foreach ($answersnapshots as $record) {
 358                  $event->add_record_snapshot('choice_answers', $record);
 359              }
 360              $event->trigger();
 361          } else {
 362  
 363              foreach ($formanswers as $answer) {
 364                  $newanswer = new stdClass();
 365                  $newanswer->choiceid = $choice->id;
 366                  $newanswer->userid = $userid;
 367                  $newanswer->optionid = $answer;
 368                  $newanswer->timemodified = time();
 369                  $newanswer->id = $DB->insert_record("choice_answers", $newanswer);
 370                  $answersnapshots[] = $newanswer;
 371              }
 372  
 373              // Update completion state
 374              $completion = new completion_info($course);
 375              if ($completion->is_enabled($cm) && $choice->completionsubmit) {
 376                  $completion->update_state($cm, COMPLETION_COMPLETE);
 377              }
 378  
 379              $eventdata = array();
 380              $eventdata['context'] = $context;
 381              $eventdata['objectid'] = $choice->id;
 382              $eventdata['userid'] = $userid;
 383              $eventdata['courseid'] = $course->id;
 384              $eventdata['other'] = array();
 385              $eventdata['other']['choiceid'] = $choice->id;
 386              $eventdata['other']['optionid'] = $formanswers;
 387  
 388              $event = \mod_choice\event\answer_submitted::create($eventdata);
 389              $event->add_record_snapshot('course', $course);
 390              $event->add_record_snapshot('course_modules', $cm);
 391              $event->add_record_snapshot('choice', $choice);
 392              foreach ($answersnapshots as $record) {
 393                  $event->add_record_snapshot('choice_answers', $record);
 394              }
 395              $event->trigger();
 396          }
 397      } else {
 398          // Check to see if current choice already selected - if not display error.
 399          $currentids = array_keys($current);
 400          if (array_diff($currentids, $formanswers) || array_diff($formanswers, $currentids) ) {
 401              print_error('choicefull', 'choice');
 402          }
 403      }
 404  }
 405  
 406  /**
 407   * @param array $user
 408   * @param object $cm
 409   * @return void Output is echo'd
 410   */
 411  function choice_show_reportlink($user, $cm) {
 412      $userschosen = array();
 413      foreach($user as $optionid => $userlist) {
 414          if ($optionid) {
 415              $userschosen = array_merge($userschosen, array_keys($userlist));
 416          }
 417      }
 418      $responsecount = count(array_unique($userschosen));
 419  
 420      echo '<div class="reportlink">';
 421      echo "<a href=\"report.php?id=$cm->id\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
 422      echo '</div>';
 423  }
 424  
 425  /**
 426   * @global object
 427   * @param object $choice
 428   * @param object $course
 429   * @param object $coursemodule
 430   * @param array $allresponses
 431  
 432   *  * @param bool $allresponses
 433   * @return object
 434   */
 435  function prepare_choice_show_results($choice, $course, $cm, $allresponses) {
 436      global $OUTPUT;
 437  
 438      $display = clone($choice);
 439      $display->coursemoduleid = $cm->id;
 440      $display->courseid = $course->id;
 441  
 442      //overwrite options value;
 443      $display->options = array();
 444      $totaluser = 0;
 445      foreach ($choice->option as $optionid => $optiontext) {
 446          $display->options[$optionid] = new stdClass;
 447          $display->options[$optionid]->text = $optiontext;
 448          $display->options[$optionid]->maxanswer = $choice->maxanswers[$optionid];
 449  
 450          if (array_key_exists($optionid, $allresponses)) {
 451              $display->options[$optionid]->user = $allresponses[$optionid];
 452              $totaluser += count($allresponses[$optionid]);
 453          }
 454      }
 455      unset($display->option);
 456      unset($display->maxanswers);
 457  
 458      $display->numberofuser = $totaluser;
 459      $context = context_module::instance($cm->id);
 460      $display->viewresponsecapability = has_capability('mod/choice:readresponses', $context);
 461      $display->deleterepsonsecapability = has_capability('mod/choice:deleteresponses',$context);
 462      $display->fullnamecapability = has_capability('moodle/site:viewfullnames', $context);
 463  
 464      if (empty($allresponses)) {
 465          echo $OUTPUT->heading(get_string("nousersyet"), 3, null);
 466          return false;
 467      }
 468  
 469      return $display;
 470  }
 471  
 472  /**
 473   * @global object
 474   * @param array $attemptids
 475   * @param object $choice Choice main table row
 476   * @param object $cm Course-module object
 477   * @param object $course Course object
 478   * @return bool
 479   */
 480  function choice_delete_responses($attemptids, $choice, $cm, $course) {
 481      global $DB, $CFG;
 482      require_once($CFG->libdir.'/completionlib.php');
 483  
 484      if(!is_array($attemptids) || empty($attemptids)) {
 485          return false;
 486      }
 487  
 488      foreach($attemptids as $num => $attemptid) {
 489          if(empty($attemptid)) {
 490              unset($attemptids[$num]);
 491          }
 492      }
 493  
 494      $completion = new completion_info($course);
 495      foreach($attemptids as $attemptid) {
 496          if ($todelete = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'id' => $attemptid))) {
 497              $DB->delete_records('choice_answers', array('choiceid' => $choice->id, 'id' => $attemptid));
 498              // Update completion state
 499              if ($completion->is_enabled($cm) && $choice->completionsubmit) {
 500                  $completion->update_state($cm, COMPLETION_INCOMPLETE, $attemptid);
 501              }
 502          }
 503      }
 504      return true;
 505  }
 506  
 507  
 508  /**
 509   * Given an ID of an instance of this module,
 510   * this function will permanently delete the instance
 511   * and any data that depends on it.
 512   *
 513   * @global object
 514   * @param int $id
 515   * @return bool
 516   */
 517  function choice_delete_instance($id) {
 518      global $DB;
 519  
 520      if (! $choice = $DB->get_record("choice", array("id"=>"$id"))) {
 521          return false;
 522      }
 523  
 524      $result = true;
 525  
 526      if (! $DB->delete_records("choice_answers", array("choiceid"=>"$choice->id"))) {
 527          $result = false;
 528      }
 529  
 530      if (! $DB->delete_records("choice_options", array("choiceid"=>"$choice->id"))) {
 531          $result = false;
 532      }
 533  
 534      if (! $DB->delete_records("choice", array("id"=>"$choice->id"))) {
 535          $result = false;
 536      }
 537  
 538      return $result;
 539  }
 540  
 541  /**
 542   * Returns text string which is the answer that matches the id
 543   *
 544   * @global object
 545   * @param object $choice
 546   * @param int $id
 547   * @return string
 548   */
 549  function choice_get_option_text($choice, $id) {
 550      global $DB;
 551  
 552      if ($result = $DB->get_record("choice_options", array("id" => $id))) {
 553          return $result->text;
 554      } else {
 555          return get_string("notanswered", "choice");
 556      }
 557  }
 558  
 559  /**
 560   * Gets a full choice record
 561   *
 562   * @global object
 563   * @param int $choiceid
 564   * @return object|bool The choice or false
 565   */
 566  function choice_get_choice($choiceid) {
 567      global $DB;
 568  
 569      if ($choice = $DB->get_record("choice", array("id" => $choiceid))) {
 570          if ($options = $DB->get_records("choice_options", array("choiceid" => $choiceid), "id")) {
 571              foreach ($options as $option) {
 572                  $choice->option[$option->id] = $option->text;
 573                  $choice->maxanswers[$option->id] = $option->maxanswers;
 574              }
 575              return $choice;
 576          }
 577      }
 578      return false;
 579  }
 580  
 581  /**
 582   * List the actions that correspond to a view of this module.
 583   * This is used by the participation report.
 584   *
 585   * Note: This is not used by new logging system. Event with
 586   *       crud = 'r' and edulevel = LEVEL_PARTICIPATING will
 587   *       be considered as view action.
 588   *
 589   * @return array
 590   */
 591  function choice_get_view_actions() {
 592      return array('view','view all','report');
 593  }
 594  
 595  /**
 596   * List the actions that correspond to a post of this module.
 597   * This is used by the participation report.
 598   *
 599   * Note: This is not used by new logging system. Event with
 600   *       crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
 601   *       will be considered as post action.
 602   *
 603   * @return array
 604   */
 605  function choice_get_post_actions() {
 606      return array('choose','choose again');
 607  }
 608  
 609  
 610  /**
 611   * Implementation of the function for printing the form elements that control
 612   * whether the course reset functionality affects the choice.
 613   *
 614   * @param object $mform form passed by reference
 615   */
 616  function choice_reset_course_form_definition(&$mform) {
 617      $mform->addElement('header', 'choiceheader', get_string('modulenameplural', 'choice'));
 618      $mform->addElement('advcheckbox', 'reset_choice', get_string('removeresponses','choice'));
 619  }
 620  
 621  /**
 622   * Course reset form defaults.
 623   *
 624   * @return array
 625   */
 626  function choice_reset_course_form_defaults($course) {
 627      return array('reset_choice'=>1);
 628  }
 629  
 630  /**
 631   * Actual implementation of the reset course functionality, delete all the
 632   * choice responses for course $data->courseid.
 633   *
 634   * @global object
 635   * @global object
 636   * @param object $data the data submitted from the reset course.
 637   * @return array status array
 638   */
 639  function choice_reset_userdata($data) {
 640      global $CFG, $DB;
 641  
 642      $componentstr = get_string('modulenameplural', 'choice');
 643      $status = array();
 644  
 645      if (!empty($data->reset_choice)) {
 646          $choicessql = "SELECT ch.id
 647                         FROM {choice} ch
 648                         WHERE ch.course=?";
 649  
 650          $DB->delete_records_select('choice_answers', "choiceid IN ($choicessql)", array($data->courseid));
 651          $status[] = array('component'=>$componentstr, 'item'=>get_string('removeresponses', 'choice'), 'error'=>false);
 652      }
 653  
 654      /// updating dates - shift may be negative too
 655      if ($data->timeshift) {
 656          shift_course_mod_dates('choice', array('timeopen', 'timeclose'), $data->timeshift, $data->courseid);
 657          $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
 658      }
 659  
 660      return $status;
 661  }
 662  
 663  /**
 664   * @global object
 665   * @global object
 666   * @global object
 667   * @uses CONTEXT_MODULE
 668   * @param object $choice
 669   * @param object $cm
 670   * @param int $groupmode
 671   * @return array
 672   */
 673  function choice_get_response_data($choice, $cm, $groupmode) {
 674      global $CFG, $USER, $DB;
 675  
 676      $context = context_module::instance($cm->id);
 677  
 678  /// Get the current group
 679      if ($groupmode > 0) {
 680          $currentgroup = groups_get_activity_group($cm);
 681      } else {
 682          $currentgroup = 0;
 683      }
 684  
 685  /// Initialise the returned array, which is a matrix:  $allresponses[responseid][userid] = responseobject
 686      $allresponses = array();
 687  
 688  /// First get all the users who have access here
 689  /// To start with we assume they are all "unanswered" then move them later
 690      $allresponses[0] = get_enrolled_users($context, 'mod/choice:choose', $currentgroup, user_picture::fields('u', array('idnumber')));
 691  
 692  /// Get all the recorded responses for this choice
 693      $rawresponses = $DB->get_records('choice_answers', array('choiceid' => $choice->id));
 694  
 695  /// Use the responses to move users into the correct column
 696  
 697      if ($rawresponses) {
 698          $answeredusers = array();
 699          foreach ($rawresponses as $response) {
 700              if (isset($allresponses[0][$response->userid])) {   // This person is enrolled and in correct group
 701                  $allresponses[0][$response->userid]->timemodified = $response->timemodified;
 702                  $allresponses[$response->optionid][$response->userid] = clone($allresponses[0][$response->userid]);
 703                  $allresponses[$response->optionid][$response->userid]->answerid = $response->id;
 704                  $answeredusers[] = $response->userid;
 705              }
 706          }
 707          foreach ($answeredusers as $answereduser) {
 708              unset($allresponses[0][$answereduser]);
 709          }
 710      }
 711      return $allresponses;
 712  }
 713  
 714  /**
 715   * Returns all other caps used in module
 716   *
 717   * @return array
 718   */
 719  function choice_get_extra_capabilities() {
 720      return array('moodle/site:accessallgroups');
 721  }
 722  
 723  /**
 724   * @uses FEATURE_GROUPS
 725   * @uses FEATURE_GROUPINGS
 726   * @uses FEATURE_MOD_INTRO
 727   * @uses FEATURE_COMPLETION_TRACKS_VIEWS
 728   * @uses FEATURE_GRADE_HAS_GRADE
 729   * @uses FEATURE_GRADE_OUTCOMES
 730   * @param string $feature FEATURE_xx constant for requested feature
 731   * @return mixed True if module supports feature, null if doesn't know
 732   */
 733  function choice_supports($feature) {
 734      switch($feature) {
 735          case FEATURE_GROUPS:                  return true;
 736          case FEATURE_GROUPINGS:               return true;
 737          case FEATURE_MOD_INTRO:               return true;
 738          case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
 739          case FEATURE_COMPLETION_HAS_RULES:    return true;
 740          case FEATURE_GRADE_HAS_GRADE:         return false;
 741          case FEATURE_GRADE_OUTCOMES:          return false;
 742          case FEATURE_BACKUP_MOODLE2:          return true;
 743          case FEATURE_SHOW_DESCRIPTION:        return true;
 744  
 745          default: return null;
 746      }
 747  }
 748  
 749  /**
 750   * Adds module specific settings to the settings block
 751   *
 752   * @param settings_navigation $settings The settings navigation object
 753   * @param navigation_node $choicenode The node to add module settings to
 754   */
 755  function choice_extend_settings_navigation(settings_navigation $settings, navigation_node $choicenode) {
 756      global $PAGE;
 757  
 758      if (has_capability('mod/choice:readresponses', $PAGE->cm->context)) {
 759  
 760          $groupmode = groups_get_activity_groupmode($PAGE->cm);
 761          if ($groupmode) {
 762              groups_get_activity_group($PAGE->cm, true);
 763          }
 764          // We only actually need the choice id here
 765          $choice = new stdClass;
 766          $choice->id = $PAGE->cm->instance;
 767          $allresponses = choice_get_response_data($choice, $PAGE->cm, $groupmode);   // Big function, approx 6 SQL calls per user
 768  
 769          $responsecount =0;
 770          foreach($allresponses as $optionid => $userlist) {
 771              if ($optionid) {
 772                  $responsecount += count($userlist);
 773              }
 774          }
 775          $choicenode->add(get_string("viewallresponses", "choice", $responsecount), new moodle_url('/mod/choice/report.php', array('id'=>$PAGE->cm->id)));
 776      }
 777  }
 778  
 779  /**
 780   * Obtains the automatic completion state for this choice based on any conditions
 781   * in forum settings.
 782   *
 783   * @param object $course Course
 784   * @param object $cm Course-module
 785   * @param int $userid User ID
 786   * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
 787   * @return bool True if completed, false if not, $type if conditions not set.
 788   */
 789  function choice_get_completion_state($course, $cm, $userid, $type) {
 790      global $CFG,$DB;
 791  
 792      // Get choice details
 793      $choice = $DB->get_record('choice', array('id'=>$cm->instance), '*',
 794              MUST_EXIST);
 795  
 796      // If completion option is enabled, evaluate it and return true/false
 797      if($choice->completionsubmit) {
 798          return $DB->record_exists('choice_answers', array(
 799                  'choiceid'=>$choice->id, 'userid'=>$userid));
 800      } else {
 801          // Completion option is not enabled so just return $type
 802          return $type;
 803      }
 804  }
 805  
 806  /**
 807   * Return a list of page types
 808   * @param string $pagetype current page type
 809   * @param stdClass $parentcontext Block's parent context
 810   * @param stdClass $currentcontext Current context of block
 811   */
 812  function choice_page_type_list($pagetype, $parentcontext, $currentcontext) {
 813      $module_pagetype = array('mod-choice-*'=>get_string('page-mod-choice-x', 'choice'));
 814      return $module_pagetype;
 815  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1