Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Page helper.
  19   *
  20   * @package    tool_lp
  21   * @copyright  2015 Frédéric Massart - FMCorz.net
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_lp;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use coding_exception;
  29  use context;
  30  use moodle_exception;
  31  use moodle_url;
  32  use core_user;
  33  use context_user;
  34  use context_course;
  35  use stdClass;
  36  
  37  /**
  38   * Page helper.
  39   *
  40   * @package    tool_lp
  41   * @copyright  2015 Frédéric Massart - FMCorz.net
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class page_helper {
  45  
  46      /**
  47       * Set-up a course page.
  48       *
  49       * Example:
  50       * list($title, $subtitle) = page_helper::setup_for_course($pagecontextid, $url, $course, $pagetitle);
  51       * echo $OUTPUT->heading($title);
  52       * echo $OUTPUT->heading($subtitle, 3);
  53       *
  54       * @param  moodle_url $url The current page.
  55       * @param  stdClass $course The course.
  56       * @param  string $subtitle The title of the subpage, if any.
  57       * @return array With the following:
  58       *               - Page title
  59       *               - Page sub title
  60       *               - Return URL (course competencies page)
  61       */
  62      public static function setup_for_course(moodle_url $url, $course, $subtitle = '') {
  63          global $PAGE;
  64  
  65          $context = context_course::instance($course->id);
  66  
  67          $PAGE->set_course($course);
  68  
  69          if (!empty($subtitle)) {
  70              $title = $subtitle;
  71          } else {
  72              $title = get_string('coursecompetencies', 'tool_lp');
  73          }
  74  
  75          $returnurl = new moodle_url('/admin/tool/lp/coursecompetencies.php', array('courseid' => $course->id));
  76  
  77          $heading = $context->get_context_name();
  78          $PAGE->set_pagelayout('incourse');
  79          $PAGE->set_url($url);
  80          $PAGE->set_title($title);
  81          $PAGE->set_heading($heading);
  82  
  83          if (!empty($subtitle)) {
  84              $PAGE->navbar->add(get_string('coursecompetencies', 'tool_lp'), $returnurl);
  85              // We're in a sub page without a specific template.
  86              $PAGE->navbar->add($subtitle, $url);
  87          }
  88  
  89          return array($title, $subtitle, $returnurl);
  90      }
  91  
  92      /**
  93       * Set-up a template page.
  94       *
  95       * Example:
  96       * list($title, $subtitle) = page_helper::setup_for_template($pagecontextid, $url, $template, $pagetitle);
  97       * echo $OUTPUT->heading($title);
  98       * echo $OUTPUT->heading($subtitle, 3);
  99       *
 100       * @param  int $pagecontextid The page context ID.
 101       * @param  moodle_url $url The current page.
 102       * @param  \core_competency\template $template The template, if any.
 103       * @param  string $subtitle The title of the subpage, if any.
 104       * @param  string $returntype The desired return page.
 105       * @return array With the following:
 106       *               - Page title
 107       *               - Page sub title
 108       *               - Return URL
 109       */
 110      public static function setup_for_template($pagecontextid, moodle_url $url, $template = null, $subtitle = '',
 111                                                $returntype = null) {
 112          global $PAGE, $SITE;
 113  
 114          $pagecontext = context::instance_by_id($pagecontextid);
 115          $context = $pagecontext;
 116          if (!empty($template)) {
 117              $context = $template->get_context();
 118          }
 119  
 120          $templatesurl = new moodle_url('/admin/tool/lp/learningplans.php', array('pagecontextid' => $pagecontextid));
 121          $templateurl = null;
 122          if ($template) {
 123              $templateurl = new moodle_url('/admin/tool/lp/templatecompetencies.php', [
 124                  'templateid' => $template->get('id'),
 125                  'pagecontextid' => $pagecontextid
 126              ]);
 127          }
 128  
 129          $returnurl = $templatesurl;
 130          if ($returntype != 'templates' && $templateurl) {
 131              $returnurl = $templateurl;
 132          }
 133  
 134          $PAGE->navigation->override_active_url($templatesurl);
 135          $PAGE->set_context($pagecontext);
 136  
 137          if (!empty($template)) {
 138              $title = format_string($template->get('shortname'), true, array('context' => $context));
 139          } else {
 140              $title = get_string('templates', 'tool_lp');
 141          }
 142  
 143          if ($pagecontext->contextlevel == CONTEXT_SYSTEM) {
 144              $heading = $SITE->fullname;
 145          } else if ($pagecontext->contextlevel == CONTEXT_COURSECAT) {
 146              $heading = $pagecontext->get_context_name();
 147          } else {
 148              throw new coding_exception('Unexpected context!');
 149          }
 150  
 151          $PAGE->set_pagelayout('admin');
 152          $PAGE->set_url($url);
 153          $PAGE->set_title($title);
 154          $PAGE->set_heading($heading);
 155  
 156          if (!empty($template)) {
 157              $PAGE->navbar->add($title, $templateurl);
 158              if (!empty($subtitle)) {
 159                  $PAGE->navbar->add($subtitle, $url);
 160              }
 161  
 162          } else if (!empty($subtitle)) {
 163              // We're in a sub page without a specific template.
 164              $PAGE->navbar->add($subtitle, $url);
 165          }
 166  
 167          return array($title, $subtitle, $returnurl);
 168      }
 169  
 170      /**
 171       * Set-up a plan page.
 172       *
 173       * Example:
 174       * list($title, $subtitle) = page_helper::setup_for_plan($url, $template, $pagetitle);
 175       * echo $OUTPUT->heading($title);
 176       * echo $OUTPUT->heading($subtitle, 3);
 177       *
 178       * @param  int $userid The user ID.
 179       * @param  moodle_url $url The current page.
 180       * @param  \core_competency\plan $plan The plan, if any.
 181       * @param  string $subtitle The title of the subpage, if any.
 182       * @param  string $returntype The desired return page.
 183       * @return array With the following:
 184       *               - Page title
 185       *               - Page sub title
 186       *               - Return URL (main plan page)
 187       */
 188      public static function setup_for_plan($userid, moodle_url $url, $plan = null, $subtitle = '', $returntype = null) {
 189          global $PAGE, $USER;
 190  
 191          // Check that the user is a valid user.
 192          $user = core_user::get_user($userid);
 193          if (!$user || !core_user::is_real_user($userid)) {
 194              throw new \moodle_exception('invaliduser', 'error');
 195          }
 196  
 197          $context = context_user::instance($user->id);
 198  
 199          $plansurl = new moodle_url('/admin/tool/lp/plans.php', array('userid' => $userid));
 200          $planurl = null;
 201          if ($plan) {
 202              $planurl = new moodle_url('/admin/tool/lp/plan.php', array('id' => $plan->get('id')));
 203          }
 204  
 205          $returnurl = $plansurl;
 206          if ($returntype != 'plans' && $planurl) {
 207              $returnurl = $planurl;
 208          }
 209  
 210          $PAGE->navigation->override_active_url($plansurl);
 211          $PAGE->set_context($context);
 212  
 213          // If not his own plan, we want to extend the navigation for the user.
 214          $iscurrentuser = ($USER->id == $user->id);
 215          if (!$iscurrentuser) {
 216              $PAGE->navigation->extend_for_user($user);
 217              $PAGE->navigation->set_userid_for_parent_checks($user->id);
 218          }
 219  
 220          if (!empty($plan)) {
 221              $title = format_string($plan->get('name'), true, array('context' => $context));
 222          } else {
 223              $title = get_string('learningplans', 'tool_lp');
 224          }
 225  
 226          $PAGE->set_pagelayout('standard');
 227          $PAGE->set_url($url);
 228          $PAGE->set_title($title);
 229          $PAGE->set_heading($title);
 230  
 231          if (!empty($plan)) {
 232              $PAGE->navbar->add($title, $planurl);
 233              if (!empty($subtitle)) {
 234                  $PAGE->navbar->add($subtitle, $url);
 235              }
 236          } else if (!empty($subtitle)) {
 237              // We're in a sub page without a specific plan.
 238              $PAGE->navbar->add($subtitle, $url);
 239          }
 240  
 241          return array($title, $subtitle, $returnurl);
 242      }
 243  
 244      /**
 245       * Set-up a user evidence page.
 246       *
 247       * Example:
 248       * list($title, $subtitle) = page_helper::setup_for_user_evidence($url, $template, $pagetitle);
 249       * echo $OUTPUT->heading($title);
 250       * echo $OUTPUT->heading($subtitle, 3);
 251       *
 252       * @param  int $userid The user ID.
 253       * @param  moodle_url $url The current page.
 254       * @param  \core_competency\user_evidence $evidence The user evidence, if any.
 255       * @param  string $subtitle The title of the subpage, if any.
 256       * @param  string $returntype The desired return page.
 257       * @return array With the following:
 258       *               - Page title
 259       *               - Page sub title
 260       *               - Return URL (main plan page)
 261       */
 262      public static function setup_for_user_evidence($userid, moodle_url $url, $evidence = null, $subtitle = '', $returntype = null) {
 263          global $PAGE, $USER;
 264  
 265          // Check that the user is a valid user.
 266          $user = core_user::get_user($userid);
 267          if (!$user || !core_user::is_real_user($userid)) {
 268              throw new \moodle_exception('invaliduser', 'error');
 269          }
 270  
 271          $context = context_user::instance($user->id);
 272  
 273          $evidencelisturl = new moodle_url('/admin/tool/lp/user_evidence_list.php', array('userid' => $userid));
 274          $evidenceurl = null;
 275          if ($evidence) {
 276              $evidenceurl = new moodle_url('/admin/tool/lp/user_evidence.php', array('id' => $evidence->get('id')));
 277          }
 278  
 279          $returnurl = $evidencelisturl;
 280          if ($returntype != 'list' && $evidenceurl) {
 281              $returnurl = $evidenceurl;
 282          }
 283  
 284          $PAGE->navigation->override_active_url($evidencelisturl);
 285          $PAGE->set_context($context);
 286  
 287          // If not his own evidence, we want to extend the navigation for the user.
 288          $iscurrentuser = ($USER->id == $user->id);
 289          if (!$iscurrentuser) {
 290              $PAGE->navigation->extend_for_user($user);
 291              $PAGE->navigation->set_userid_for_parent_checks($user->id);
 292          }
 293  
 294          if (!empty($evidence)) {
 295              $title = format_string($evidence->get('name'), true, array('context' => $context));
 296          } else {
 297              $title = get_string('userevidence', 'tool_lp');
 298          }
 299  
 300          $PAGE->set_pagelayout('standard');
 301          $PAGE->set_url($url);
 302          $PAGE->set_title($title);
 303          $PAGE->set_heading($title);
 304  
 305          if (!empty($evidence)) {
 306              $PAGE->navbar->add($title, $evidenceurl);
 307              if (!empty($subtitle)) {
 308                  $PAGE->navbar->add($subtitle, $url);
 309              }
 310          } else if (!empty($subtitle)) {
 311              // We're in a sub page without a specific evidence.
 312              $PAGE->navbar->add($subtitle, $url);
 313          }
 314  
 315          return array($title, $subtitle, $returnurl);
 316      }
 317  
 318      /**
 319       * Set-up a framework page.
 320       *
 321       * Example:
 322       * list($pagetitle, $pagesubtitle, $url, $frameworksurl) = page_helper::setup_for_framework($id, $pagecontextid);
 323       * echo $OUTPUT->heading($pagetitle);
 324       * echo $OUTPUT->heading($pagesubtitle, 3);
 325       *
 326       * @param  int $id The framework ID.
 327       * @param  int $pagecontextid The page context ID.
 328       * @param  \core_competency\competency_framework $framework The framework.
 329       * @param  string $returntype The desired return page.
 330       * @return array With the following:
 331       *               - Page title
 332       *               - Page sub title
 333       *               - Page URL
 334       *               - Page framework URL
 335       */
 336      public static function setup_for_framework($id, $pagecontextid, $framework = null, $returntype = null) {
 337          global $PAGE;
 338  
 339          // We keep the original context in the URLs, so that we remain in the same context.
 340          $url = new moodle_url("/admin/tool/lp/editcompetencyframework.php", array('id' => $id, 'pagecontextid' => $pagecontextid));
 341          if ($returntype) {
 342              $url->param('return', $returntype);
 343          }
 344          $frameworksurl = new moodle_url('/admin/tool/lp/competencyframeworks.php', array('pagecontextid' => $pagecontextid));
 345  
 346          $PAGE->navigation->override_active_url($frameworksurl);
 347          $title = get_string('competencies', 'core_competency');
 348          if (empty($id)) {
 349              $pagetitle = get_string('competencyframeworks', 'tool_lp');
 350              $pagesubtitle = get_string('addnewcompetencyframework', 'tool_lp');
 351  
 352              $url->remove_params(array('id'));
 353              $PAGE->navbar->add($pagesubtitle, $url);
 354          } else {
 355              $pagetitle = $framework->get('shortname');
 356              $pagesubtitle = get_string('editcompetencyframework', 'tool_lp');
 357              if ($returntype == 'competencies') {
 358                  $frameworksurl = new moodle_url('/admin/tool/lp/competencies.php', array(
 359                      'pagecontextid' => $pagecontextid,
 360                      'competencyframeworkid' => $id
 361                  ));
 362              } else {
 363                  $frameworksurl->param('competencyframeworkid', $id);
 364              }
 365  
 366              $PAGE->navbar->add($pagetitle, $frameworksurl);
 367              $PAGE->navbar->add($pagesubtitle, $url);
 368          }
 369  
 370          $PAGE->set_context(context::instance_by_id($pagecontextid));
 371          $PAGE->set_pagelayout('admin');
 372          $PAGE->set_url($url);
 373          $PAGE->set_title($title);
 374          $PAGE->set_heading($title);
 375          return array($pagetitle, $pagesubtitle, $url, $frameworksurl);
 376      }
 377  
 378      /**
 379       * Set-up a competency page.
 380       *
 381       * Example:
 382       * list($title, $subtitle) = page_helper::setup_for_competency($pagecontextid, $url, $competency, $pagetitle);
 383       * echo $OUTPUT->heading($title);
 384       * echo $OUTPUT->heading($subtitle, 3);
 385       *
 386       * @param  int $pagecontextid The page context ID.
 387       * @param  moodle_url $url The current page.
 388       * @param  \core_competency\competency_framework $framework The competency framework.
 389       * @param  \core_competency\competency $competency The competency, if any.
 390       * @param  \core_competency\competency $parent The parent competency, if any.
 391       * @return array With the following:
 392       *               - Page title
 393       *               - Page sub title
 394       *               - Return URL (main competencies page)
 395       * @throws coding_exception
 396       */
 397      public static function setup_for_competency($pagecontextid, moodle_url $url, $framework, $competency = null, $parent = null) {
 398          global $PAGE, $SITE;
 399  
 400          // Set page context.
 401          $pagecontext = context::instance_by_id($pagecontextid);
 402          $PAGE->set_context($pagecontext);
 403  
 404          // Set page heading.
 405          if ($pagecontext->contextlevel == CONTEXT_SYSTEM) {
 406              $heading = $SITE->fullname;
 407          } else if ($pagecontext->contextlevel == CONTEXT_COURSECAT) {
 408              $heading = $pagecontext->get_context_name();
 409          } else {
 410              throw new coding_exception('Unexpected context!');
 411          }
 412          $PAGE->set_heading($heading);
 413  
 414          // Set override active url.
 415          $frameworksurl = new moodle_url('/admin/tool/lp/competencyframeworks.php', ['pagecontextid' => $pagecontextid]);
 416          $PAGE->navigation->override_active_url($frameworksurl);
 417  
 418          // Set return url.
 419          $returnurloptions = [
 420              'competencyframeworkid' => $framework->get('id'),
 421              'pagecontextid' => $pagecontextid
 422          ];
 423          $returnurl = new moodle_url('/admin/tool/lp/competencies.php', $returnurloptions);
 424          $PAGE->navbar->add($framework->get('shortname'), $returnurl);
 425  
 426          // Set page layout.
 427          $PAGE->set_pagelayout('admin');
 428  
 429          if (empty($competency)) {
 430              // Add mode.
 431              $title = format_string($framework->get('shortname'), true, ['context' => $pagecontext]);
 432  
 433              // Set the sub-title for add mode.
 434              $level = $parent ? $parent->get_level() + 1 : 1;
 435              $subtitle = get_string('taxonomy_add_' . $framework->get_taxonomy($level), 'tool_lp');
 436  
 437          } else {
 438              // Edit mode.
 439              $title = format_string($competency->get('shortname'), true, ['context' => $competency->get_context()]);
 440  
 441              // Add competency name to breadcrumbs, if available.
 442              $PAGE->navbar->add($title);
 443  
 444              // Set the sub-title for edit mode.
 445              $subtitle = get_string('taxonomy_edit_' . $framework->get_taxonomy($competency->get_level()), 'tool_lp');
 446          }
 447  
 448          // Set page title.
 449          $PAGE->set_title($title);
 450  
 451          // Set page url.
 452          $PAGE->set_url($url);
 453  
 454          // Add editing mode link to breadcrumbs, if available.
 455          if (!empty($subtitle)) {
 456              $PAGE->navbar->add($subtitle, $url);
 457          }
 458  
 459          return [$title, $subtitle, $returnurl];
 460      }
 461  }