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.
   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   * This page lists public api for tool_moodlenet plugin.
  19   *
  20   * @package    tool_moodlenet
  21   * @copyright  2020 Peter Dias
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die;
  26  
  27  use \core_course\local\entity\activity_chooser_footer;
  28  
  29  /**
  30   * The default endpoint to MoodleNet.
  31   */
  32  define('MOODLENET_DEFAULT_ENDPOINT', "lms/moodle/search");
  33  
  34  /**
  35   * Generate the endpoint url to the user's moodlenet site.
  36   *
  37   * @param string $profileurl The user's moodlenet profile page
  38   * @param int $course The moodle course the mnet resource will be added to
  39   * @param int $section The section of the course will be added to. Defaults to the 0th element.
  40   * @return string the resulting endpoint
  41   * @throws moodle_exception
  42   */
  43  function generate_mnet_endpoint(string $profileurl, int $course, int $section = 0) {
  44      global $CFG;
  45      $urlportions = explode('@', $profileurl);
  46      $domain = end($urlportions);
  47      $parsedurl = parse_url($domain);
  48      $params = [
  49          'site' => $CFG->wwwroot,
  50          'course' => $course,
  51          'section' => $section
  52      ];
  53      $endpoint = new moodle_url(MOODLENET_DEFAULT_ENDPOINT, $params);
  54      return (isset($parsedurl['scheme']) ? $domain : "https://$domain")."/{$endpoint->out(false)}";
  55  }
  56  
  57  /**
  58   * Hooking function to build up the initial Activity Chooser footer information for MoodleNet
  59   *
  60   * @param int $courseid The course the user is currently in and wants to add resources to
  61   * @param int $sectionid The section the user is currently in and wants to add resources to
  62   * @return activity_chooser_footer
  63   * @throws dml_exception
  64   * @throws moodle_exception
  65   */
  66  function tool_moodlenet_custom_chooser_footer(int $courseid, int $sectionid): activity_chooser_footer {
  67      global $CFG, $USER, $OUTPUT;
  68      $defaultlink = get_config('tool_moodlenet', 'defaultmoodlenet');
  69      $enabled = get_config('tool_moodlenet', 'enablemoodlenet');
  70  
  71      $advanced = false;
  72      // We are in the MoodleNet lib. It is safe assume we have our own functions here.
  73      $mnetprofile = \tool_moodlenet\profile_manager::get_moodlenet_user_profile($USER->id);
  74      if ($mnetprofile !== null) {
  75          $advanced = $mnetprofile->get_domain() ?? false;
  76      }
  77  
  78      $defaultlink = generate_mnet_endpoint($defaultlink, $courseid, $sectionid);
  79      if ($advanced !== false) {
  80          $advanced = generate_mnet_endpoint($advanced, $courseid, $sectionid);
  81      }
  82  
  83      $renderedfooter = $OUTPUT->render_from_template('tool_moodlenet/chooser_footer', (object)[
  84          'enabled' => (bool)$enabled,
  85          'generic' => $defaultlink,
  86          'advanced' => $advanced,
  87          'courseID' => $courseid,
  88          'sectionID' => $sectionid,
  89          'img' => $OUTPUT->image_url('MoodleNet', 'tool_moodlenet')->out(false),
  90      ]);
  91  
  92      $renderedcarousel = $OUTPUT->render_from_template('tool_moodlenet/chooser_moodlenet', (object)[
  93          'buttonName' => get_config('tool_moodlenet', 'defaultmoodlenetname'),
  94          'generic' => $defaultlink,
  95          'courseID' => $courseid,
  96          'sectionID' => $sectionid,
  97          'img' => $OUTPUT->image_url('MoodleNet', 'tool_moodlenet')->out(false),
  98      ]);
  99      return new activity_chooser_footer(
 100          'tool_moodlenet/instance_form',
 101          $renderedfooter,
 102          $renderedcarousel
 103      );
 104  }