Skip to content

Commit 6a617f7

Browse files
authored
Merge pull request #8 from lushonline/7-enhancement-major-rework-to-be-more-class-orientated
7 enhancement major rework to be more class orientated
2 parents d9deba2 + bd0c031 commit 6a617f7

25 files changed

+1593
-259
lines changed

classes/instance.php

+500
Large diffs are not rendered by default.

classes/output/index.php

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
namespace mod_externalcontent\output;
17+
18+
use html_table;
19+
use html_writer;
20+
use mod_externalcontent\instance;
21+
use mod_externalcontent\plugin;
22+
use renderable;
23+
use renderer_base;
24+
use stdClass;
25+
26+
/**
27+
* Renderer for Index page.
28+
*
29+
* @package mod_externalcontent
30+
* @copyright 2019-2022 LushOnline
31+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32+
*/
33+
class index implements renderable {
34+
35+
/** @var stdClass */
36+
protected $course;
37+
38+
/** @var stdClass[] */
39+
protected $instances;
40+
41+
/**
42+
* Constructor for the index renderable.
43+
*
44+
* @param stdClass $course
45+
* @param instance[] List of external content instances
46+
*/
47+
public function __construct(stdClass $course, array $instances) {
48+
$this->course = $course;
49+
$this->instances = $instances;
50+
$this->usesections = course_format_uses_sections($this->course->format);
51+
}
52+
53+
/**
54+
* Get the table for the index page.
55+
*
56+
* @param renderer_base $output
57+
* @return html_table
58+
*/
59+
public function get_table(renderer_base $output): html_table {
60+
// Get strings.
61+
$strname = get_string('name');
62+
$strintro = get_string('moduleintro');
63+
64+
// Print the list of instances.
65+
$table = new html_table();
66+
$table->attributes['class'] = 'generaltable mod_index';
67+
68+
if ($this->usesections) {
69+
$strsectionname = get_string('sectionname', 'format_'.$this->course->format);
70+
$table->head = array ($strsectionname, $strname, $strintro);
71+
$table->align = array ('center', 'left', 'left');
72+
} else {
73+
$table->head = array ($strname, $strintro);
74+
$table->align = array ('left', 'left');
75+
}
76+
77+
$currentsection = '';
78+
foreach ($this->instances as $instance) {
79+
$currentsection = $this->add_instance_to_table($output, $table, $instance, $currentsection);
80+
}
81+
82+
return $table;
83+
}
84+
85+
/**
86+
* Add details of the external content instance to the table.
87+
*
88+
* @param renderer_base $output
89+
* @param html_table $table
90+
* @param instance $instance
91+
* @param string $currentsection
92+
* @return string The current section
93+
*/
94+
protected function add_instance_to_table(renderer_base $output, html_table $table,
95+
instance $instance, string $currentsection): string {
96+
global $DB;
97+
$cm = $instance->get_cm();
98+
if (!$cm->uservisible) {
99+
return $currentsection;
100+
}
101+
102+
$classes = [];
103+
if (!$cm->visible) {
104+
$classes = array('class' => 'dimmed');
105+
}
106+
$link = html_writer::link(
107+
$instance->get_view_url(),
108+
format_string($instance->get_name(), true),
109+
$classes);
110+
$intro = format_module_intro('externalcontent', $instance->get_instance_data(), $cm->id);
111+
112+
// Determine the current section name.
113+
$printsection = '';
114+
if ($this->usesections) {
115+
$cs = $DB->get_record('course_sections', array('course' => $this->course->id, 'section' => $cm->sectionnum));
116+
117+
if ($cs->name !== $currentsection) {
118+
if ($cm->section) {
119+
$printsection = get_section_name($this->course->id, $cs);
120+
}
121+
if ($currentsection !== '') {
122+
$table->data[] = 'hr';
123+
}
124+
$currentsection = $printsection;
125+
}
126+
$table->data[] = array($printsection, $link, $intro);
127+
} else {
128+
$table->data[] = array($link, $intro);
129+
}
130+
131+
return $currentsection;
132+
}
133+
134+
}

classes/output/mobile.php

+40-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
defined('MOODLE_INTERNAL') || die();
2828

29+
use mod_externalcontent\instance;
30+
2931
require_once($CFG->dirroot . '/mod/externalcontent/lib.php');
3032

