[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

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

   1  <?php // $Id: lib.php,v 1.59.2.24 2008/08/19 05:17:14 tjhunt Exp $
   2  
   3  $COLUMN_HEIGHT = 300;
   4  
   5  define('CHOICE_PUBLISH_ANONYMOUS', '0');
   6  define('CHOICE_PUBLISH_NAMES',     '1');
   7  
   8  define('CHOICE_SHOWRESULTS_NOT',          '0');
   9  define('CHOICE_SHOWRESULTS_AFTER_ANSWER', '1');
  10  define('CHOICE_SHOWRESULTS_AFTER_CLOSE',  '2');
  11  define('CHOICE_SHOWRESULTS_ALWAYS',       '3');
  12  
  13  define('CHOICE_DISPLAY_HORIZONTAL',  '0');
  14  define('CHOICE_DISPLAY_VERTICAL',    '1');
  15  
  16  $CHOICE_PUBLISH = array (CHOICE_PUBLISH_ANONYMOUS  => get_string('publishanonymous', 'choice'),
  17                           CHOICE_PUBLISH_NAMES      => get_string('publishnames', 'choice'));
  18  
  19  $CHOICE_SHOWRESULTS = array (CHOICE_SHOWRESULTS_NOT          => get_string('publishnot', 'choice'),
  20                           CHOICE_SHOWRESULTS_AFTER_ANSWER => get_string('publishafteranswer', 'choice'),
  21                           CHOICE_SHOWRESULTS_AFTER_CLOSE  => get_string('publishafterclose', 'choice'),
  22                           CHOICE_SHOWRESULTS_ALWAYS       => get_string('publishalways', 'choice'));
  23  
  24  $CHOICE_DISPLAY = array (CHOICE_DISPLAY_HORIZONTAL   => get_string('displayhorizontal', 'choice'),
  25                           CHOICE_DISPLAY_VERTICAL     => get_string('displayvertical','choice'));
  26  
  27  /// Standard functions /////////////////////////////////////////////////////////
  28  
  29  function choice_user_outline($course, $user, $mod, $choice) {
  30      if ($answer = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id)) {
  31          $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
  32          $result->time = $answer->timemodified;
  33          return $result;
  34      }
  35      return NULL;
  36  }
  37  
  38  
  39  function choice_user_complete($course, $user, $mod, $choice) {
  40      if ($answer = get_record('choice_answers', "choiceid", $choice->id, "userid", $user->id)) {
  41          $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
  42          $result->time = $answer->timemodified;
  43          echo get_string("answered", "choice").": $result->info. ".get_string("updated", '', userdate($result->time));
  44      } else {
  45          print_string("notanswered", "choice");
  46      }
  47  }
  48  
  49  
  50  function choice_add_instance($choice) {
  51  // Given an object containing all the necessary data,
  52  // (defined by the form in mod.html) this function
  53  // will create a new instance and return the id number
  54  // of the new instance.
  55  
  56      $choice->timemodified = time();
  57  
  58      if (empty($choice->timerestrict)) {
  59          $choice->timeopen = 0;
  60          $choice->timeclose = 0;
  61      }
  62  
  63      //insert answers
  64      if ($choice->id = insert_record("choice", $choice)) {
  65          foreach ($choice->option as $key => $value) {
  66              $value = trim($value);
  67              if (isset($value) && $value <> '') {
  68                  $option = new object();
  69                  $option->text = $value;
  70                  $option->choiceid = $choice->id;
  71                  if (isset($choice->limit[$key])) {
  72                      $option->maxanswers = $choice->limit[$key];
  73                  }
  74                  $option->timemodified = time();
  75                  insert_record("choice_options", $option);
  76              }
  77          }
  78      }
  79      return $choice->id;
  80  }
  81  
  82  
  83  function choice_update_instance($choice) {
  84  // Given an object containing all the necessary data,
  85  // (defined by the form in mod.html) this function
  86  // will update an existing instance with new data.
  87  
  88      $choice->id = $choice->instance;
  89      $choice->timemodified = time();
  90  
  91  
  92      if (empty($choice->timerestrict)) {
  93          $choice->timeopen = 0;
  94          $choice->timeclose = 0;
  95      }
  96  
  97      //update, delete or insert answers
  98      foreach ($choice->option as $key => $value) {
  99          $value = trim($value);
 100          $option = new object();
 101          $option->text = $value;
 102          $option->choiceid = $choice->id;
 103          if (isset($choice->limit[$key])) {
 104              $option->maxanswers = $choice->limit[$key];
 105          }
 106          $option->timemodified = time();
 107          if (isset($choice->optionid[$key]) && !empty($choice->optionid[$key])){//existing choice record
 108              $option->id=$choice->optionid[$key];
 109              if (isset($value) && $value <> '') {
 110                  update_record("choice_options", $option);
 111              } else { //empty old option - needs to be deleted.
 112                  delete_records("choice_options", "id", $option->id);
 113              }
 114          } else {
 115              if (isset($value) && $value <> '') {
 116                  insert_record("choice_options", $option);
 117              }
 118          }
 119      }
 120  
 121      return update_record('choice', $choice);
 122  
 123  }
 124  
 125  function choice_show_form($choice, $user, $cm, $allresponses) {
 126  
 127  //$cdisplay is an array of the display info for a choice $cdisplay[$optionid]->text  - text name of option.
 128  //                                                                            ->maxanswers -maxanswers for this option
 129  //                                                                            ->full - whether this option is full or not. 0=not full, 1=full
 130      $cdisplay = array();
 131  
 132      $aid = 0;
 133      $choicefull = false;
 134      $cdisplay = array();
 135  
 136      if ($choice->limitanswers) { //set choicefull to true by default if limitanswers.
 137          $choicefull = true;
 138      }
 139  
 140      $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 141  
 142      foreach ($choice->option as $optionid => $text) {
 143          if (isset($text)) { //make sure there are no dud entries in the db with blank text values.
 144              $cdisplay[$aid]->optionid = $optionid;
 145              $cdisplay[$aid]->text = $text;
 146              $cdisplay[$aid]->maxanswers = $choice->maxanswers[$optionid];
 147              if (isset($allresponses[$optionid])) {
 148                  $cdisplay[$aid]->countanswers = count($allresponses[$optionid]);
 149              } else {
 150                  $cdisplay[$aid]->countanswers = 0;
 151              }
 152              if ($current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id, 'optionid', $optionid)) {
 153                  $cdisplay[$aid]->checked = ' checked="checked" ';
 154              } else {
 155                  $cdisplay[$aid]->checked = '';
 156              }
 157              if ( $choice->limitanswers && 
 158                  ($cdisplay[$aid]->countanswers >= $cdisplay[$aid]->maxanswers) && 
 159                  (empty($cdisplay[$aid]->checked)) ) {
 160                  $cdisplay[$aid]->disabled = ' disabled="disabled" ';
 161              } else {
 162                  $cdisplay[$aid]->disabled = '';
 163                  if ($choice->limitanswers && ($cdisplay[$aid]->countanswers < $cdisplay[$aid]->maxanswers)) {
 164                      $choicefull = false; //set $choicefull to false - as the above condition hasn't been set.
 165                  }
 166              }
 167              $aid++;
 168          }
 169      }
 170  
 171      switch ($choice->display) {
 172          case CHOICE_DISPLAY_HORIZONTAL:
 173              echo "<table cellpadding=\"20\" cellspacing=\"20\" class=\"boxaligncenter\"><tr>";
 174  
 175              foreach ($cdisplay as $cd) {
 176                  echo "<td align=\"center\" valign=\"top\">";
 177                  echo "<input type=\"radio\" name=\"answer\" value=\"".$cd->optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />";
 178                  if (!empty($cd->disabled)) {
 179                      echo format_text($cd->text."<br /><strong>".get_string('full', 'choice')."</strong>");
 180                  } else {
 181                      echo format_text($cd->text);
 182                  }
 183                  echo "</td>";
 184              }
 185              echo "</tr>";
 186              echo "</table>";
 187              break;
 188  
 189          case CHOICE_DISPLAY_VERTICAL:
 190              $displayoptions->para = false;
 191              echo "<table cellpadding=\"10\" cellspacing=\"10\" class=\"boxaligncenter\">";
 192              foreach ($cdisplay as $cd) {
 193                  echo "<tr><td align=\"left\">";
 194                  echo "<input type=\"radio\" name=\"answer\" value=\"".$cd->optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />";
 195  
 196                  echo format_text($cd->text. ' ', FORMAT_MOODLE, $displayoptions); //display text for option.
 197  
 198                  if ($choice->limitanswers && ($choice->showresults==CHOICE_SHOWRESULTS_ALWAYS) ){ //if limit is enabled, and show results always has been selected, display info beside each choice.
 199                      echo "</td><td>";
 200  
 201                      if (!empty($cd->disabled)) {
 202                          echo get_string('full', 'choice');
 203                      } elseif(!empty($cd->checked)) {
 204                                  //currently do nothing - maybe some text could be added here to signfy that the choice has been 'selected'
 205                      } elseif ($cd->maxanswers-$cd->countanswers==1) {
 206                          echo ($cd->maxanswers - $cd->countanswers);
 207                          echo " ".get_string('spaceleft', 'choice');
 208                      } else {
 209                          echo ($cd->maxanswers - $cd->countanswers);
 210                          echo " ".get_string('spacesleft', 'choice');
 211                      }
 212                      echo "</td>";
 213                  } else if ($choice->limitanswers && ($cd->countanswers >= $cd->maxanswers)) {  //if limitanswers and answers exceeded, display "full" beside the choice.
 214                      echo " <strong>".get_string('full', 'choice')."</strong>";
 215                  }
 216                  echo "</td>";
 217                  echo "</tr>";
 218              }
 219          echo "</table>";
 220          break;
 221      }
 222      //show save choice button
 223      echo '<div class="button">';
 224      echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
 225      if (has_capability('mod/choice:choose', $context, $user->id, false)) { //don't show save button if the logged in user is the guest user.
 226          if ($choicefull) {
 227              print_string('choicefull', 'choice');
 228              echo "</br>";
 229          } else {
 230              echo "<input type=\"submit\" value=\"".get_string("savemychoice","choice")."\" />";
 231          }
 232          if ($choice->allowupdate && $aaa = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id)) {
 233              echo "<br /><a href='view.php?id=".$cm->id."&amp;action=delchoice'>".get_string("removemychoice","choice")."</a>";
 234          }
 235      } else {
 236          print_string('havetologin', 'choice');
 237      }
 238      echo "</div>";
 239  }
 240  
 241  function choice_user_submit_response($formanswer, $choice, $userid, $courseid, $cm) {
 242  
 243      $current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $userid);
 244      $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 245  
 246      $countanswers=0;
 247      if($choice->limitanswers) {
 248          // Find out whether groups are being used and enabled
 249          if (groups_get_activity_groupmode($cm) > 0) {
 250              $currentgroup = groups_get_activity_group($cm);
 251          } else {
 252              $currentgroup = 0;
 253          }
 254          if($currentgroup) {
 255              // If groups are being used, retrieve responses only for users in
 256              // current group
 257              global $CFG;
 258              $answers = get_records_sql("
 259  SELECT 
 260      ca.*
 261  FROM 
 262      {$CFG->prefix}choice_answers ca
 263      INNER JOIN {$CFG->prefix}groups_members gm ON ca.userid=gm.userid
 264  WHERE
 265      optionid=$formanswer
 266      AND gm.groupid=$currentgroup");
 267          } else {
 268              // Groups are not used, retrieve all answers for this option ID
 269              $answers = get_records("choice_answers", "optionid", $formanswer);
 270          }
 271  
 272          if ($answers) {
 273              foreach ($answers as $a) { //only return enrolled users.
 274                  if (has_capability('mod/choice:choose', $context, $a->userid, false)) {
 275                      $countanswers++;
 276                  }
 277              }
 278          }
 279          $maxans = $choice->maxanswers[$formanswer];
 280      }
 281  
 282      if (!($choice->limitanswers && ($countanswers >= $maxans) )) {
 283          if ($current) {
 284  
 285              $newanswer = $current;
 286              $newanswer->optionid = $formanswer;
 287              $newanswer->timemodified = time();
 288              if (! update_record("choice_answers", $newanswer)) {
 289                  error("Could not update your choice because of a database error");
 290              }
 291              add_to_log($courseid, "choice", "choose again", "view.php?id=$cm->id", $choice->id, $cm->id);
 292          } else {
 293              $newanswer = NULL;
 294              $newanswer->choiceid = $choice->id;
 295              $newanswer->userid = $userid;
 296              $newanswer->optionid = $formanswer;
 297              $newanswer->timemodified = time();
 298              if (! insert_record("choice_answers", $newanswer)) {
 299                  error("Could not save your choice");
 300              }
 301              add_to_log($courseid, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id);
 302          }
 303      } else {
 304          if (!($current->optionid==$formanswer)) { //check to see if current choice already selected - if not display error
 305              error("this choice is full!");
 306          }
 307      }
 308  }
 309  
 310  function choice_show_reportlink($user, $cm) {
 311      $responsecount =0;
 312      foreach($user as $optionid => $userlist) {
 313          if ($optionid) {
 314              $responsecount += count($userlist);
 315          }
 316      }
 317  
 318      echo '<div class="reportlink">';
 319      echo "<a href=\"report.php?id=$cm->id\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
 320      echo '</div>';
 321  }
 322  
 323  function choice_show_results($choice, $course, $cm, $allresponses, $forcepublish='') {
 324      global $CFG, $COLUMN_HEIGHT;
 325      
 326      print_heading(get_string("responses", "choice"));
 327      if (empty($forcepublish)) { //alow the publish setting to be overridden
 328          $forcepublish = $choice->publish;
 329      }
 330  
 331      if (!$allresponses) {
 332          print_heading(get_string("nousersyet"));
 333      }
 334  
 335      $totalresponsecount = 0;
 336      foreach ($allresponses as $optionid => $userlist) {
 337          if ($choice->showunanswered || $optionid) {
 338              $totalresponsecount += count($userlist);
 339          }
 340      }
 341      
 342      $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 343  
 344      $hascapfullnames = has_capability('moodle/site:viewfullnames', $context);
 345      
 346      $viewresponses = has_capability('mod/choice:readresponses', $context); 
 347      switch ($forcepublish) {
 348          case CHOICE_PUBLISH_NAMES:
 349              echo '<div id="tablecontainer">';
 350              if ($viewresponses) {
 351                  echo '<form id="attemptsform" method="post" action="'.$_SERVER['PHP_SELF'].'" onsubmit="var menu = document.getElementById(\'menuaction\'); return (menu.options[menu.selectedIndex].value == \'delete\' ? \''.addslashes(get_string('deleteattemptcheck','quiz')).'\' : true);">';
 352                  echo '<div>';
 353                  echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
 354                  echo '<input type="hidden" name="mode" value="overview" />';
 355              }
 356  
 357              echo "<table cellpadding=\"5\" cellspacing=\"10\" class=\"results names\">";
 358              echo "<tr>";
 359    
 360              $columncount = array(); // number of votes in each column
 361              if ($choice->showunanswered) {
 362                  $columncount[0] = 0;
 363                  echo "<th class=\"col0 header\" scope=\"col\">";
 364                  print_string('notanswered', 'choice');
 365                  echo "</th>";
 366              }
 367              $count = 1;
 368              foreach ($choice->option as $optionid => $optiontext) {
 369                  $columncount[$optionid] = 0; // init counters
 370                  echo "<th class=\"col$count header\" scope=\"col\">";
 371                  echo format_string($optiontext);
 372                  echo "</th>";
 373                  $count++;
 374              }
 375              echo "</tr><tr>";
 376  
 377              if ($choice->showunanswered) {
 378                  echo "<td class=\"col$count data\" >";
 379                  // added empty row so that when the next iteration is empty,
 380                  // we do not get <table></table> erro from w3c validator
 381                  // MDL-7861
 382                  echo "<table class=\"choiceresponse\"><tr><td></td></tr>";
 383                  foreach ($allresponses[0] as $user) {
 384                      echo "<tr>";
 385                      echo "<td class=\"picture\">";
 386                      print_user_picture($user->id, $course->id, $user->picture);
 387                      echo "</td><td class=\"fullname\">";
 388                      echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
 389                      echo fullname($user, $hascapfullnames);
 390                      echo "</a>";
 391                      echo "</td></tr>";
 392                  }
 393                  echo "</table></td>";
 394              }
 395              $count = 0;
 396              foreach ($choice->option as $optionid => $optiontext) {
 397                      echo '<td class="col'.$count.' data" >';
 398  
 399                      // added empty row so that when the next iteration is empty,
 400                      // we do not get <table></table> erro from w3c validator
 401                      // MDL-7861
 402                      echo '<table class="choiceresponse"><tr><td></td></tr>';
 403                      if (isset($allresponses[$optionid])) {
 404                          foreach ($allresponses[$optionid] as $user) {
 405                              $columncount[$optionid] += 1;
 406                              echo '<tr><td class="attemptcell">';
 407                              if ($viewresponses and has_capability('mod/choice:deleteresponses',$context)) {
 408                                  echo '<input type="checkbox" name="attemptid[]" value="'. $user->id. '" />';
 409                              }
 410                              echo '</td><td class="picture">';
 411                              print_user_picture($user->id, $course->id, $user->picture);
 412                              echo '</td><td class="fullname">';
 413                              echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
 414                              echo fullname($user, $hascapfullnames);
 415                              echo '</a>';
 416                              echo '</td></tr>';
 417                         }
 418                      }
 419                      $count++;
 420                      echo '</table></td>';
 421              }
 422              echo "</tr><tr>";
 423              $count = 0;
 424              
 425              if ($choice->showunanswered) {
 426                  echo "<td></td>";
 427              }
 428              
 429              foreach ($choice->option as $optionid => $optiontext) {
 430                  echo "<td align=\"center\" class=\"count\">";
 431                  if ($choice->limitanswers) {
 432                      echo get_string("taken", "choice").":";
 433                      echo $columncount[$optionid];
 434                      echo "<br/>";
 435                      echo get_string("limit", "choice").":";
 436                      $choice_option = get_record("choice_options", "id", $optionid);
 437                      echo $choice_option->maxanswers;
 438                  } else {
 439                      if (isset($columncount[$optionid])) {
 440                          echo $columncount[$optionid];
 441                      }
 442                  }
 443                  echo "</td>";
 444                  $count++;
 445              }
 446              echo "</tr>";
 447              
 448              /// Print "Select all" etc.
 449              if ($viewresponses and has_capability('mod/choice:deleteresponses',$context)) {
 450                  echo '<tr><td></td><td>';
 451                  echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectall', 'quiz').'</a> / ';
 452                  echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectnone', 'quiz').'</a> ';
 453                  echo '&nbsp;&nbsp;';
 454                  $options = array('delete' => get_string('delete'));
 455                  echo choose_from_menu($options, 'action', '', get_string('withselected', 'quiz'), 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true);
 456                  echo '<noscript id="noscriptmenuaction" style="display: inline;">';
 457                  echo '<div>';
 458                  echo '<input type="submit" value="'.get_string('go').'" /></div></noscript>';
 459                  echo '<script type="text/javascript">'."\n<!--\n".'document.getElementById("noscriptmenuaction").style.display = "none";'."\n-->\n".'</script>';
 460                  echo '</td><td></td></tr>';
 461              }
 462              
 463              echo "</table></div>";
 464              if ($viewresponses) {
 465                  echo "</form></div>";
 466              }
 467              break;
 468          
 469          
 470          case CHOICE_PUBLISH_ANONYMOUS:
 471  
 472              echo "<table cellpadding=\"5\" cellspacing=\"0\" class=\"results anonymous\">";
 473              echo "<tr>";
 474              $maxcolumn = 0;
 475              if ($choice->showunanswered) {
 476                  echo "<th  class=\"col0 header\" scope=\"col\">";
 477                  print_string('notanswered', 'choice');
 478                  echo "</th>";
 479                  $column[0] = 0;
 480                  foreach ($allresponses[0] as $user) {
 481                      $column[0]++;
 482                  }
 483                  $maxcolumn = $column[0];
 484              }
 485              $count = 1;
 486  
 487              foreach ($choice->option as $optionid => $optiontext) {
 488                  echo "<th class=\"col$count header\" scope=\"col\">";
 489                  echo format_string($optiontext);
 490                  echo "</th>";
 491                  
 492                  $column[$optionid] = 0;
 493                  if (isset($allresponses[$optionid])) {
 494                      $column[$optionid] = count($allresponses[$optionid]);
 495                      if ($column[$optionid] > $maxcolumn) {
 496                          $maxcolumn = $column[$optionid];
 497                      }
 498                  } else {
 499                      $column[$optionid] = 0;
 500                  }
 501              }
 502              echo "</tr><tr>";
 503  
 504              $height = 0;
 505  
 506              if ($choice->showunanswered) {
 507                  if ($maxcolumn) {
 508                      $height = $COLUMN_HEIGHT * ((float)$column[0] / (float)$maxcolumn);
 509                  }
 510                  echo "<td style=\"vertical-align:bottom\" align=\"center\" class=\"col0 data\">";
 511                  echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
 512                  echo "</td>";
 513              }
 514              $count = 1;
 515              foreach ($choice->option as $optionid => $optiontext) {
 516                  if ($maxcolumn) {
 517                      $height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn);
 518                  }
 519                  echo "<td style=\"vertical-align:bottom\" align=\"center\" class=\"col$count data\">";
 520                  echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
 521                  echo "</td>";
 522                  $count++;
 523              }
 524              echo "</tr><tr>";
 525  
 526  
 527              if ($choice->showunanswered) {
 528                  echo '<td align="center" class="col0 count">';
 529                  if (!$choice->limitanswers) {
 530                      echo $column[0];
 531                      echo '<br />('.format_float(((float)$column[0]/(float)$totalresponsecount)*100.0,1).'%)';
 532                  }
 533                  echo '</td>';
 534              }
 535              $count = 1;
 536              foreach ($choice->option as $optionid => $optiontext) {
 537                  echo "<td align=\"center\" class=\"col$count count\">";
 538                  if ($choice->limitanswers) {
 539                      echo get_string("taken", "choice").":";
 540                      echo $column[$optionid].'<br />';
 541                      echo get_string("limit", "choice").":";
 542                      $choice_option = get_record("choice_options", "id", $optionid);
 543                      echo $choice_option->maxanswers;
 544                  } else {
 545                      echo $column[$optionid];
 546                      echo '<br />('.format_float(((float)$column[$optionid]/(float)$totalresponsecount)*100.0,1).'%)';
 547                  }
 548                  echo "</td>";
 549                  $count++;
 550              }
 551              echo "</tr></table>";
 552              
 553              break;
 554      }
 555  }
 556  
 557  
 558  function choice_delete_responses($attemptids, $choiceid) {
 559  
 560      if(!is_array($attemptids) || empty($attemptids)) {
 561          return false;
 562      }
 563  
 564      foreach($attemptids as $num => $attemptid) {
 565          if(empty($attemptid)) {
 566              unset($attemptids[$num]);
 567          }
 568      }
 569  
 570      foreach($attemptids as $attemptid) {
 571          if ($todelete = get_record('choice_answers', 'choiceid', $choiceid, 'userid', $attemptid)) {
 572              delete_records('choice_answers', 'choiceid', $choiceid, 'userid', $attemptid);
 573          }
 574      }
 575      return true;
 576  }
 577  
 578  
 579  function choice_delete_instance($id) {
 580  // Given an ID of an instance of this module,
 581  // this function will permanently delete the instance
 582  // and any data that depends on it.
 583  
 584      if (! $choice = get_record("choice", "id", "$id")) {
 585          return false;
 586      }
 587  
 588      $result = true;
 589  
 590      if (! delete_records("choice_answers", "choiceid", "$choice->id")) {
 591          $result = false;
 592      }
 593  
 594      if (! delete_records("choice_options", "choiceid", "$choice->id")) {
 595          $result = false;
 596      }
 597  
 598      if (! delete_records("choice", "id", "$choice->id")) {
 599          $result = false;
 600      }
 601  
 602      return $result;
 603  }
 604  
 605  function choice_get_participants($choiceid) {
 606  //Returns the users with data in one choice
 607  //(users with records in choice_responses, students)
 608  
 609      global $CFG;
 610  
 611      //Get students
 612      $students = get_records_sql("SELECT DISTINCT u.id, u.id
 613                                   FROM {$CFG->prefix}user u,
 614                                        {$CFG->prefix}choice_answers a
 615                                   WHERE a.choiceid = '$choiceid' and
 616                                         u.id = a.userid");
 617  
 618      //Return students array (it contains an array of unique users)
 619      return ($students);
 620  }
 621  
 622  
 623  function choice_get_option_text($choice, $id) {
 624  // Returns text string which is the answer that matches the id
 625      if ($result = get_record("choice_options", "id", $id)) {
 626          return $result->text;
 627      } else {
 628          return get_string("notanswered", "choice");
 629      }
 630  }
 631  
 632  function choice_get_choice($choiceid) {
 633  // Gets a full choice record
 634  
 635      if ($choice = get_record("choice", "id", $choiceid)) {
 636          if ($options = get_records("choice_options", "choiceid", $choiceid, "id")) {
 637              foreach ($options as $option) {
 638                  $choice->option[$option->id] = $option->text;
 639                  $choice->maxanswers[$option->id] = $option->maxanswers;
 640              }
 641              return $choice;
 642          }
 643      }
 644      return false;
 645  }
 646  
 647  function choice_get_view_actions() {
 648      return array('view','view all','report');
 649  }
 650  
 651  function choice_get_post_actions() {
 652      return array('choose','choose again');
 653  }
 654  
 655  
 656  /**
 657   * Implementation of the function for printing the form elements that control
 658   * whether the course reset functionality affects the choice.
 659   * @param $mform form passed by reference
 660   */
 661  function choice_reset_course_form_definition(&$mform) {
 662      $mform->addElement('header', 'choiceheader', get_string('modulenameplural', 'choice'));
 663      $mform->addElement('advcheckbox', 'reset_choice', get_string('removeresponses','choice'));
 664  }
 665  
 666  /**
 667   * Course reset form defaults.
 668   */
 669  function choice_reset_course_form_defaults($course) {
 670      return array('reset_choice'=>1);
 671  }
 672  
 673  /**
 674   * Actual implementation of the rest coures functionality, delete all the
 675   * choice responses for course $data->courseid.
 676   * @param $data the data submitted from the reset course.
 677   * @return array status array
 678   */
 679  function choice_reset_userdata($data) {
 680      global $CFG;
 681  
 682      $componentstr = get_string('modulenameplural', 'choice');
 683      $status = array();
 684  
 685      if (!empty($data->reset_choice)) {
 686          $choicessql = "SELECT ch.id
 687                           FROM {$CFG->prefix}choice ch
 688                          WHERE ch.course={$data->courseid}";
 689  
 690          delete_records_select('choice_answers', "choiceid IN ($choicessql)");
 691          $status[] = array('component'=>$componentstr, 'item'=>get_string('removeresponses', 'choice'), 'error'=>false);
 692      }
 693  
 694      /// updating dates - shift may be negative too
 695      if ($data->timeshift) {
 696          shift_course_mod_dates('choice', array('timeopen', 'timeclose'), $data->timeshift, $data->courseid);
 697          $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
 698      }
 699  
 700      return $status;
 701  }
 702  
 703  function choice_get_response_data($choice, $cm, $groupmode) {
 704      global $CFG, $USER;
 705  
 706      $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 707  
 708  /// Get the current group
 709      if ($groupmode > 0) {
 710          $currentgroup = groups_get_activity_group($cm);
 711      } else {
 712          $currentgroup = 0;
 713      }
 714  
 715  /// Initialise the returned array, which is a matrix:  $allresponses[responseid][userid] = responseobject
 716      $allresponses = array();
 717  
 718  /// First get all the users who have access here
 719  /// To start with we assume they are all "unanswered" then move them later
 720      $allresponses[0] = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC', '', '', $currentgroup, '', false, true);
 721  
 722  /// Get all the recorded responses for this choice
 723      $rawresponses = get_records('choice_answers', 'choiceid', $choice->id);
 724  
 725  /// Use the responses to move users into the correct column
 726  
 727      if ($rawresponses) {
 728          foreach ($rawresponses as $response) {
 729              if (isset($allresponses[0][$response->userid])) {   // This person is enrolled and in correct group
 730                  $allresponses[0][$response->userid]->timemodified = $response->timemodified;
 731                  $allresponses[$response->optionid][$response->userid] = clone($allresponses[0][$response->userid]);
 732                  unset($allresponses[0][$response->userid]);   // Remove from unanswered column
 733              }
 734          }
 735      }
 736  
 737      return $allresponses;
 738  }
 739  
 740  /**
 741   * Returns all other caps used in module
 742   */
 743  function choice_get_extra_capabilities() {
 744      return array('moodle/site:accessallgroups');
 745  }
 746  
 747  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7