forked from nadavkav/moodle-block_sharing_cart
/
block_sharing_cart.php
138 lines (119 loc) · 3.69 KB
/
block_sharing_cart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* Sharing Cart block
*
* @author VERSION2, Inc.
* @version $Id: block_sharing_cart.php 948 2013-03-28 12:14:34Z malu $
*/
require_once __DIR__.'/classes/controller.php';
class block_sharing_cart extends block_base
{
public function init()
{
$this->title = get_string('pluginname', __CLASS__);
$this->version = 2015012700;
}
public function applicable_formats()
{
return array(
'site' => true,
'course' => true,
'course-category' => false,
'mod' => false,
'my' => false,
'tag' => false,
'admin' => false,
);
}
public function instance_can_be_docked()
{
return false; // AJAX won't work with Dock
}
public function has_config()
{
return true;
}
/**
* Get the block content
*
* @global object $CFG
* @global object $USER
* @return object|string
*/
public function get_content()
{
global $CFG, $USER;
if ($this->content !== null)
return $this->content;
if (!$this->page->user_is_editing())
return $this->content = '';
$context = context_course::instance($this->page->course->id);
if (!has_capability('moodle/backup:backupactivity', $context))
return $this->content = '';
$controller = new sharing_cart\controller();
$html = $controller->render_tree($USER->id);
if (moodle_major_version() >= 2.7) {
// Moodle 2.7 or later runs always with Ajax
} elseif (empty($CFG->enableajax)) {
$html = $this->get_content_noajax();
} else {
$noscript = html_writer::tag('noscript',
html_writer::tag('div', get_string('requirejs', __CLASS__), array('class' => 'error'))
);
$html = $noscript . $html;
}
$this->page->requires->css('/blocks/sharing_cart/styles.css');
$this->page->requires->js('/blocks/sharing_cart/module.js');
$this->page->requires->yui_module('block_sharing_cart', 'M.block_sharing_cart.init', array(), null, true);
$this->page->requires->strings_for_js(
array('yes', 'no', 'ok', 'cancel', 'error', 'edit', 'move', 'delete', 'movehere'),
'moodle'
);
$this->page->requires->strings_for_js(
array('copyhere', 'notarget', 'backup', 'restore', 'movedir', 'clipboard',
'confirm_backup', 'confirm_userdata', 'confirm_delete'),
__CLASS__
);
$footer = '<div style="display:none;">'
. '<div class="header-commands">' . $this->get_header() . '</div>'
. '</div>';
return $this->content = (object)array('text' => $html, 'footer' => $footer);
}
/**
* Get the block header
*
* @global core_renderer $OUTPUT
* @return string
*/
private function get_header()
{
global $OUTPUT;
// link to bulkdelete
$alt = get_string('bulkdelete', __CLASS__);
$src = $OUTPUT->pix_url('bulkdelete', __CLASS__);
$url = new moodle_url('/blocks/sharing_cart/bulkdelete.php', array('course' => $this->page->course->id));
$bulkdelete = '<a class="icon editing_bulkdelete" title="' . $alt . '" href="' . $url . '">'
. '<img src="' . $src . '" alt="' . $alt . '" />'
. '</a>';
// help for Sharing Cart
$helpicon = $OUTPUT->help_icon('sharing_cart', __CLASS__);
return $bulkdelete . $helpicon;
}
/**
* Get the block content for no-AJAX
*
* @global core_renderer $OUTPUT
* @return string
*/
private function get_content_noajax()
{
global $OUTPUT;
$html = '<div class="error">' . get_string('requireajax', __CLASS__) . '</div>';
if (has_capability('moodle/site:config', context_system::instance())) {
$url = new moodle_url('/admin/settings.php?section=ajax');
$link = '<a href="' . $url . '">' . get_string('ajaxuse') . '</a>';
$html .= '<div>' . $OUTPUT->rarrow() . ' ' . $link . '</div>';
}
return $html;
}
}