MDL-44070 Conditional availability enhancements (3): conditions
[moodle.git] / availability / condition / completion / classes / frontend.php
CommitLineData
e01efa2c 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 * Front-end class.
19 *
20 * @package availability_completion
21 * @copyright 2014 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace availability_completion;
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Front-end class.
31 *
32 * @package availability_completion
33 * @copyright 2014 The Open University
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class frontend extends \core_availability\frontend {
37 /**
38 * @var array Cached init parameters
39 */
40 protected $cacheparams = array();
41
42 /**
43 * @var string IDs of course, cm, and section for cache (if any)
44 */
45 protected $cachekey = '';
46
47 protected function get_javascript_strings() {
48 return array('option_complete', 'option_fail', 'option_incomplete', 'option_pass',
49 'label_cm', 'label_completion');
50 }
51
52 protected function get_javascript_init_params($course, \cm_info $cm = null,
53 \section_info $section = null) {
54 // Use cached result if available. The cache is just because we call it
55 // twice (once from allow_add) so it's nice to avoid doing all the
56 // print_string calls twice.
57 $cachekey = $course->id . ',' . ($cm ? $cm->id : '') . ($section ? $section->id : '');
58 if ($cachekey !== $this->cachekey) {
59 // Get list of activities on course which have completion values,
60 // to fill the dropdown.
61 $context = \context_course::instance($course->id);
62 $cms = array();
63 $modinfo = get_fast_modinfo($course);
64 foreach ($modinfo->cms as $id => $othercm) {
65 // Add each course-module if it has completion turned on and is not
66 // the one currently being edited.
67 if ($othercm->completion && (empty($cm) || $cm->id != $id)) {
68 $cms[] = (object)array('id' => $id, 'name' =>
69 format_string($othercm->name, true, array('context' => $context)));
70 }
71 }
72
73 $this->cachekey = $cachekey;
74 $this->cacheinitparams = array($cms);
75 }
76 return $this->cacheinitparams;
77 }
78
79 protected function allow_add($course, \cm_info $cm = null,
80 \section_info $section = null) {
81 global $CFG;
82
83 // Check if completion is enabled for the course.
84 require_once($CFG->libdir . '/completionlib.php');
85 $info = new \completion_info($course);
86 if (!$info->is_enabled()) {
87 return false;
88 }
89
90 // Check if there's at least one other module with completion info.
91 $params = $this->get_javascript_init_params($course, $cm, $section);
92 return ((array)$params[0]) != false;
93 }
94}