[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/lesson/ -> view.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   * This page prints a particular instance of lesson
  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 late
  24   **/
  25  
  26  require_once(__DIR__ . '/../../config.php');
  27  require_once($CFG->dirroot.'/mod/lesson/locallib.php');
  28  require_once($CFG->dirroot.'/mod/lesson/view_form.php');
  29  require_once($CFG->libdir . '/completionlib.php');
  30  require_once($CFG->libdir . '/grade/constants.php');
  31  
  32  $id      = required_param('id', PARAM_INT);             // Course Module ID
  33  $pageid  = optional_param('pageid', null, PARAM_INT);   // Lesson Page ID
  34  $edit    = optional_param('edit', -1, PARAM_BOOL);
  35  $userpassword = optional_param('userpassword','',PARAM_RAW);
  36  $backtocourse = optional_param('backtocourse', false, PARAM_RAW);
  37  
  38  $cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);
  39  $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  40  $lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
  41  
  42  require_login($course, false, $cm);
  43  
  44  if ($backtocourse) {
  45      redirect(new moodle_url('/course/view.php', array('id'=>$course->id)));
  46  }
  47  
  48  // Apply overrides.
  49  $lesson->update_effective_access($USER->id);
  50  
  51  // Mark as viewed
  52  $completion = new completion_info($course);
  53  $completion->set_module_viewed($cm);
  54  
  55  $url = new moodle_url('/mod/lesson/view.php', array('id'=>$id));
  56  if ($pageid !== null) {
  57      $url->param('pageid', $pageid);
  58  }
  59  $PAGE->set_url($url);
  60  
  61  $context = context_module::instance($cm->id);
  62  $canmanage = has_capability('mod/lesson:manage', $context);
  63  
  64  $lessonoutput = $PAGE->get_renderer('mod_lesson');
  65  
  66  $reviewmode = false;
  67  $userhasgrade = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id));
  68  if ($userhasgrade && !$lesson->retake) {
  69      $reviewmode = true;
  70  }
  71  
  72  /// Check these for students only TODO: Find a better method for doing this!
  73  ///     Check lesson availability
  74  ///     Check for password
  75  ///     Check dependencies
  76  if (!$canmanage) {
  77      if (!$lesson->is_accessible()) {  // Deadline restrictions
  78          echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('notavailable'));
  79          if ($lesson->deadline != 0 && time() > $lesson->deadline) {
  80              echo $lessonoutput->lesson_inaccessible(get_string('lessonclosed', 'lesson', userdate($lesson->deadline)));
  81          } else {
  82              echo $lessonoutput->lesson_inaccessible(get_string('lessonopen', 'lesson', userdate($lesson->available)));
  83          }
  84          echo $lessonoutput->footer();
  85          exit();
  86      } else if ($lesson->usepassword && empty($USER->lessonloggedin[$lesson->id])) { // Password protected lesson code
  87          $correctpass = false;
  88          if (!empty($userpassword) && (($lesson->password == md5(trim($userpassword))) || ($lesson->password == trim($userpassword)))) {
  89              require_sesskey();
  90              // with or without md5 for backward compatibility (MDL-11090)
  91              $correctpass = true;
  92              $USER->lessonloggedin[$lesson->id] = true;
  93  
  94          } else if (isset($lesson->extrapasswords)) {
  95  
  96              // Group overrides may have additional passwords.
  97              foreach ($lesson->extrapasswords as $password) {
  98                  if (strcmp($password, md5(trim($userpassword))) === 0 || strcmp($password, trim($userpassword)) === 0) {
  99                      require_sesskey();
 100                      $correctpass = true;
 101                      $USER->lessonloggedin[$lesson->id] = true;
 102                  }
 103              }
 104          }
 105          if (!$correctpass) {
 106              echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('passwordprotectedlesson', 'lesson', format_string($lesson->name)));
 107              echo $lessonoutput->login_prompt($lesson, $userpassword !== '');
 108              echo $lessonoutput->footer();
 109              exit();
 110          }
 111      } else if ($lesson->dependency) { // check for dependencies
 112          if ($dependentlesson = $DB->get_record('lesson', array('id' => $lesson->dependency))) {
 113              // lesson exists, so we can proceed
 114              $conditions = unserialize($lesson->conditions);
 115              // assume false for all
 116              $errors = array();
 117  
 118              // check for the timespent condition
 119              if ($conditions->timespent) {
 120                  $timespent = false;
 121                  if ($attempttimes = $DB->get_records('lesson_timer', array("userid"=>$USER->id, "lessonid"=>$dependentlesson->id))) {
 122                      // go through all the times and test to see if any of them satisfy the condition
 123                      foreach($attempttimes as $attempttime) {
 124                          $duration = $attempttime->lessontime - $attempttime->starttime;
 125                          if ($conditions->timespent < $duration/60) {
 126                              $timespent = true;
 127                          }
 128                      }
 129                  }
 130                  if (!$timespent) {
 131                      $errors[] = get_string('timespenterror', 'lesson', $conditions->timespent);
 132                  }
 133              }
 134  
 135              // check for the gradebetterthan condition
 136              if($conditions->gradebetterthan) {
 137                  $gradebetterthan = false;
 138                  if ($studentgrades = $DB->get_records('lesson_grades', array("userid"=>$USER->id, "lessonid"=>$dependentlesson->id))) {
 139                      // go through all the grades and test to see if any of them satisfy the condition
 140                      foreach($studentgrades as $studentgrade) {
 141                          if ($studentgrade->grade >= $conditions->gradebetterthan) {
 142                              $gradebetterthan = true;
 143                          }
 144                      }
 145                  }
 146                  if (!$gradebetterthan) {
 147                      $errors[] = get_string('gradebetterthanerror', 'lesson', $conditions->gradebetterthan);
 148                  }
 149              }
 150  
 151              // check for the completed condition
 152              if ($conditions->completed) {
 153                  if (!$DB->count_records('lesson_grades', array('userid'=>$USER->id, 'lessonid'=>$dependentlesson->id))) {
 154                      $errors[] = get_string('completederror', 'lesson');
 155                  }
 156              }
 157  
 158              if (!empty($errors)) {  // print out the errors if any
 159                  echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('completethefollowingconditions', 'lesson', format_string($lesson->name)));
 160                  echo $lessonoutput->dependancy_errors($dependentlesson, $errors);
 161                  echo $lessonoutput->footer();
 162                  exit();
 163              }
 164          }
 165      }
 166  }
 167  
 168      // this is called if a student leaves during a lesson
 169  if ($pageid == LESSON_UNSEENBRANCHPAGE) {
 170      $pageid = lesson_unseen_question_jump($lesson, $USER->id, $pageid);
 171  }
 172  
 173  // display individual pages and their sets of answers
 174  // if pageid is EOL then the end of the lesson has been reached
 175  // for flow, changed to simple echo for flow styles, michaelp, moved lesson name and page title down
 176  $attemptflag = false;
 177  if (empty($pageid)) {
 178      // make sure there are pages to view
 179      if (!$DB->get_field('lesson_pages', 'id', array('lessonid' => $lesson->id, 'prevpageid' => 0))) {
 180          if (!$canmanage) {
 181              $lesson->add_message(get_string('lessonnotready2', 'lesson')); // a nice message to the student
 182          } else {
 183              if (!$DB->count_records('lesson_pages', array('lessonid'=>$lesson->id))) {
 184                  redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id"); // no pages - redirect to add pages
 185              } else {
 186                  $lesson->add_message(get_string('lessonpagelinkingbroken', 'lesson'));  // ok, bad mojo
 187              }
 188          }
 189      }
 190  
 191      // if no pageid given see if the lesson has been started
 192      $retries = $DB->count_records('lesson_grades', array("lessonid" => $lesson->id, "userid" => $USER->id));
 193      if ($retries > 0) {
 194          $attemptflag = true;
 195      }
 196  
 197      if (isset($USER->modattempts[$lesson->id])) {
 198          unset($USER->modattempts[$lesson->id]);  // if no pageid, then student is NOT reviewing
 199      }
 200  
 201      // If there are any questions that have been answered correctly (or not) in this attempt.
 202      $allattempts = $lesson->get_attempts($retries);
 203      if (!empty($allattempts)) {
 204          $attempt = end($allattempts);
 205          $attemptpage = $lesson->load_page($attempt->pageid);
 206          $jumpto = $DB->get_field('lesson_answers', 'jumpto', array('id' => $attempt->answerid));
 207          // convert the jumpto to a proper page id
 208          if ($jumpto == 0) {
 209              // Check if a question has been incorrectly answered AND no more attempts at it are left.
 210              $nattempts = $lesson->get_attempts($attempt->retry, false, $attempt->pageid, $USER->id);
 211              if (count($nattempts) >= $lesson->maxattempts) {
 212                  $lastpageseen = $lesson->get_next_page($attemptpage->nextpageid);
 213              } else {
 214                  $lastpageseen = $attempt->pageid;
 215              }
 216          } elseif ($jumpto == LESSON_NEXTPAGE) {
 217              $lastpageseen = $lesson->get_next_page($attemptpage->nextpageid);
 218          } else if ($jumpto == LESSON_CLUSTERJUMP) {
 219              $lastpageseen = $lesson->cluster_jump($attempt->pageid);
 220          } else {
 221              $lastpageseen = $jumpto;
 222          }
 223      }
 224  
 225      if ($branchtables = $DB->get_records('lesson_branch', array("lessonid" => $lesson->id, "userid" => $USER->id, "retry" => $retries), 'timeseen DESC')) {
 226          // in here, user has viewed a branch table
 227          $lastbranchtable = current($branchtables);
 228          if (count($allattempts) > 0) {
 229              if ($lastbranchtable->timeseen > $attempt->timeseen) {
 230                  // This branch table was viewed more recently than the question page.
 231                  if (!empty($lastbranchtable->nextpageid)) {
 232                      $lastpageseen = $lastbranchtable->nextpageid;
 233                  } else {
 234                      // Next page ID did not exist prior to MDL-34006.
 235                      $lastpageseen = $lastbranchtable->pageid;
 236                  }
 237              }
 238          } else {
 239              // Has not answered any questions but has viewed a branch table.
 240              if (!empty($lastbranchtable->nextpageid)) {
 241                  $lastpageseen = $lastbranchtable->nextpageid;
 242              } else {
 243                  // Next page ID did not exist prior to MDL-34006.
 244                  $lastpageseen = $lastbranchtable->pageid;
 245              }
 246          }
 247      }
 248      // Check to see if end of lesson was reached.
 249      if ((isset($lastpageseen) && ($lastpageseen != LESSON_EOL))) {
 250          if (($DB->count_records('lesson_attempts', array('lessonid' => $lesson->id, 'userid' => $USER->id, 'retry' => $retries)) > 0)
 251                  || $DB->count_records('lesson_branch', array("lessonid" => $lesson->id, "userid" => $USER->id, "retry" => $retries)) > 0) {
 252  
 253              echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('leftduringtimedsession', 'lesson'));
 254              if ($lesson->timelimit) {
 255                  if ($lesson->retake) {
 256                      $continuelink = new single_button(new moodle_url('/mod/lesson/view.php',
 257                              array('id' => $cm->id, 'pageid' => $lesson->firstpageid, 'startlastseen' => 'no')),
 258                              get_string('continue', 'lesson'), 'get');
 259  
 260                      echo html_writer::div($lessonoutput->message(get_string('leftduringtimed', 'lesson'), $continuelink),
 261                              'center leftduring');
 262  
 263                  } else {
 264                      $courselink = new single_button(new moodle_url('/course/view.php',
 265                              array('id' => $PAGE->course->id)), get_string('returntocourse', 'lesson'), 'get');
 266  
 267                      echo html_writer::div($lessonoutput->message(get_string('leftduringtimednoretake', 'lesson'), $courselink),
 268                              'center leftduring');
 269                  }
 270              } else {
 271                  echo $lessonoutput->continue_links($lesson, $lastpageseen);
 272              }
 273              echo $lessonoutput->footer();
 274              exit();
 275          }
 276      }
 277  
 278      if ($attemptflag) {
 279          if (!$lesson->retake) {
 280              echo $lessonoutput->header($lesson, $cm, 'view', '', null, get_string("noretake", "lesson"));
 281              $courselink = new single_button(new moodle_url('/course/view.php', array('id'=>$PAGE->course->id)), get_string('returntocourse', 'lesson'), 'get');
 282              echo $lessonoutput->message(get_string("noretake", "lesson"), $courselink);
 283              echo $lessonoutput->footer();
 284              exit();
 285          }
 286      }
 287      // start at the first page
 288      if (!$pageid = $DB->get_field('lesson_pages', 'id', array('lessonid' => $lesson->id, 'prevpageid' => 0))) {
 289          echo $lessonoutput->header($lesson, $cm, 'view', '', null);
 290          // Lesson currently has no content. A message for display has been prepared and will be displayed by the header method
 291          // of the lesson renderer.
 292          echo $lessonoutput->footer();
 293          exit();
 294      }
 295      /// This is the code for starting a timed test
 296      if(!isset($USER->startlesson[$lesson->id]) && !$canmanage) {
 297          $lesson->start_timer();
 298      }
 299  }
 300  
 301  $currenttab = 'view';
 302  $extraeditbuttons = false;
 303  $lessonpageid = null;
 304  $timer = null;
 305  
 306  if ($pageid != LESSON_EOL) {
 307      /// This is the code updates the lessontime for a timed test
 308      $startlastseen = optional_param('startlastseen', '', PARAM_ALPHA);
 309  
 310      $page = $lesson->load_page($pageid);
 311      // Check if the page is of a special type and if so take any nessecary action
 312      $newpageid = $page->callback_on_view($canmanage);
 313      if (is_numeric($newpageid)) {
 314          $page = $lesson->load_page($newpageid);
 315      }
 316  
 317      // Trigger module viewed event.
 318      $event = \mod_lesson\event\course_module_viewed::create(array(
 319          'objectid' => $lesson->id,
 320          'context' => $context
 321      ));
 322      $event->add_record_snapshot('course_modules', $cm);
 323      $event->add_record_snapshot('course', $course);
 324      $event->trigger();
 325  
 326      // This is where several messages (usually warnings) are displayed
 327      // all of this is displayed above the actual page
 328  
 329      // check to see if the user can see the left menu
 330      if (!$canmanage) {
 331          $lesson->displayleft = lesson_displayleftif($lesson);
 332  
 333          $continue = ($startlastseen !== '');
 334          $restart  = ($continue && $startlastseen == 'yes');
 335          $timer = $lesson->update_timer($continue, $restart);
 336  
 337          if ($lesson->timelimit) {
 338              $timeleft = $timer->starttime + $lesson->timelimit - time();
 339              if ($timeleft <= 0) {
 340                  // Out of time
 341                  $lesson->add_message(get_string('eolstudentoutoftime', 'lesson'));
 342                  redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id,'pageid'=>LESSON_EOL, 'outoftime'=>'normal')));
 343                  die; // Shouldn't be reached, but make sure
 344              } else if ($timeleft < 60) {
 345                  // One minute warning
 346                  $lesson->add_message(get_string('studentoneminwarning', 'lesson'));
 347              }
 348          }
 349  
 350          if ($page->qtype == LESSON_PAGE_BRANCHTABLE && $lesson->minquestions) {
 351              // tell student how many questions they have seen, how many are required and their grade
 352              $ntries = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id));
 353              $gradeinfo = lesson_grade($lesson, $ntries);
 354              if ($gradeinfo->attempts) {
 355                  if ($gradeinfo->nquestions < $lesson->minquestions) {
 356                      $a = new stdClass;
 357                      $a->nquestions   = $gradeinfo->nquestions;
 358                      $a->minquestions = $lesson->minquestions;
 359                      $lesson->add_message(get_string('numberofpagesviewednotice', 'lesson', $a));
 360                  }
 361  
 362                  if (!$reviewmode && !$lesson->retake){
 363                      $lesson->add_message(get_string("numberofcorrectanswers", "lesson", $gradeinfo->earned), 'notify');
 364                      if ($lesson->grade != GRADE_TYPE_NONE) {
 365                          $a = new stdClass;
 366                          $a->grade = number_format($gradeinfo->grade * $lesson->grade / 100, 1);
 367                          $a->total = $lesson->grade;
 368                          $lesson->add_message(get_string('yourcurrentgradeisoutof', 'lesson', $a), 'notify');
 369                      }
 370                  }
 371              }
 372          }
 373      } else {
 374          $timer = null;
 375          if ($lesson->timelimit) {
 376              $lesson->add_message(get_string('teachertimerwarning', 'lesson'));
 377          }
 378          if (lesson_display_teacher_warning($lesson)) {
 379              // This is the warning msg for teachers to inform them that cluster
 380              // and unseen does not work while logged in as a teacher
 381              $warningvars = new stdClass();
 382              $warningvars->cluster = get_string('clusterjump', 'lesson');
 383              $warningvars->unseen = get_string('unseenpageinbranch', 'lesson');
 384              $lesson->add_message(get_string('teacherjumpwarning', 'lesson', $warningvars));
 385          }
 386      }
 387  
 388      $PAGE->set_subpage($page->id);
 389      $currenttab = 'view';
 390      $extraeditbuttons = true;
 391      $lessonpageid = $page->id;
 392      $extrapagetitle = $page->title;
 393  
 394      if (($edit != -1) && $PAGE->user_allowed_editing()) {
 395          $USER->editing = $edit;
 396      }
 397  
 398      if (is_array($page->answers) && count($page->answers)>0) {
 399          // this is for modattempts option.  Find the users previous answer to this page,
 400          //   and then display it below in answer processing
 401          if (isset($USER->modattempts[$lesson->id])) {
 402              $retries = $DB->count_records('lesson_grades', array("lessonid"=>$lesson->id, "userid"=>$USER->id));
 403              if (!$attempts = $lesson->get_attempts($retries-1, false, $page->id)) {
 404                  print_error('cannotfindpreattempt', 'lesson');
 405              }
 406              $attempt = end($attempts);
 407              $USER->modattempts[$lesson->id] = $attempt;
 408          } else {
 409              $attempt = false;
 410          }
 411          $lessoncontent = $lessonoutput->display_page($lesson, $page, $attempt);
 412      } else {
 413          $data = new stdClass;
 414          $data->id = $PAGE->cm->id;
 415          $data->pageid = $page->id;
 416          $data->newpageid = $lesson->get_next_page($page->nextpageid);
 417  
 418          $customdata = array(
 419              'title'     => $page->title,
 420              'contents'  => $page->get_contents()
 421          );
 422          $mform = new lesson_page_without_answers($CFG->wwwroot.'/mod/lesson/continue.php', $customdata);
 423          $mform->set_data($data);
 424          ob_start();
 425          $mform->display();
 426          $lessoncontent = ob_get_contents();
 427          ob_end_clean();
 428      }
 429  
 430      lesson_add_fake_blocks($PAGE, $cm, $lesson, $timer);
 431      echo $lessonoutput->header($lesson, $cm, $currenttab, $extraeditbuttons, $lessonpageid, $extrapagetitle);
 432      if ($attemptflag) {
 433          // We are using level 3 header because attempt heading is a sub-heading of lesson title (MDL-30911).
 434          echo $OUTPUT->heading(get_string('attempt', 'lesson', $retries), 3);
 435      }
 436      /// This calculates and prints the ongoing score
 437      if ($lesson->ongoing && !empty($pageid) && !$reviewmode) {
 438          echo $lessonoutput->ongoing_score($lesson);
 439      }
 440      if ($lesson->displayleft) {
 441          echo '<a name="maincontent" id="maincontent" title="' . get_string('anchortitle', 'lesson') . '"></a>';
 442      }
 443      echo $lessoncontent;
 444      echo $lessonoutput->progress_bar($lesson);
 445      echo $lessonoutput->footer();
 446  
 447  } else {
 448  
 449      $lessoncontent = '';
 450      // end of lesson reached work out grade
 451      // Used to check to see if the student ran out of time
 452      $outoftime = optional_param('outoftime', '', PARAM_ALPHA);
 453  
 454      $ntries = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id));
 455      if (isset($USER->modattempts[$lesson->id])) {
 456          $ntries--;  // need to look at the old attempts :)
 457      }
 458      $gradelesson = true;
 459      $gradeinfo = lesson_grade($lesson, $ntries);
 460      if ($lesson->custom && !$canmanage) {
 461          // Before we calculate the custom score make sure they answered the minimum
 462          // number of questions. We only need to do this for custom scoring as we can
 463          // not get the miniumum score the user should achieve. If we are not using
 464          // custom scoring (so all questions are valued as 1) then we simply check if
 465          // they answered more than the minimum questions, if not, we mark it out of the
 466          // number specified in the minimum questions setting - which is done in lesson_grade().
 467          // Get the number of answers given.
 468          if ($gradeinfo->nquestions < $lesson->minquestions) {
 469              $gradelesson = false;
 470              $a = new stdClass;
 471              $a->nquestions = $gradeinfo->nquestions;
 472              $a->minquestions = $lesson->minquestions;
 473              $lessoncontent .= $OUTPUT->box_start('generalbox boxaligncenter');
 474              $lesson->add_message(get_string('numberofpagesviewednotice', 'lesson', $a));
 475          }
 476      }
 477      if ($gradelesson) {
 478          // We are using level 3 header because the page title is a sub-heading of lesson title (MDL-30911).
 479          $lessoncontent .= $OUTPUT->heading(get_string("congratulations", "lesson"), 3);
 480          $lessoncontent .= $OUTPUT->box_start('generalbox boxaligncenter');
 481      }
 482      if (!$canmanage) {
 483          if ($gradelesson) {
 484              // Store this now before any modifications to pages viewed.
 485              $progressbar = $lessonoutput->progress_bar($lesson);
 486              // Update the clock / get time information for this user.
 487              $lesson->stop_timer();
 488  
 489              // Update completion state.
 490              $completion = new completion_info($course);
 491              if ($completion->is_enabled($cm) && $lesson->completionendreached) {
 492                  $completion->update_state($cm, COMPLETION_COMPLETE);
 493              }
 494  
 495              if ($lesson->completiontimespent > 0) {
 496                  $duration = $DB->get_field_sql(
 497                      "SELECT SUM(lessontime - starttime)
 498                                     FROM {lesson_timer}
 499                                    WHERE lessonid = :lessonid
 500                                      AND userid = :userid",
 501                      array('userid' => $USER->id, 'lessonid' => $lesson->id));
 502                  if (!$duration) {
 503                      $duration = 0;
 504                  }
 505  
 506                  // If student has not spend enough time in the lesson, display a message.
 507                  if ($duration < $lesson->completiontimespent) {
 508                      $a = new stdClass;
 509                      $a->timespent = format_time($duration);
 510                      $a->timerequired = format_time($lesson->completiontimespent);
 511                      $lessoncontent .= $lessonoutput->paragraph(get_string("notenoughtimespent", "lesson", $a), 'center');
 512                  }
 513              }
 514  
 515  
 516              if ($gradeinfo->attempts) {
 517                  if (!$lesson->custom) {
 518                      $lessoncontent .= $lessonoutput->paragraph(get_string("numberofpagesviewed", "lesson", $gradeinfo->nquestions), 'center');
 519                      if ($lesson->minquestions) {
 520                          if ($gradeinfo->nquestions < $lesson->minquestions) {
 521                              // print a warning and set nviewed to minquestions
 522                              $lessoncontent .= $lessonoutput->paragraph(get_string("youshouldview", "lesson", $lesson->minquestions), 'center');
 523                          }
 524                      }
 525                      $lessoncontent .= $lessonoutput->paragraph(get_string("numberofcorrectanswers", "lesson", $gradeinfo->earned), 'center');
 526                  }
 527                  $a = new stdClass;
 528                  $a->score = $gradeinfo->earned;
 529                  $a->grade = $gradeinfo->total;
 530                  if ($gradeinfo->nmanual) {
 531                      $a->tempmaxgrade = $gradeinfo->total - $gradeinfo->manualpoints;
 532                      $a->essayquestions = $gradeinfo->nmanual;
 533                      $lessoncontent .= $OUTPUT->box(get_string("displayscorewithessays", "lesson", $a), 'center');
 534                  } else {
 535                      $lessoncontent .= $OUTPUT->box(get_string("displayscorewithoutessays", "lesson", $a), 'center');
 536                  }
 537                  if ($lesson->grade != GRADE_TYPE_NONE) {
 538                      $a = new stdClass;
 539                      $a->grade = number_format($gradeinfo->grade * $lesson->grade / 100, 1);
 540                      $a->total = $lesson->grade;
 541                      $lessoncontent .= $lessonoutput->paragraph(get_string("yourcurrentgradeisoutof", "lesson", $a), 'center');
 542                  }
 543  
 544                  $grade = new stdClass();
 545                  $grade->lessonid = $lesson->id;
 546                  $grade->userid = $USER->id;
 547                  $grade->grade = $gradeinfo->grade;
 548                  $grade->completed = time();
 549                  if (isset($USER->modattempts[$lesson->id])) { // If reviewing, make sure update old grade record.
 550                      if (!$grades = $DB->get_records("lesson_grades",
 551                          array("lessonid" => $lesson->id, "userid" => $USER->id), "completed DESC", '*', 0, 1)) {
 552                          print_error('cannotfindgrade', 'lesson');
 553                      }
 554                      $oldgrade = array_shift($grades);
 555                      $grade->id = $oldgrade->id;
 556                      $DB->update_record("lesson_grades", $grade);
 557                  } else {
 558                      $newgradeid = $DB->insert_record("lesson_grades", $grade);
 559                  }
 560              } else {
 561                  if ($lesson->timelimit) {
 562                      if ($outoftime == 'normal') {
 563                          $grade = new stdClass();
 564                          $grade->lessonid = $lesson->id;
 565                          $grade->userid = $USER->id;
 566                          $grade->grade = 0;
 567                          $grade->completed = time();
 568                          $newgradeid = $DB->insert_record("lesson_grades", $grade);
 569                          $lessoncontent .= $lessonoutput->paragraph(get_string("eolstudentoutoftimenoanswers", "lesson"));
 570                      }
 571                  } else {
 572                      $lessoncontent .= $lessonoutput->paragraph(get_string("welldone", "lesson"));
 573                  }
 574              }
 575  
 576              // update central gradebook
 577              lesson_update_grades($lesson, $USER->id);
 578              $lessoncontent .= $progressbar;
 579          }
 580      } else {
 581          // display for teacher
 582          if ($lesson->grade != GRADE_TYPE_NONE) {
 583              $lessoncontent .= $lessonoutput->paragraph(get_string("displayofgrade", "lesson"), 'center');
 584          }
 585      }
 586      $lessoncontent .= $OUTPUT->box_end(); //End of Lesson button to Continue.
 587  
 588      if ($lesson->modattempts && !$canmanage) {
 589          // make sure if the student is reviewing, that he/she sees the same pages/page path that he/she saw the first time
 590          // look at the attempt records to find the first QUESTION page that the user answered, then use that page id
 591          // to pass to view again.  This is slick cause it wont call the empty($pageid) code
 592          // $ntries is decremented above
 593          if (!$attempts = $lesson->get_attempts($ntries)) {
 594              $attempts = array();
 595              $url = new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id));
 596          } else {
 597              $firstattempt = current($attempts);
 598              $pageid = $firstattempt->pageid;
 599              // IF the student wishes to review, need to know the last question page that the student answered.  This will help to make
 600              // sure that the student can leave the lesson via pushing the continue button.
 601              $lastattempt = end($attempts);
 602              $USER->modattempts[$lesson->id] = $lastattempt->pageid;
 603  
 604              $url = new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id, 'pageid'=>$pageid));
 605          }
 606          $lessoncontent .= html_writer::link($url, get_string('reviewlesson', 'lesson'), array('class' => 'centerpadded lessonbutton standardbutton'));
 607      } elseif ($lesson->modattempts && $canmanage) {
 608          $lessoncontent .= $lessonoutput->paragraph(get_string("modattemptsnoteacher", "lesson"), 'centerpadded');
 609      }
 610  
 611      if ($lesson->activitylink) {
 612          $lessoncontent .= $lesson->link_for_activitylink();
 613      }
 614  
 615      $url = new moodle_url('/course/view.php', array('id'=>$course->id));
 616      $lessoncontent .= html_writer::link($url, get_string('returnto', 'lesson', format_string($course->fullname, true)), array('class'=>'centerpadded lessonbutton standardbutton'));
 617  
 618      if (has_capability('gradereport/user:view', context_course::instance($course->id))
 619              && $course->showgrades && $lesson->grade != 0 && !$lesson->practice) {
 620          $url = new moodle_url('/grade/index.php', array('id' => $course->id));
 621          $lessoncontent .= html_writer::link($url, get_string('viewgrades', 'lesson'),
 622              array('class' => 'centerpadded lessonbutton standardbutton'));
 623      }
 624  
 625      lesson_add_fake_blocks($PAGE, $cm, $lesson, $timer);
 626      echo $lessonoutput->header($lesson, $cm, $currenttab, $extraeditbuttons, $lessonpageid, get_string("congratulations", "lesson"));
 627      echo $lessoncontent;
 628      echo $lessonoutput->footer();
 629  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1