[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/lesson/ -> highscores.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   * Provides the interface for viewing and adding high scores
  20   *
  21   * @package mod_lesson
  22   * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   **/
  25  
  26  /** include required files */
  27  require_once('../../config.php');
  28  require_once($CFG->dirroot.'/mod/lesson/locallib.php');
  29  
  30  $id      = required_param('id', PARAM_INT);             // Course Module ID
  31  $mode    = optional_param('mode', '', PARAM_ALPHA);
  32  $link = optional_param('link', 0, PARAM_INT);
  33  
  34  $cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);
  35  $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  36  $lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
  37  
  38  require_login($course, false, $cm);
  39  
  40  $url = new moodle_url('/mod/lesson/highscores.php', array('id'=>$id));
  41  if ($mode !== '') {
  42      $url->param('mode', $mode);
  43  }
  44  if ($link !== 0) {
  45      $url->param('link', $link);
  46  }
  47  $PAGE->set_url($url);
  48  
  49  $context = context_module::instance($cm->id);
  50  
  51  switch ($mode) {
  52      case 'add':
  53          // Ensure that we came from view.php
  54          if (!confirm_sesskey() or !data_submitted()) {
  55              print_error('invalidformdata');
  56          }
  57          break;
  58  
  59      case 'save':
  60          if (confirm_sesskey() and $form = data_submitted($CFG->wwwroot.'/mod/lesson/view.php')) {
  61              $name = trim(optional_param('name', '', PARAM_CLEAN));
  62  
  63              // Make sure it is not empty
  64              if (empty($name)) {
  65                  $lesson->add_message(get_string('missingname', 'lesson'));
  66                  $mode = 'add';
  67                  break;
  68              }
  69              // Check for censored words
  70              $filterwords = explode(',', get_string('censorbadwords'));
  71              foreach ($filterwords as $filterword) {
  72                  if (strstr($name, $filterword)) {
  73                      $lesson->add_message(get_string('namereject', 'lesson'));
  74                      $mode = 'add';
  75                      break;
  76                  }
  77              }
  78              // Bad word was found
  79              if ($mode == 'add') {
  80                  break;
  81              }
  82              $params = array ("lessonid" => $lesson->id, "userid" => $USER->id);
  83              if (!$grades = $DB->get_records_select('lesson_grades', "lessonid = :lessonid", $params, 'completed')) {
  84                  print_error('cannotfindfirstgrade', 'lesson');
  85              }
  86  
  87              if (!$newgrade = $DB->get_record_sql("SELECT *
  88                                                 FROM {lesson_grades}
  89                                                WHERE lessonid = :lessonid
  90                                                  AND userid = :userid
  91                                             ORDER BY completed DESC", $params, true)) {
  92                  print_error('cannotfindnewestgrade', 'lesson');
  93              }
  94  
  95              // Check for multiple submissions
  96              if ($DB->record_exists('lesson_high_scores', array('gradeid' => $newgrade->id))) {
  97                  print_error('onpostperpage', 'lesson');
  98              }
  99  
 100              // Find out if we need to delete any records
 101              if ($highscores = $DB->get_records_sql("SELECT h.*, g.grade
 102                                                   FROM {lesson_grades} g, {lesson_high_scores} h
 103                                                  WHERE h.gradeid = g.id
 104                                                  AND h.lessonid = :lessonid
 105                                                  ORDER BY g.grade DESC", $params)) {
 106                  // Only count unique scores in our total for max high scores
 107                  $uniquescores = array();
 108                  foreach ($highscores as $highscore) {
 109                      $uniquescores[$highscore->grade] = 1;
 110                  }
 111                  if (count($uniquescores) >= $lesson->maxhighscores) {
 112                      // Top scores list is full, might need to delete a score
 113                      $flag = true;
 114                      // See if the new score is already listed in the top scores list
 115                      // if it is listed, then dont need to delete any records
 116                      foreach ($highscores as $highscore) {
 117                          if ($newgrade->grade == $highscore->grade) {
 118                              $flag = false;
 119                          }
 120                      }
 121                      if ($flag) {
 122                          // Pushing out the lowest score (could be multiple records)
 123                          $lowscore = 0;
 124                          foreach ($highscores as $highscore) {
 125                              if (empty($lowscore) or $lowscore > $highscore->grade) {
 126                                  $lowscore = $highscore->grade;
 127                              }
 128                          }
 129                          // Now, delete all high scores with the low score
 130                          foreach ($highscores as $highscore) {
 131                              if ($highscore->grade == $lowscore) {
 132                                  $DB->delete_records('lesson_high_scores', array('id' => $highscore->id));
 133                              }
 134                          }
 135                      }
 136                  }
 137              }
 138  
 139              $newhighscore = new stdClass;
 140              $newhighscore->lessonid = $lesson->id;
 141              $newhighscore->userid = $USER->id;
 142              $newhighscore->gradeid = $newgrade->id;
 143              $newhighscore->nickname = $name;
 144  
 145              $newhighscore->id = $DB->insert_record('lesson_high_scores', $newhighscore);
 146  
 147              // Trigger highscore updated event.
 148              $event = \mod_lesson\event\highscore_added::create(array(
 149                  'objectid' => $newhighscore->id,
 150                  'context' => $context,
 151                  'courseid' => $course->id,
 152                  'other' => array(
 153                      'lessonid' => $lesson->id,
 154                      'nickname' => $newhighscore->nickname
 155                  )
 156              ));
 157              $event->trigger();
 158  
 159              $lesson->add_message(get_string('postsuccess', 'lesson'), 'notifysuccess');
 160              redirect("$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id&amp;link=1");
 161          } else {
 162              print_error('invalidformdata');
 163          }
 164          break;
 165  }
 166  
 167  // Trigger highscore viewed event.
 168  $event = \mod_lesson\event\highscores_viewed::create(array(
 169      'objectid' => $lesson->properties()->id,
 170      'context' => $context,
 171      'courseid' => $course->id
 172  ));
 173  $event->trigger();
 174  
 175  $lessonoutput = $PAGE->get_renderer('mod_lesson');
 176  echo $lessonoutput->header($lesson, $cm, 'highscores', false, null, get_string('viewhighscores', 'lesson'));
 177  
 178  switch ($mode) {
 179      case 'add':
 180          echo $lessonoutput->add_highscores_form($lesson);
 181          break;
 182      default:
 183          $params = array ("lessonid" => $lesson->id);
 184          if (!$grades = $DB->get_records_select("lesson_grades", "lessonid = :lessonid", $params, "completed")) {
 185              $grades = array();
 186          }
 187  
 188          echo $OUTPUT->heading(get_string("topscorestitle", "lesson", $lesson->maxhighscores), 4);
 189  
 190          if (!$highscores = $DB->get_records_select("lesson_high_scores", "lessonid = :lessonid", $params)) {
 191              echo $OUTPUT->heading(get_string("nohighscores", "lesson"), 3);
 192          } else {
 193              foreach ($highscores as $highscore) {
 194                  $grade = $grades[$highscore->gradeid]->grade;
 195                  $topscores[$grade][] = $highscore->nickname;
 196              }
 197              krsort($topscores);
 198  
 199              $table = new html_table();
 200              $table->align = array('center', 'left', 'right');
 201              $table->wrap = array();
 202              $table->width = "30%";
 203              $table->cellspacing = '10px';
 204              $table->size = array('*', '*', '*');
 205  
 206              $table->head = array(get_string("rank", "lesson"), get_string('name'), get_string("scores", "lesson"));
 207  
 208              $printed = 0;
 209              while (true) {
 210                  $temp = current($topscores);
 211                  $score = key($topscores);
 212                  $rank = $printed + 1;
 213                  sort($temp);
 214                  foreach ($temp as $student) {
 215                      $table->data[] = array($rank, $student, $score.'%');
 216                  }
 217                  $printed++;
 218                  if (!next($topscores) || !($printed < $lesson->maxhighscores)) {
 219                      break;
 220                  }
 221              }
 222              echo html_writer::table($table);
 223          }
 224  
 225          if (!has_capability('mod/lesson:manage', $context)) {  // teachers don't need the links
 226              echo $OUTPUT->box_start('mdl-align');
 227              echo $OUTPUT->box_start('lessonbutton standardbutton');
 228              if ($link) {
 229                  echo html_writer::link(new moodle_url('/course/view.php', array('id'=>$course->id)), get_string("returntocourse", "lesson"));
 230              } else {
 231                  echo html_writer::link(new moodle_url('/course/view.php', array('id'=>$course->id)), get_string("cancel", "lesson")). ' ';
 232                  echo html_writer::link(new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id, 'viewed'=>'1')), get_string("startlesson", "lesson"));
 233              }
 234              echo $OUTPUT->box_end();
 235              echo $OUTPUT->box_end();
 236          }
 237          break;
 238  }
 239  
 240  echo $lessonoutput->footer();


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