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
 * End of branch table
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
 /** End of Branch page */
29
define("LESSON_PAGE_ENDOFBRANCH",   "21");
30
 
31
class lesson_page_type_endofbranch extends lesson_page {
32
 
33
    protected $type = lesson_page::TYPE_STRUCTURE;
34
    protected $typeidstring = 'endofbranch';
35
    protected $typeid = LESSON_PAGE_ENDOFBRANCH;
36
    protected $string = null;
37
    protected $jumpto = null;
38
 
39
    public function display($renderer, $attempt) {
40
        return '';
41
    }
42
    public function get_typeid() {
43
        return $this->typeid;
44
    }
45
    public function get_typestring() {
46
        if ($this->string===null) {
47
            $this->string = get_string($this->typeidstring, 'lesson');
48
        }
49
        return $this->string;
50
    }
51
    public function get_idstring() {
52
        return $this->typeidstring;
53
    }
54
    public function callback_on_view($canmanage, $redirect = true) {
55
        return (int) $this->redirect_to_first_answer($canmanage, $redirect);
56
    }
57
 
58
    public function redirect_to_first_answer($canmanage, $redirect) {
59
        global $USER, $PAGE;
60
        $answers = $this->get_answers();
61
        $answer = array_shift($answers);
62
        $jumpto = $answer->jumpto;
63
        if ($jumpto == LESSON_RANDOMBRANCH) {
64
 
65
            $jumpto = lesson_unseen_branch_jump($this->lesson, $USER->id);
66
 
67
        } elseif ($jumpto == LESSON_CLUSTERJUMP) {
68
 
69
            if (!$canmanage) {
70
                $jumpto = $this->lesson->cluster_jump($this->properties->id);
71
            } else {
72
                if ($this->properties->nextpageid == 0) {
73
                    $jumpto = LESSON_EOL;
74
                } else {
75
                    $jumpto = $this->properties->nextpageid;
76
                }
77
            }
78
 
79
        } else if ($answer->jumpto == LESSON_NEXTPAGE) {
80
 
81
            if ($this->properties->nextpageid == 0) {
82
                $jumpto = LESSON_EOL;
83
            } else {
84
                $jumpto = $this->properties->nextpageid;
85
            }
86
 
87
        } else if ($jumpto == 0) {
88
 
89
            $jumpto = $this->properties->id;
90
 
91
        } else if ($jumpto == LESSON_PREVIOUSPAGE) {
92
 
93
            $jumpto = $this->properties->prevpageid;
94
 
95
        }
96
 
97
        if ($redirect) {
98
            redirect(new moodle_url('/mod/lesson/view.php', array('id' => $PAGE->cm->id, 'pageid' => $jumpto)));
99
            die;
100
        }
101
        return $jumpto;
102
    }
103
    public function get_grayout() {
104
        return 1;
105
    }
106
 
107
    public function add_page_link($previd) {
108
        global $PAGE, $CFG;
109
        if ($previd != 0) {
110
            $addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'sesskey'=>sesskey(), 'qtype'=>LESSON_PAGE_ENDOFBRANCH));
111
            return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_ENDOFBRANCH, 'name'=>get_string('addanendofbranch', 'lesson'));
112
        }
113
        return false;
114
    }
115
    public function valid_page_and_view(&$validpages, &$pageviews) {
116
        return $this->properties->nextpageid;
117
    }
118
 
119
    /**
120
     * Creates answers within the database for this end of cluster page. Usually only ever
121
     * called when creating a new page instance.
122
     * @param object $properties
123
     * @return array
124
     */
125
    public function create_answers($properties) {
126
        global $DB;
127
 
128
        $newanswer = new stdClass;
129
        $newanswer->lessonid = $this->lesson->id;
130
        $newanswer->pageid = $this->properties->id;
131
        $newanswer->timecreated = $this->properties->timecreated;
132
 
133
        if (isset($properties->jumpto[0])) {
134
            $newanswer->jumpto = $properties->jumpto[0];
135
        }
136
        $newanswer->id = $DB->insert_record('lesson_answers', $newanswer);
137
        $answers = [$newanswer->id => new lesson_page_answer($newanswer)];
138
        $this->answers = $answers;
139
        return $answers;
140
    }
