Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
// This file is part of Moodle - http://moodle.org/
4
//
5
// Moodle is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// Moodle is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
/**
19
 * Cluster
20
 *
21
 * @package mod_lesson
22
 * @copyright  2009 Sam Hemelryk
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 **/
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
 /** Start of Cluster page */
29
define("LESSON_PAGE_CLUSTER",   "30");
30
 
31
class lesson_page_type_cluster extends lesson_page {
32
 
33
    protected $type = lesson_page::TYPE_STRUCTURE;
34
    protected $typeidstring = 'cluster';
35
    protected $typeid = LESSON_PAGE_CLUSTER;
36
    protected $string = null;
37
    protected $jumpto = null;
38
 
39
    public function display($renderer, $attempt) {
40
        return '';
41
    }
42
 
43
    public function get_typeid() {
44
        return $this->typeid;
45
    }
46
    public function get_typestring() {
47
        if ($this->string===null) {
48
            $this->string = get_string($this->typeidstring, 'lesson');
49
        }
50
        return $this->string;
51
    }
52
    public function get_idstring() {
53
        return $this->typeidstring;
54
    }
55
    public function get_grayout() {
56
        return 1;
57
    }
58
    public function callback_on_view($canmanage, $redirect = true) {
59
        global $USER;
60
        if (!$canmanage) {
61
            // Get the next page in the lesson cluster jump
62
            return (int) $this->lesson->cluster_jump($this->properties->id);
63
        } else {
64
            // get the next page
65
            return (int) $this->properties->nextpageid;
66
        }
67
    }
68
    public function override_next_page() {
69
        global $USER;
70
        return $this->lesson->cluster_jump($this->properties->id);
71
    }
72
    public function add_page_link($previd) {
73
        global $PAGE, $CFG;
74
        $addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'sesskey'=>sesskey(), 'qtype'=>LESSON_PAGE_CLUSTER));
75
        return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_CLUSTER, 'name'=>get_string('addcluster', 'lesson'));
76
    }
77
    public function valid_page_and_view(&$validpages, &$pageviews) {
78
        $validpages[$this->properties->id] = 1;  // add the cluster page as a valid page
79
        foreach ($this->lesson->get_sub_pages_of($this->properties->id, array(LESSON_PAGE_ENDOFCLUSTER)) as $subpage) {
80
            if (in_array($subpage->id, $pageviews)) {
81
                unset($pageviews[array_search($subpage->id, $pageviews)]);  // remove it
82
                // since the user did see one page in the cluster, add the cluster pageid to the viewedpageids
83
                if (!in_array($this->properties->id, $pageviews)) {
84
                    $pageviews[] = $this->properties->id;
85
                }
86
            }
87
        }
88
        return $this->properties->nextpageid;
89
    }
90
 
91
    /**
92
     * Creates answers within the database for this cluster page. Usually only ever
93
     * called when creating a new page instance.
94
     * @param object $properties
95
     * @return array
96
     */
97
    public function create_answers($properties) {
98
        global $DB;
99
 
100
        $newanswer = new stdClass;
101
        $newanswer->lessonid = $this->lesson->id;
102
        $newanswer->pageid = $this->properties->id;
103
        $newanswer->timecreated = $this->properties->timecreated;
104
 
105
        if (isset($properties->jumpto[0])) {
106
            $newanswer->jumpto = $properties->jumpto[0];
107
        }
108
        $newanswer->id = $DB->insert_record('lesson_answers', $newanswer);
109
        $answers = [$newanswer->id => new lesson_page_answer($newanswer)];
110
        $this->answers = $answers;
111
        return $answers;
112
    }
113
}
114
 
115
class lesson_add_page_form_cluster extends lesson_add_page_form_base {
116
 
117
    public $qtype = LESSON_PAGE_CLUSTER;
118
    public $qtypestring = 'cluster';
119
    protected $standard = false;
120
 
121
    public function custom_definition() {
122
        global $PAGE, $CFG;
123
 
124
        $mform = $this->_form;
125
        $lesson = $this->_customdata['lesson'];
126
        $jumptooptions = lesson_page_type_branchtable::get_jumptooptions(optional_param('firstpage', false, PARAM_BOOL), $lesson);
127
 
128
        $mform->addElement('hidden', 'firstpage');
129
        $mform->setType('firstpage', PARAM_BOOL);
130
 
131
        $mform->addElement('hidden', 'qtype');
132
        $mform->setType('qtype', PARAM_TEXT);
133
 
134
        $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), ['size' => 70, 'maxlength' => 255]);
135
        $mform->addRule('title', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
136
        if (!empty($CFG->formatstringstriptags)) {
137
            $mform->setType('title', PARAM_TEXT);
138
        } else {
139
            $mform->setType('title', PARAM_CLEANHTML);
140
        }
141
 
142
        $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes);
143
        $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions);
144
        $mform->setType('contents_editor', PARAM_RAW);
145
 
146
        $this->add_jumpto(0);
147
    }
148
 
149
 
150
    public function construction_override($pageid, lesson $lesson) {
151
        global $PAGE, $CFG, $DB;
152
        require_sesskey();
153
 
154
        $timenow = time();
155
 
156
        if ($pageid == 0) {
157
            if ($lesson->has_pages()) {
158
                if (!$page = $DB->get_record("lesson_pages", array("prevpageid" => 0, "lessonid" => $lesson->id))) {
159
                    throw new \moodle_exception('cannotfindpagerecord', 'lesson');
160
                }
161
            } else {
162
                // This is the ONLY page
163
                $page = new stdClass;
164
                $page->id = 0;
165
            }
166
        } else {
167
            if (!$page = $DB->get_record("lesson_pages", array("id" => $pageid))) {
168
                throw new \moodle_exception('cannotfindpagerecord', 'lesson');
169
            }
170
        }
171
        $newpage = new stdClass;
172
        $newpage->lessonid = $lesson->id;
173
        $newpage->prevpageid = $pageid;
174
        if ($pageid != 0) {
175
            $newpage->nextpageid = $page->nextpageid;
176
        } else {
177
            $newpage->nextpageid = $page->id;
178
        }
179
        $newpage->qtype = $this->qtype;
180
        $newpage->timecreated = $timenow;
181
        $newpage->title = get_string("clustertitle", "lesson");
182
        $newpage->contents = get_string("clustertitle", "lesson");
183
        $newpageid = $DB->insert_record("lesson_pages", $newpage);
184
        // update the linked list...
185
        if ($pageid != 0) {
186
            $DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid));
187
        }
188
 
189
        if ($pageid == 0) {
190
            $page->nextpageid = $page->id;
191
        }
192
        if ($page->nextpageid) {
193
            // the new page is not the last page
194
            $DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->nextpageid));
195
        }
196
        // ..and the single "answer"
197
        $newanswer = new stdClass;
198
        $newanswer->lessonid = $lesson->id;
199
        $newanswer->pageid = $newpageid;
200
        $newanswer->timecreated = $timenow;
201
        $newanswer->jumpto = LESSON_CLUSTERJUMP;
202
        $newanswerid = $DB->insert_record("lesson_answers", $newanswer);
203
        $lesson->add_message(get_string('addedcluster', 'lesson'), 'notifysuccess');
204
        redirect($CFG->wwwroot.'/mod/lesson/edit.php?id='.$PAGE->cm->id);
205
    }
206
}