3133
/**
@@ -48,19 +50,25 @@ class mobile {
4850
* @throws \moodle_exception
4951
*/
5052
public static function mobile_course_view($args) {
51-
global $OUTPUT, $DB;
53+
global $OUTPUT;
5254

5355
$args = (object) $args;
5456
$versionname = $args->appversioncode >= 3950 ? 'latest' : 'ionic3';
55-
$cm = get_coursemodule_from_id('externalcontent', $args->cmid);
57+
58+
$instance = instance::get_from_cmid($args->cmid);
59+
60+
if (!$instance) {
61+
return self::mobile_print_error(get_string('invalidaccessparameter'));
62+
}
63+
$cm = $instance->get_cm();
5664

5765
require_login($args->courseid, false, $cm, true, true);
5866

59-
$context = \context_module::instance($cm->id);
67+
$context = $instance->get_context();
6068
require_capability('mod/externalcontent:view', $context);
6169

62-
$externalcontent = $DB->get_record('externalcontent', array('id' => $cm->instance));
63-
$course = get_course($cm->course);
70+
$course = $instance->get_course();
71+
$externalcontent = $instance->get_instance_data();
6472

6573
// Mark the externalcontent as viewed.
6674
externalcontent_view($externalcontent, $course, $cm, $context);
@@ -94,4 +102,31 @@ public static function mobile_course_view($args) {
94102
'files' => [],
95103
];
96104
}
105+
106+
107+
/**
108+
* Returns the view for errors.
109+
*
110+
* @param string $error Error to display.
111+
* @param string $versionname The version name of the template to use
112+
* @return array HTML, javascript and otherdata
113+
*/
114+
protected static function mobile_print_error($error, $versionname = 'ionic3'): array {
115+
global $OUTPUT;
116+
117+
return [
118+
'templates' => [
119+
[
120+
'id' => 'main',
121+
'html' => $OUTPUT->render_from_template(
122+
'mod_externalcontent/mobile_view_error_'.$versionname,
123+
['error' => $error]
124+
),
125+
],
126+
],
127+
'javascript' => '',
128+
'otherdata' => '',
129+
'files' => '',
130+
];
131+
}
97132
}

classes/output/renderer.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
* Provides {@see \mod_externalcontent\output\renderer} class.
19+
*
20+
* @package mod_externalcontent
21+
* @copyright 2019-2022 LushOnline
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
namespace mod_externalcontent\output;
26+
27+
use core\notification;
28+
use html_table;
29+
use html_writer;
30+
use mod_externalcontent\instance;
31+
use plugin_renderer_base;
32+
33+
/**
34+
* Renderer for mod_externalcontent plugin.
35+
*
36+
* @package mod_externalcontent
37+
* @copyright 2019-2022 LushOnline
38+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39+
*/
40+
class renderer extends plugin_renderer_base {
41+
42+
/**
43+
* Render the index table.
44+
*
45+
* @param index $index
46+
* @return string
47+
*/
48+
protected function render_index(index $index): string {
49+
return html_writer::table($index->get_table($this));
50+
}
51+
52+
53+
/**
54+
* Render the view page.
55+
*
56+
* @param view_page $page
57+
* @return string
58+
*/
59+
public function render_view_page(view_page $page): string {
60+
return $this->render_from_template(
61+
'mod_externalcontent/view_page',
62+
$page->export_for_template($this)
63+
);
64+
}
65+
}

classes/output/view_page.php

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
namespace mod_externalcontent\output;
17+
18+
use mod_externalcontent\instance;
19+
use mod_externalcontent\plugin;
20+
use renderable;
21+
use renderer_base;
22+
use stdClass;
23+
use templatable;
24+
25+
/**
26+
* Renderer for Index page.
27+
*
28+
* @package mod_externalcontent
29+
* @copyright 2019-2022 LushOnline
30+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31+
*/
32+
class view_page implements renderable, templatable {
33+
34+
/** @var instance The instance being rendered */
35+
protected $instance;
36+
37+
/**
38+
* Constructor for the View Page.
39+
*
40+
* @param instance $instance
41+
*/
42+
public function __construct(instance $instance) {
43+
$this->instance = $instance;
44+
}
45+
46+
/**
47+
* Export the content required to render the template.
48+
*
49+
* @param renderer_base $output
50+
* @return stdClass
51+
*/
52+
public function export_for_template(renderer_base $output): stdClass {
53+
54+
$displayopions = empty($this->instance->get_instance_var('displayoptions')) ?
55+
array() :
56+
unserialize($this->instance->get_instance_var('displayoptions'));
57+
58+
$templatedata = (object) [
59+
'instanceid' => $this->instance->get_instance_id(),
60+
'name' => $this->instance->get_name(),
61+
'description' => $this->instance->get_description(),
62+
'content' => $this->instance->get_content(),
63+
'showlastmodified' => $displayopions['printlastmodified'] == 1,
64+
'lastmodified' => get_string("lastmodified").": ".userdate($this->instance->get_instance_var('timemodified'))
65+
];
66+
return $templatedata;
67+
}
68+
}

0 commit comments

Comments
 (0)