141
}
142
 
143
class lesson_add_page_form_endofbranch extends lesson_add_page_form_base {
144
 
145
    public $qtype = LESSON_PAGE_ENDOFBRANCH;
146
    public $qtypestring = 'endofbranch';
147
    protected $standard = false;
148
 
149
    public function custom_definition() {
150
        global $PAGE, $CFG;
151
 
152
        $mform = $this->_form;
153
        $lesson = $this->_customdata['lesson'];
154
        $jumptooptions = lesson_page_type_branchtable::get_jumptooptions(optional_param('firstpage', false, PARAM_BOOL), $lesson);
155
 
156
        $mform->addElement('hidden', 'firstpage');
157
        $mform->setType('firstpage', PARAM_BOOL);
158
 
159
        $mform->addElement('hidden', 'qtype');
160
        $mform->setType('qtype', PARAM_TEXT);
161
 
162
        $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), ['size' => 70, 'maxlength' => 255]);
163
        $mform->addRule('title', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
164
        if (!empty($CFG->formatstringstriptags)) {
165
            $mform->setType('title', PARAM_TEXT);
166
        } else {
167
            $mform->setType('title', PARAM_CLEANHTML);
168
        }
169
 
170
        $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes);
171
        $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions);
172
        $mform->setType('contents_editor', PARAM_RAW);
173
 
174
        $this->add_jumpto(0);
175
    }
176
 
177
    public function construction_override($pageid, lesson $lesson) {
178
        global $DB, $CFG, $PAGE;
179
        require_sesskey();
180
 
181
        // first get the preceeding page
182
 
183
        $timenow = time();
184
 
185
        // the new page is not the first page (end of branch always comes after an existing page)
186
        if (!$page = $DB->get_record("lesson_pages", array("id" => $pageid))) {
187
            throw new \moodle_exception('cannotfindpagerecord', 'lesson');
188
        }
189
        // chain back up to find the (nearest branch table)
190
        $btpage = clone($page);
191
        $btpageid = $btpage->id;
192
        while (($btpage->qtype != LESSON_PAGE_BRANCHTABLE) && ($btpage->prevpageid > 0)) {
193
            $btpageid = $btpage->prevpageid;
194
            if (!$btpage = $DB->get_record("lesson_pages", array("id" => $btpageid))) {
195
                throw new \moodle_exception('cannotfindpagerecord', 'lesson');
196
            }
197
        }
198
 
199
        if ($btpage->qtype == LESSON_PAGE_BRANCHTABLE) {
200
            $newpage = new stdClass;
201
            $newpage->lessonid = $lesson->id;
202
            $newpage->prevpageid = $pageid;
203
            $newpage->nextpageid = $page->nextpageid;
204
            $newpage->qtype = $this->qtype;
205
            $newpage->timecreated = $timenow;
206
            $newpage->title = get_string("endofbranch", "lesson");
207
            $newpage->contents = get_string("endofbranch", "lesson");
208
            $newpageid = $DB->insert_record("lesson_pages", $newpage);
209
            // update the linked list...
210
            $DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid));
211
            if ($page->nextpageid) {
212
                // the new page is not the last page
213
                $DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->nextpageid));
214
            }
215
            // ..and the single "answer"
216
            $newanswer = new stdClass;
217
            $newanswer->lessonid = $lesson->id;
218
            $newanswer->pageid = $newpageid;
219
            $newanswer->timecreated = $timenow;
220
            $newanswer->jumpto = $btpageid;
221
            $newanswerid = $DB->insert_record("lesson_answers", $newanswer);
222
            $lesson->add_message(get_string('addedanendofbranch', 'lesson'), 'notifysuccess');
223
        } else {
224
            $lesson->add_message(get_string('nobranchtablefound', 'lesson'));
225
        }
226
 
227
        redirect($CFG->wwwroot."/mod/lesson/edit.php?id=".$PAGE->cm->id);
228
    }
229
}