| 1 | efrain | 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 file contains main class for the course format singleactivity
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 20 |  * @package    format_singleactivity
 | 
        
           |  |  | 21 |  * @copyright  2012 Marina Glancy
 | 
        
           |  |  | 22 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 23 |  */
 | 
        
           |  |  | 24 |   | 
        
           | 1441 | ariadna | 25 | use core_courseformat\sectiondelegate;
 | 
        
           |  |  | 26 |   | 
        
           | 1 | efrain | 27 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 28 | require_once($CFG->dirroot. '/course/format/lib.php');
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 | /**
 | 
        
           |  |  | 31 |  * Main class for the singleactivity course format
 | 
        
           |  |  | 32 |  *
 | 
        
           |  |  | 33 |  * @package    format_singleactivity
 | 
        
           |  |  | 34 |  * @copyright  2012 Marina Glancy
 | 
        
           |  |  | 35 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 36 |  */
 | 
        
           |  |  | 37 | class format_singleactivity extends core_courseformat\base {
 | 
        
           |  |  | 38 |     /** @var cm_info the current activity. Use get_activity() to retrieve it. */
 | 
        
           |  |  | 39 |     private $activity = false;
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 |     /** @var int The category ID guessed from the form data. */
 | 
        
           |  |  | 42 |     private $categoryid = false;
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 |     /**
 | 
        
           |  |  | 45 |      * The URL to use for the specified course
 | 
        
           |  |  | 46 |      *
 | 
        
           |  |  | 47 |      * @param int|stdClass $section Section object from database or just field course_sections.section
 | 
        
           |  |  | 48 |      *     if null the course view page is returned
 | 
        
           |  |  | 49 |      * @param array $options options for view URL. At the moment core uses:
 | 
        
           |  |  | 50 |      *     'navigation' (bool) ignored by this format
 | 
        
           |  |  | 51 |      *     'sr' (int) ignored by this format
 | 
        
           |  |  | 52 |      * @return null|moodle_url
 | 
        
           |  |  | 53 |      */
 | 
        
           |  |  | 54 |     public function get_view_url($section, $options = array()) {
 | 
        
           |  |  | 55 |         return new moodle_url('/course/view.php', ['id' => $this->courseid]);
 | 
        
           |  |  | 56 |     }
 | 
        
           |  |  | 57 |   | 
        
           |  |  | 58 |     /**
 | 
        
           |  |  | 59 |      * Loads all of the course sections into the navigation
 | 
        
           |  |  | 60 |      *
 | 
        
           |  |  | 61 |      * @param global_navigation $navigation
 | 
        
           |  |  | 62 |      * @param navigation_node $node The course node within the navigation
 | 
        
           |  |  | 63 |      */
 | 
        
           |  |  | 64 |     public function extend_course_navigation($navigation, navigation_node $node) {
 | 
        
           |  |  | 65 |         // Display orphaned activities for the users who can see them.
 | 
        
           |  |  | 66 |         $context = context_course::instance($this->courseid);
 | 
        
           |  |  | 67 |         if (has_capability('moodle/course:viewhiddensections', $context)) {
 | 
        
           |  |  | 68 |             $modinfo = get_fast_modinfo($this->courseid);
 | 
        
           |  |  | 69 |             if (!empty($modinfo->sections[1])) {
 | 
        
           |  |  | 70 |                 $section1 = $modinfo->get_section_info(1);
 | 
        
           |  |  | 71 |                 // Show orphaned activities.
 | 
        
           |  |  | 72 |                 $orphanednode = $node->add(get_string('orphaned', 'format_singleactivity'),
 | 
        
           |  |  | 73 |                         $this->get_view_url(1), navigation_node::TYPE_SECTION, null, $section1->id);
 | 
        
           |  |  | 74 |                 $orphanednode->nodetype = navigation_node::NODETYPE_BRANCH;
 | 
        
           |  |  | 75 |                 $orphanednode->add_class('orphaned');
 | 
        
           |  |  | 76 |                 foreach ($modinfo->sections[1] as $cmid) {
 | 
        
           |  |  | 77 |                     if (has_capability('moodle/course:viewhiddenactivities', context_module::instance($cmid))) {
 | 
        
           |  |  | 78 |                         $this->navigation_add_activity($orphanednode, $modinfo->cms[$cmid]);
 | 
        
           |  |  | 79 |                     }
 | 
        
           |  |  | 80 |                 }
 | 
        
           |  |  | 81 |             }
 | 
        
           |  |  | 82 |         }
 | 
        
           |  |  | 83 |     }
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 |     /**
 | 
        
           |  |  | 86 |      * Adds a course module to the navigation node
 | 
        
           |  |  | 87 |      *
 | 
        
           |  |  | 88 |      * This is basically copied from function global_navigation::load_section_activities()
 | 
        
           |  |  | 89 |      * because it is not accessible from outside.
 | 
        
           |  |  | 90 |      *
 | 
        
           |  |  | 91 |      * @param navigation_node $node
 | 
        
           |  |  | 92 |      * @param cm_info $cm
 | 
        
           |  |  | 93 |      * @return null|navigation_node
 | 
        
           |  |  | 94 |      */
 | 
        
           |  |  | 95 |     protected function navigation_add_activity(navigation_node $node, $cm) {
 | 
        
           |  |  | 96 |         if (!$cm->uservisible) {
 | 
        
           |  |  | 97 |             return null;
 | 
        
           |  |  | 98 |         }
 | 
        
           |  |  | 99 |         $action = $cm->url;
 | 
        
           |  |  | 100 |         if (!$action) {
 | 
        
           |  |  | 101 |             // Do not add to navigation activity without url (i.e. labels).
 | 
        
           |  |  | 102 |             return null;
 | 
        
           |  |  | 103 |         }
 | 
        
           |  |  | 104 |         $activityname = format_string($cm->name, true, array('context' => context_module::instance($cm->id)));
 | 
        
           |  |  | 105 |         if ($cm->icon) {
 | 
        
           |  |  | 106 |             $icon = new pix_icon($cm->icon, $cm->modfullname, $cm->iconcomponent);
 | 
        
           |  |  | 107 |         } else {
 | 
        
           |  |  | 108 |             $icon = new pix_icon('monologo', $cm->modfullname, $cm->modname);
 | 
        
           |  |  | 109 |         }
 | 
        
           |  |  | 110 |         $activitynode = $node->add($activityname, $action, navigation_node::TYPE_ACTIVITY, null, $cm->id, $icon);
 | 
        
           |  |  | 111 |         if (global_navigation::module_extends_navigation($cm->modname)) {
 | 
        
           |  |  | 112 |             $activitynode->nodetype = navigation_node::NODETYPE_BRANCH;
 | 
        
           |  |  | 113 |         } else {
 | 
        
           |  |  | 114 |             $activitynode->nodetype = navigation_node::NODETYPE_LEAF;
 | 
        
           |  |  | 115 |         }
 | 
        
           |  |  | 116 |         return $activitynode;
 | 
        
           |  |  | 117 |     }
 | 
        
           |  |  | 118 |   | 
        
           |  |  | 119 |     /**
 | 
        
           |  |  | 120 |      * Returns the list of blocks to be automatically added for the newly created course
 | 
        
           |  |  | 121 |      *
 | 
        
           |  |  | 122 |      * @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
 | 
        
           |  |  | 123 |      *     each of values is an array of block names (for left and right side columns)
 | 
        
           |  |  | 124 |      */
 | 
        
           |  |  | 125 |     public function get_default_blocks() {
 | 
        
           |  |  | 126 |         // No blocks for this format because course view page is not displayed anyway.
 | 
        
           |  |  | 127 |         return array(
 | 
        
           |  |  | 128 |             BLOCK_POS_LEFT => array(),
 | 
        
           |  |  | 129 |             BLOCK_POS_RIGHT => array()
 | 
        
           |  |  | 130 |         );
 | 
        
           |  |  | 131 |     }
 | 
        
           |  |  | 132 |   | 
        
           |  |  | 133 |     /**
 | 
        
           |  |  | 134 |      * Definitions of the additional options that this course format uses for course
 | 
        
           |  |  | 135 |      *
 | 
        
           |  |  | 136 |      * Singleactivity course format uses one option 'activitytype'
 | 
        
           |  |  | 137 |      *
 | 
        
           |  |  | 138 |      * @param bool $foreditform
 | 
        
           |  |  | 139 |      * @return array of options
 | 
        
           |  |  | 140 |      */
 | 
        
           |  |  | 141 |     public function course_format_options($foreditform = false) {
 | 
        
           |  |  | 142 |         static $courseformatoptions = false;
 | 
        
           |  |  | 143 |   | 
        
           |  |  | 144 |         $fetchtypes = $courseformatoptions === false;
 | 
        
           |  |  | 145 |         $fetchtypes = $fetchtypes || ($foreditform && !isset($courseformatoptions['activitytype']['label']));
 | 
        
           |  |  | 146 |   | 
        
           |  |  | 147 |         if ($fetchtypes) {
 | 
        
           |  |  | 148 |             $availabletypes = $this->get_supported_activities();
 | 
        
           |  |  | 149 |             if ($this->courseid) {
 | 
        
           |  |  | 150 |                 // The course exists. Test against the course.
 | 
        
           |  |  | 151 |                 $testcontext = context_course::instance($this->courseid);
 | 
        
           |  |  | 152 |             } else if ($this->categoryid) {
 | 
        
           |  |  | 153 |                 // The course does not exist yet, but we have a category ID that we can test against.
 | 
        
           |  |  | 154 |                 $testcontext = context_coursecat::instance($this->categoryid);
 | 
        
           |  |  | 155 |             } else {
 | 
        
           |  |  | 156 |                 // The course does not exist, and we somehow do not have a category. Test capabilities against the system context.
 | 
        
           |  |  | 157 |                 $testcontext = context_system::instance();
 | 
        
           |  |  | 158 |             }
 | 
        
           |  |  | 159 |             foreach (array_keys($availabletypes) as $activity) {
 | 
        
           |  |  | 160 |                 $capability = "mod/{$activity}:addinstance";
 | 
        
           |  |  | 161 |                 if (!has_capability($capability, $testcontext)) {
 | 
        
           | 11 | efrain | 162 |                     if (!$this->categoryid) {
 | 
        
           |  |  | 163 |                         unset($availabletypes[$activity]);
 | 
        
           |  |  | 164 |                     } else {
 | 
        
           |  |  | 165 |                         // We do not have a course yet, so we guess if the user will have the capability to add the activity after
 | 
        
           |  |  | 166 |                         // creating the course.
 | 
        
           |  |  | 167 |                         $categorycontext = \context_coursecat::instance($this->categoryid);
 | 
        
           |  |  | 168 |                         if (!guess_if_creator_will_have_course_capability($capability, $categorycontext)) {
 | 
        
           |  |  | 169 |                             unset($availabletypes[$activity]);
 | 
        
           |  |  | 170 |                         }
 | 
        
           |  |  | 171 |                     }
 | 
        
           | 1 | efrain | 172 |                 }
 | 
        
           |  |  | 173 |             }
 | 
        
           |  |  | 174 |         }
 | 
        
           |  |  | 175 |   | 
        
           |  |  | 176 |         if ($courseformatoptions === false) {
 | 
        
           |  |  | 177 |             $config = get_config('format_singleactivity');
 | 
        
           |  |  | 178 |             $courseformatoptions = array(
 | 
        
           |  |  | 179 |                 'activitytype' => array(
 | 
        
           |  |  | 180 |                     'default' => $config->activitytype,
 | 
        
           |  |  | 181 |                     'type' => PARAM_TEXT,
 | 
        
           |  |  | 182 |                 ),
 | 
        
           |  |  | 183 |             );
 | 
        
           |  |  | 184 |   | 
        
           |  |  | 185 |             if (!empty($availabletypes) && !isset($availabletypes[$config->activitytype])) {
 | 
        
           |  |  | 186 |                 $courseformatoptions['activitytype']['default'] = array_keys($availabletypes)[0];
 | 
        
           |  |  | 187 |             }
 | 
        
           |  |  | 188 |         }
 | 
        
           |  |  | 189 |   | 
        
           |  |  | 190 |         if ($foreditform && !isset($courseformatoptions['activitytype']['label'])) {
 | 
        
           |  |  | 191 |             $courseformatoptionsedit = array(
 | 
        
           |  |  | 192 |                 'activitytype' => array(
 | 
        
           |  |  | 193 |                     'label' => new lang_string('activitytype', 'format_singleactivity'),
 | 
        
           |  |  | 194 |                     'help' => 'activitytype',
 | 
        
           |  |  | 195 |                     'help_component' => 'format_singleactivity',
 | 
        
           |  |  | 196 |                     'element_type' => 'select',
 | 
        
           |  |  | 197 |                     'element_attributes' => array($availabletypes),
 | 
        
           |  |  | 198 |                 ),
 | 
        
           |  |  | 199 |             );
 | 
        
           |  |  | 200 |             $courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
 | 
        
           |  |  | 201 |         }
 | 
        
           |  |  | 202 |         return $courseformatoptions;
 | 
        
           |  |  | 203 |     }
 | 
        
           |  |  | 204 |   | 
        
           |  |  | 205 |     /**
 | 
        
           |  |  | 206 |      * Adds format options elements to the course/section edit form
 | 
        
           |  |  | 207 |      *
 | 
        
           |  |  | 208 |      * This function is called from {@link course_edit_form::definition_after_data()}
 | 
        
           |  |  | 209 |      *
 | 
        
           |  |  | 210 |      * Format singleactivity adds a warning when format of the course is about to be changed.
 | 
        
           |  |  | 211 |      *
 | 
        
           |  |  | 212 |      * @param MoodleQuickForm $mform form the elements are added to
 | 
        
           |  |  | 213 |      * @param bool $forsection 'true' if this is a section edit form, 'false' if this is course edit form
 | 
        
           |  |  | 214 |      * @return array array of references to the added form elements
 | 
        
           |  |  | 215 |      */
 | 
        
           |  |  | 216 |     public function create_edit_form_elements(&$mform, $forsection = false) {
 | 
        
           |  |  | 217 |         global $PAGE;
 | 
        
           |  |  | 218 |   | 
        
           |  |  | 219 |         if (!$this->course && $submitvalues = $mform->getSubmitValues()) {
 | 
        
           |  |  | 220 |             $this->categoryid = $submitvalues['category'];
 | 
        
           |  |  | 221 |         }
 | 
        
           |  |  | 222 |   | 
        
           |  |  | 223 |         $elements = parent::create_edit_form_elements($mform, $forsection);
 | 
        
           |  |  | 224 |         if (!$forsection && ($course = $PAGE->course) && !empty($course->format) &&
 | 
        
           |  |  | 225 |                 $course->format !== 'site' && $course->format !== 'singleactivity') {
 | 
        
           |  |  | 226 |             // This is the existing course in other format, display a warning.
 | 
        
           |  |  | 227 |             $element = $mform->addElement('static', '', '',
 | 
        
           |  |  | 228 |                     html_writer::tag('span', get_string('warningchangeformat', 'format_singleactivity'),
 | 
        
           |  |  | 229 |                             array('class' => 'error')));
 | 
        
           |  |  | 230 |             array_unshift($elements, $element);
 | 
        
           |  |  | 231 |         }
 | 
        
           |  |  | 232 |         return $elements;
 | 
        
           |  |  | 233 |     }
 | 
        
           |  |  | 234 |   | 
        
           |  |  | 235 |     /**
 | 
        
           |  |  | 236 |      * Make sure that current active activity is in section 0
 | 
        
           |  |  | 237 |      *
 | 
        
           |  |  | 238 |      * All other activities are moved to section 1 that will be displayed as 'Orphaned'.
 | 
        
           |  |  | 239 |      * It may be needed after the course format was changed or activitytype in
 | 
        
           |  |  | 240 |      * course settings has been changed.
 | 
        
           |  |  | 241 |      *
 | 
        
           |  |  | 242 |      * @return null|cm_info current activity
 | 
        
           |  |  | 243 |      */
 | 
        
           |  |  | 244 |     public function reorder_activities() {
 | 
        
           |  |  | 245 |         course_create_sections_if_missing($this->courseid, array(0, 1));
 | 
        
           |  |  | 246 |         foreach ($this->get_sections() as $sectionnum => $section) {
 | 
        
           |  |  | 247 |             if (($sectionnum && $section->visible) ||
 | 
        
           |  |  | 248 |                     (!$sectionnum && !$section->visible)) {
 | 
        
           |  |  | 249 |                 // Make sure that 0 section is visible and all others are hidden.
 | 
        
           |  |  | 250 |                 set_section_visible($this->courseid, $sectionnum, $sectionnum == 0);
 | 
        
           |  |  | 251 |             }
 | 
        
           |  |  | 252 |         }
 | 
        
           |  |  | 253 |         $modinfo = get_fast_modinfo($this->courseid);
 | 
        
           |  |  | 254 |   | 
        
           |  |  | 255 |         // Find the current activity (first activity with the specified type in all course activities).
 | 
        
           |  |  | 256 |         $activitytype = $this->get_activitytype();
 | 
        
           |  |  | 257 |         $activity = null;
 | 
        
           |  |  | 258 |         if (!empty($activitytype)) {
 | 
        
           |  |  | 259 |             foreach ($modinfo->sections as $sectionnum => $cmlist) {
 | 
        
           |  |  | 260 |                 foreach ($cmlist as $cmid) {
 | 
        
           |  |  | 261 |                     if ($modinfo->cms[$cmid]->modname === $activitytype) {
 | 
        
           |  |  | 262 |                         $activity = $modinfo->cms[$cmid];
 | 
        
           |  |  | 263 |                         break 2;
 | 
        
           |  |  | 264 |                     }
 | 
        
           |  |  | 265 |                 }
 | 
        
           |  |  | 266 |             }
 | 
        
           |  |  | 267 |         }
 | 
        
           |  |  | 268 |   | 
        
           |  |  | 269 |         // Make sure the current activity is in the 0-section.
 | 
        
           |  |  | 270 |         $changed = false;
 | 
        
           |  |  | 271 |         if ($activity && $activity->sectionnum != 0) {
 | 
        
           |  |  | 272 |             moveto_module($activity, $modinfo->get_section_info(0));
 | 
        
           |  |  | 273 |             $changed = true;
 | 
        
           |  |  | 274 |         }
 | 
        
           |  |  | 275 |         if ($activity && !$activity->visible) {
 | 
        
           |  |  | 276 |             set_coursemodule_visible($activity->id, 1);
 | 
        
           |  |  | 277 |             $changed = true;
 | 
        
           |  |  | 278 |         }
 | 
        
           |  |  | 279 |         if ($changed) {
 | 
        
           |  |  | 280 |             // Cache was reset so get modinfo again.
 | 
        
           |  |  | 281 |             $modinfo = get_fast_modinfo($this->courseid);
 | 
        
           |  |  | 282 |         }
 | 
        
           |  |  | 283 |   | 
        
           |  |  | 284 |         // Move all other activities into section 1 (the order must be kept).
 | 
        
           |  |  | 285 |         $hasvisibleactivities = false;
 | 
        
           |  |  | 286 |         $firstorphanedcm = null;
 | 
        
           |  |  | 287 |         foreach ($modinfo->sections as $sectionnum => $cmlist) {
 | 
        
           |  |  | 288 |             if ($sectionnum && !empty($cmlist) && $firstorphanedcm === null) {
 | 
        
           |  |  | 289 |                 $firstorphanedcm = reset($cmlist);
 | 
        
           |  |  | 290 |             }
 | 
        
           |  |  | 291 |             foreach ($cmlist as $cmid) {
 | 
        
           |  |  | 292 |                 if ($sectionnum > 1) {
 | 
        
           | 1441 | ariadna | 293 |                     // These module types cannot be moved from section 0.
 | 
        
           |  |  | 294 |                     if (!$modinfo->cms[$cmid]->is_of_type_that_can_display()) {
 | 
        
           |  |  | 295 |                         continue;
 | 
        
           |  |  | 296 |                     }
 | 
        
           | 1 | efrain | 297 |                     moveto_module($modinfo->get_cm($cmid), $modinfo->get_section_info(1));
 | 
        
           |  |  | 298 |                 } else if (!$hasvisibleactivities && $sectionnum == 1 && $modinfo->get_cm($cmid)->visible) {
 | 
        
           |  |  | 299 |                     $hasvisibleactivities = true;
 | 
        
           |  |  | 300 |                 }
 | 
        
           |  |  | 301 |             }
 | 
        
           |  |  | 302 |         }
 | 
        
           |  |  | 303 |         if (!empty($modinfo->sections[0])) {
 | 
        
           |  |  | 304 |             foreach ($modinfo->sections[0] as $cmid) {
 | 
        
           | 1441 | ariadna | 305 |                 // These module types cannot be moved from section 0.
 | 
        
           |  |  | 306 |                 if (!$modinfo->cms[$cmid]->is_of_type_that_can_display()) {
 | 
        
           |  |  | 307 |                     continue;
 | 
        
           |  |  | 308 |                 }
 | 
        
           | 1 | efrain | 309 |                 if (!$activity || $cmid != $activity->id) {
 | 
        
           |  |  | 310 |                     moveto_module($modinfo->get_cm($cmid), $modinfo->get_section_info(1), $firstorphanedcm);
 | 
        
           |  |  | 311 |                 }
 | 
        
           |  |  | 312 |             }
 | 
        
           |  |  | 313 |         }
 | 
        
           |  |  | 314 |         if ($hasvisibleactivities) {
 | 
        
           |  |  | 315 |             set_section_visible($this->courseid, 1, false);
 | 
        
           |  |  | 316 |         }
 | 
        
           |  |  | 317 |         return $activity;
 | 
        
           |  |  | 318 |     }
 | 
        
           |  |  | 319 |   | 
        
           |  |  | 320 |     /**
 | 
        
           |  |  | 321 |      * Returns the name of activity type used for this course
 | 
        
           |  |  | 322 |      *
 | 
        
           |  |  | 323 |      * @return string|null
 | 
        
           |  |  | 324 |      */
 | 
        
           |  |  | 325 |     protected function get_activitytype() {
 | 
        
           |  |  | 326 |         $options = $this->get_format_options();
 | 
        
           |  |  | 327 |         $availabletypes = $this->get_supported_activities();
 | 
        
           |  |  | 328 |         if (!empty($options['activitytype']) &&
 | 
        
           |  |  | 329 |                 array_key_exists($options['activitytype'], $availabletypes)) {
 | 
        
           |  |  | 330 |             return $options['activitytype'];
 | 
        
           |  |  | 331 |         } else {
 | 
        
           |  |  | 332 |             return null;
 | 
        
           |  |  | 333 |         }
 | 
        
           |  |  | 334 |     }
 | 
        
           |  |  | 335 |   | 
        
           |  |  | 336 |     /**
 | 
        
           |  |  | 337 |      * Returns the current activity if exists
 | 
        
           |  |  | 338 |      *
 | 
        
           |  |  | 339 |      * @return null|cm_info
 | 
        
           |  |  | 340 |      */
 | 
        
           |  |  | 341 |     protected function get_activity() {
 | 
        
           |  |  | 342 |         if ($this->activity === false) {
 | 
        
           |  |  | 343 |             $this->activity = $this->reorder_activities();
 | 
        
           |  |  | 344 |         }
 | 
        
           |  |  | 345 |         return $this->activity;
 | 
        
           |  |  | 346 |     }
 | 
        
           |  |  | 347 |   | 
        
           |  |  | 348 |     /**
 | 
        
           |  |  | 349 |      * Get the activities supported by the format.
 | 
        
           |  |  | 350 |      *
 | 
        
           | 1441 | ariadna | 351 |      * Here we ignore the modules that do not have a page of their own or need sections,
 | 
        
           |  |  | 352 |      * like the label or subsection.
 | 
        
           | 1 | efrain | 353 |      *
 | 
        
           |  |  | 354 |      * @return array array($module => $name of the module).
 | 
        
           |  |  | 355 |      */
 | 
        
           |  |  | 356 |     public static function get_supported_activities() {
 | 
        
           |  |  | 357 |         $availabletypes = get_module_types_names();
 | 
        
           |  |  | 358 |         foreach ($availabletypes as $module => $name) {
 | 
        
           |  |  | 359 |             if (plugin_supports('mod', $module, FEATURE_NO_VIEW_LINK, false)) {
 | 
        
           |  |  | 360 |                 unset($availabletypes[$module]);
 | 
        
           |  |  | 361 |             }
 | 
        
           | 1441 | ariadna | 362 |             if (sectiondelegate::has_delegate_class('mod_' . $module)) {
 | 
        
           |  |  | 363 |                 unset($availabletypes[$module]);
 | 
        
           |  |  | 364 |             }
 | 
        
           | 1 | efrain | 365 |         }
 | 
        
           |  |  | 366 |         return $availabletypes;
 | 
        
           |  |  | 367 |     }
 | 
        
           |  |  | 368 |   | 
        
           |  |  | 369 |     /**
 | 
        
           |  |  | 370 |      * Checks if the current user can add the activity of the specified type to this course.
 | 
        
           |  |  | 371 |      *
 | 
        
           |  |  | 372 |      * @return bool
 | 
        
           |  |  | 373 |      */
 | 
        
           |  |  | 374 |     protected function can_add_activity() {
 | 
        
           |  |  | 375 |         global $CFG;
 | 
        
           |  |  | 376 |         if (!($modname = $this->get_activitytype())) {
 | 
        
           |  |  | 377 |             return false;
 | 
        
           |  |  | 378 |         }
 | 
        
           |  |  | 379 |         if (!has_capability('moodle/course:manageactivities', context_course::instance($this->courseid))) {
 | 
        
           |  |  | 380 |             return false;
 | 
        
           |  |  | 381 |         }
 | 
        
           |  |  | 382 |         if (!course_allowed_module($this->get_course(), $modname)) {
 | 
        
           |  |  | 383 |             return false;
 | 
        
           |  |  | 384 |         }
 | 
        
           |  |  | 385 |         $libfile = "$CFG->dirroot/mod/$modname/lib.php";
 | 
        
           |  |  | 386 |         if (!file_exists($libfile)) {
 | 
        
           |  |  | 387 |             return null;
 | 
        
           |  |  | 388 |         }
 | 
        
           |  |  | 389 |         return true;
 | 
        
           |  |  | 390 |     }
 | 
        
           |  |  | 391 |   | 
        
           |  |  | 392 |     /**
 | 
        
           |  |  | 393 |      * Checks if the activity type has multiple items in the activity chooser.
 | 
        
           |  |  | 394 |      *
 | 
        
           |  |  | 395 |      * @return bool|null (null if the check is not possible)
 | 
        
           |  |  | 396 |      */
 | 
        
           |  |  | 397 |     public function activity_has_subtypes() {
 | 
        
           |  |  | 398 |         global $USER;
 | 
        
           |  |  | 399 |         if (!($modname = $this->get_activitytype())) {
 | 
        
           |  |  | 400 |             return null;
 | 
        
           |  |  | 401 |         }
 | 
        
           |  |  | 402 |         $contentitemservice = \core_course\local\factory\content_item_service_factory::get_content_item_service();
 | 
        
           |  |  | 403 |         $metadata = $contentitemservice->get_content_items_for_user_in_course($USER, $this->get_course());
 | 
        
           |  |  | 404 |   | 
        
           |  |  | 405 |         // If there are multiple items originating from this mod_xxx component, then it's deemed to have subtypes.
 | 
        
           |  |  | 406 |         // If there is only 1 item, but it's not a reference to the core content item for the module, then it's also deemed to
 | 
        
           |  |  | 407 |         // have subtypes.
 | 
        
           |  |  | 408 |         $count = 0;
 | 
        
           |  |  | 409 |         foreach ($metadata as $key => $moduledata) {
 | 
        
           |  |  | 410 |             if ('mod_'.$modname === $moduledata->componentname) {
 | 
        
           |  |  | 411 |                 $count ++;
 | 
        
           |  |  | 412 |             }
 | 
        
           |  |  | 413 |         }
 | 
        
           |  |  | 414 |         if ($count > 1) {
 | 
        
           |  |  | 415 |             return true;
 | 
        
           |  |  | 416 |         } else {
 | 
        
           |  |  | 417 |             // Get the single item.
 | 
        
           |  |  | 418 |             $itemmetadata = $metadata[array_search('mod_' . $modname, array_column($metadata, 'componentname'))];
 | 
        
           |  |  | 419 |             $urlbase = new \moodle_url('/course/mod.php', ['id' => $this->get_course()->id]);
 | 
        
           |  |  | 420 |             $referenceurl = new \moodle_url($urlbase, ['add' => $modname]);
 | 
        
           |  |  | 421 |             if ($referenceurl->out(false) != $itemmetadata->link) {
 | 
        
           |  |  | 422 |                 return true;
 | 
        
           |  |  | 423 |             }
 | 
        
           |  |  | 424 |         }
 | 
        
           |  |  | 425 |         return false;
 | 
        
           |  |  | 426 |     }
 | 
        
           |  |  | 427 |   | 
        
           |  |  | 428 |     /**
 | 
        
           |  |  | 429 |      * Allows course format to execute code on moodle_page::set_course()
 | 
        
           |  |  | 430 |      *
 | 
        
           |  |  | 431 |      * This function is executed before the output starts.
 | 
        
           |  |  | 432 |      *
 | 
        
           |  |  | 433 |      * If everything is configured correctly, user is redirected from the
 | 
        
           |  |  | 434 |      * default course view page to the activity view page.
 | 
        
           |  |  | 435 |      *
 | 
        
           |  |  | 436 |      * "Section 1" is the administrative page to manage orphaned activities
 | 
        
           |  |  | 437 |      *
 | 
        
           |  |  | 438 |      * If user is on course view page and there is no module added to the course
 | 
        
           |  |  | 439 |      * and the user has 'moodle/course:manageactivities' capability, redirect to create module
 | 
        
           |  |  | 440 |      * form.
 | 
        
           |  |  | 441 |      *
 | 
        
           |  |  | 442 |      * @param moodle_page $page instance of page calling set_course
 | 
        
           |  |  | 443 |      */
 | 
        
           |  |  | 444 |     public function page_set_course(moodle_page $page) {
 | 
        
           |  |  | 445 |         global $PAGE;
 | 
        
           |  |  | 446 |         $page->add_body_class('format-'. $this->get_format());
 | 
        
           |  |  | 447 |         if ($PAGE == $page && $page->has_set_url() &&
 | 
        
           |  |  | 448 |                 $page->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
 | 
        
           |  |  | 449 |             $edit = optional_param('edit', -1, PARAM_BOOL);
 | 
        
           |  |  | 450 |             if (($edit == 0 || $edit == 1) && confirm_sesskey()) {
 | 
        
           |  |  | 451 |                 // This is a request to turn editing mode on or off, do not redirect here, /course/view.php will do redirection.
 | 
        
           |  |  | 452 |                 return;
 | 
        
           |  |  | 453 |             }
 | 
        
           |  |  | 454 |             $cm = $this->get_activity();
 | 
        
           |  |  | 455 |             $cursection = optional_param('section', null, PARAM_INT);
 | 
        
           |  |  | 456 |             if (!empty($cursection) && has_capability('moodle/course:viewhiddensections',
 | 
        
           |  |  | 457 |                     context_course::instance($this->courseid))) {
 | 
        
           |  |  | 458 |                 // Display orphaned activities (course view page, section 1).
 | 
        
           |  |  | 459 |                 return;
 | 
        
           |  |  | 460 |             }
 | 
        
           |  |  | 461 |             if (!$this->get_activitytype()) {
 | 
        
           |  |  | 462 |                 if (has_capability('moodle/course:update', context_course::instance($this->courseid))) {
 | 
        
           |  |  | 463 |                     // Teacher is redirected to edit course page.
 | 
        
           |  |  | 464 |                     $url = new moodle_url('/course/edit.php', array('id' => $this->courseid));
 | 
        
           |  |  | 465 |                     redirect($url, get_string('erroractivitytype', 'format_singleactivity'));
 | 
        
           |  |  | 466 |                 } else {
 | 
        
           |  |  | 467 |                     // Student sees an empty course page.
 | 
        
           |  |  | 468 |                     return;
 | 
        
           |  |  | 469 |                 }
 | 
        
           |  |  | 470 |             }
 | 
        
           |  |  | 471 |             if ($cm === null) {
 | 
        
           |  |  | 472 |                 if ($this->can_add_activity()) {
 | 
        
           |  |  | 473 |                     // This is a user who has capability to create an activity.
 | 
        
           |  |  | 474 |                     if ($this->activity_has_subtypes()) {
 | 
        
           |  |  | 475 |                         // Activity has multiple items in the activity chooser, it can not be added automatically.
 | 
        
           |  |  | 476 |                         if (optional_param('addactivity', 0, PARAM_INT)) {
 | 
        
           |  |  | 477 |                             return;
 | 
        
           |  |  | 478 |                         } else {
 | 
        
           |  |  | 479 |                             $url = new moodle_url('/course/view.php', array('id' => $this->courseid, 'addactivity' => 1));
 | 
        
           |  |  | 480 |                             redirect($url);
 | 
        
           |  |  | 481 |                         }
 | 
        
           |  |  | 482 |                     }
 | 
        
           |  |  | 483 |                     // Redirect to the add activity form.
 | 
        
           |  |  | 484 |                     $url = new moodle_url('/course/mod.php', array('id' => $this->courseid,
 | 
        
           |  |  | 485 |                         'section' => 0, 'sesskey' => sesskey(), 'add' => $this->get_activitytype()));
 | 
        
           |  |  | 486 |                     redirect($url);
 | 
        
           |  |  | 487 |                 } else {
 | 
        
           |  |  | 488 |                     // Student views an empty course page.
 | 
        
           |  |  | 489 |                     return;
 | 
        
           |  |  | 490 |                 }
 | 
        
           |  |  | 491 |             } else if (!$cm->uservisible || !$cm->url) {
 | 
        
           |  |  | 492 |                 // Activity is set but not visible to current user or does not have url.
 | 
        
           |  |  | 493 |                 // Display course page (either empty or with availability restriction info).
 | 
        
           |  |  | 494 |                 return;
 | 
        
           |  |  | 495 |             } else {
 | 
        
           |  |  | 496 |                 // Everything is set up and accessible, redirect to the activity page!
 | 
        
           |  |  | 497 |                 redirect($cm->url);
 | 
        
           |  |  | 498 |             }
 | 
        
           |  |  | 499 |         }
 | 
        
           |  |  | 500 |     }
 | 
        
           |  |  | 501 |   | 
        
           |  |  | 502 |     /**
 | 
        
           |  |  | 503 |      * Allows course format to execute code on moodle_page::set_cm()
 | 
        
           |  |  | 504 |      *
 | 
        
           |  |  | 505 |      * If we are inside the main module for this course, remove extra node level
 | 
        
           |  |  | 506 |      * from navigation: substitute course node with activity node, move all children
 | 
        
           |  |  | 507 |      *
 | 
        
           |  |  | 508 |      * @param moodle_page $page instance of page calling set_cm
 | 
        
           |  |  | 509 |      */
 | 
        
           |  |  | 510 |     public function page_set_cm(moodle_page $page) {
 | 
        
           |  |  | 511 |         global $PAGE;
 | 
        
           |  |  | 512 |         parent::page_set_cm($page);
 | 
        
           |  |  | 513 |         if ($PAGE == $page && ($cm = $this->get_activity()) &&
 | 
        
           |  |  | 514 |                 $cm->uservisible &&
 | 
        
           |  |  | 515 |                 ($cm->id === $page->cm->id) &&
 | 
        
           |  |  | 516 |                 ($activitynode = $page->navigation->find($cm->id, navigation_node::TYPE_ACTIVITY)) &&
 | 
        
           |  |  | 517 |                 ($node = $page->navigation->find($page->course->id, navigation_node::TYPE_COURSE))) {
 | 
        
           |  |  | 518 |             // Substitute course node with activity node, move all children.
 | 
        
           |  |  | 519 |             $node->action = $activitynode->action;
 | 
        
           |  |  | 520 |             $node->type = $activitynode->type;
 | 
        
           |  |  | 521 |             $node->id = $activitynode->id;
 | 
        
           |  |  | 522 |             $node->key = $activitynode->key;
 | 
        
           |  |  | 523 |             $node->isactive = $node->isactive || $activitynode->isactive;
 | 
        
           |  |  | 524 |             $node->icon = null;
 | 
        
           |  |  | 525 |             if ($activitynode->children->count()) {
 | 
        
           |  |  | 526 |                 foreach ($activitynode->children as &$child) {
 | 
        
           |  |  | 527 |                     $child->remove();
 | 
        
           |  |  | 528 |                     $node->add_node($child);
 | 
        
           |  |  | 529 |                 }
 | 
        
           |  |  | 530 |             } else {
 | 
        
           |  |  | 531 |                 $node->search_for_active_node();
 | 
        
           |  |  | 532 |             }
 | 
        
           |  |  | 533 |             $activitynode->remove();
 | 
        
           |  |  | 534 |         }
 | 
        
           |  |  | 535 |     }
 | 
        
           |  |  | 536 |   | 
        
           |  |  | 537 |     /**
 | 
        
           |  |  | 538 |      * Returns true if the course has a front page.
 | 
        
           |  |  | 539 |      *
 | 
        
           |  |  | 540 |      * @return boolean false
 | 
        
           |  |  | 541 |      */
 | 
        
           |  |  | 542 |     public function has_view_page() {
 | 
        
           |  |  | 543 |         return false;
 | 
        
           |  |  | 544 |     }
 | 
        
           |  |  | 545 |   | 
        
           |  |  | 546 |     /**
 | 
        
           |  |  | 547 |      * Return the plugin configs for external functions.
 | 
        
           |  |  | 548 |      *
 | 
        
           |  |  | 549 |      * @return array the list of configuration settings
 | 
        
           |  |  | 550 |      * @since Moodle 3.5
 | 
        
           |  |  | 551 |      */
 | 
        
           |  |  | 552 |     public function get_config_for_external() {
 | 
        
           |  |  | 553 |         // Return everything (nothing to hide).
 | 
        
           |  |  | 554 |         return $this->get_format_options();
 | 
        
           |  |  | 555 |     }
 | 
        
           |  |  | 556 | }
 |