MDL-67613 availability_completion: add previous activity condition
[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);
c54b183c 64 $previouscm = false;
e01efa2c 65 foreach ($modinfo->cms as $id => $othercm) {
66 // Add each course-module if it has completion turned on and is not
67 // the one currently being edited.
2f6e0d92 68 if ($othercm->completion && (empty($cm) || $cm->id != $id) && !$othercm->deletioninprogress) {
49a84cec
AA
69 $cms[] = (object)array('id' => $id,
70 'name' => format_string($othercm->name, true, array('context' => $context)),
71 'completiongradeitemnumber' => $othercm->completiongradeitemnumber);
e01efa2c 72 }
c54b183c
FR
73 if (count($cms) && (empty($cm) || $cm->id == $id)) {
74 $previouscm = true;
75 }
76 }
77 if ($previouscm) {
78 $previous = (object)array('id' => \availability_completion\condition::OPTION_PREVIOUS,
79 'name' => get_string('option_previous', 'availability_completion'),
80 'completiongradeitemnumber' => \availability_completion\condition::OPTION_PREVIOUS);
81 array_unshift($cms, $previous);
e01efa2c 82 }
e01efa2c 83 $this->cachekey = $cachekey;
84 $this->cacheinitparams = array($cms);
85 }
86 return $this->cacheinitparams;
87 }
88
89 protected function allow_add($course, \cm_info $cm = null,
90 \section_info $section = null) {
91 global $CFG;
92
93 // Check if completion is enabled for the course.
94 require_once($CFG->libdir . '/completionlib.php');
95 $info = new \completion_info($course);
96 if (!$info->is_enabled()) {
97 return false;
98 }
99
100 // Check if there's at least one other module with completion info.
101 $params = $this->get_javascript_init_params($course, $cm, $section);
102 return ((array)$params[0]) != false;
103 }
104}