Project

General

Profile

Feature #4694 » student_publication_doctor.class.php

Laurent Opprecht, 22/06/2012 15:52

 
1
<?php
2

    
3
/**
4
 * Description of student_publication_doctor
5
 *
6
 * @copyright (c) 2012 University of Geneva
7
 * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
8
 * @author Laurent Opprecht <laurent@opprecht.info>
9
 */
10
class StudentPublicationDoctor
11
{
12

    
13
    /**
14
     * @return StudentPublicationDoctor
15
     */
16
    static function call()
17
    {
18
        return new self();
19
    }
20

    
21
    function __construct()
22
    {
23
        db::instance(); //forces autoload to register
24
    }
25

    
26
    function writeln($text = '')
27
    {
28
        echo $text . '<br/>';
29
    }
30

    
31
    function cure_folders_missing_name()
32
    {
33
        $this->writeln();
34
        $this->writeln(strtoupper(__FUNCTION__));
35
        $folder = new Entity\StudentPublication();
36
        $repo = Entity\StudentPublication::repository();
37
        $folders = $repo->findByFiletype('folder');
38
        foreach ($folders as $folder) {
39
            $title = $folder->get_title();
40
            if (empty($title)) {
41
                $url = $folder->get_url();
42
                $url = explode('/', $url);
43
                $title = end($url);
44
                if ($title) {
45
                    $folder->set_title($title);
46
                    $this->writeln("cure folder missing name new name: $title");
47
                }
48
            }
49
        }
50
        db::instance()->flush();
51
    }
52

    
53
    function cure_folders_starting_with_work()
54
    {
55
        $this->writeln();
56
        $this->writeln(strtoupper(__FUNCTION__));
57
        $folders = $this->get_folders_starting_with_work();
58
        foreach ($folders as $folder) {
59
            $old_url = $folder->get_url();
60
            $new_url = str_replace('work/', '/', $old_url);
61
            $folder->set_url($new_url);
62
            $this->writeln("folder starting with work old url: $old_url new url: $new_url");
63
        }
64
        db::instance()->flush();
65
    }
66

    
67
    function get_sub_folders()
68
    {
69
        $result = array();
70
        $folder = new Entity\StudentPublication();
71
        $repo = Entity\StudentPublication::repository();
72
        $folders = $repo->findByFiletype('folder');
73
        foreach ($folders as $folder) {
74
            $url = $folder->get_url();
75
            $url = trim($url, '/');
76
            $url = trim($url, 'work/');
77
            $is_sub_folder = strpos($url, '/');
78
            if ($is_sub_folder) {
79
                $result[] = $folder;
80
            }
81
        }
82
        return $result;
83
    }
84

    
85
    /**
86
     *
87
     * @param Entity\StudentPublication $pub 
88
     */
89
    function get_parents($pub)
90
    {
91
        $result = array();
92
        $url = $pub->get_url();
93
        $url = str_replace('work/', '', $url);
94
        $url = ltrim($url, '/');
95
        $repo = Entity\StudentPublication::repository();
96
        $parts = explode('/', $url);
97
        $path = '';
98
        foreach ($parts as $part) {
99
            $path .= '/' . $part;
100
            $folder = $repo->findOneByUrl($path);
101
            if ($folder->get_url() != $pub->get_url()) {
102
                $result[] = $folder;
103
            }
104
        }
105
        return $result;
106
    }
107

    
108
    function get_folders_starting_with_work()
109
    {
110
        $repo = Entity\StudentPublication::repository();
111
        $query = $repo->createQueryBuilder('f')->where("f.filetype='folder'")->andWhere("f.url LIKE 'work/%'");
112
        $result = db::instance()->em()->createQuery($query)->execute();
113
        return $result;
114
    }
115

    
116
    /**
117
     *
118
     * @param Entity\StudentPublication $folder 
119
     */
120
    function get_direct_children($folder)
121
    {
122
        $result = array();
123
        $repo = Entity\StudentPublication::repository();
124
        $url = $folder->get_url();
125
        $url = 'work' . $url;
126
        $query = $repo->createQueryBuilder('f')->where("f.filetype='file'")->andWhere("f.url LIKE '$url/%'");
127
        $items = db::instance()->em()->createQuery($query)->execute();
128
        foreach ($items as $item) {
129
            $child_url = $item->get_url();
130
            $dir = explode('/', $child_url);
131
            array_pop($dir);
132
            $dir = implode('/', $dir);
133
            if ($dir == $url) {
134
                $result[] = $item;
135
            }
136
        }
137
        return $result;
138
    }
139

    
140
    function cure_sub_folders()
141
    {
142
        $this->writeln();
143
        $this->writeln(strtoupper(__FUNCTION__));
144
        $this->cure_folders_missing_name();
145

    
146
        $folder = new Entity\StudentPublication();
147
        $repo = Entity\StudentPublication::repository();
148
        $folders = $this->get_sub_folders();
149
        foreach ($folders as $folder) {
150
            $course = @api_get_course_info_by_id($folder->get_c_id());
151
            $root = api_get_path(SYS_COURSE_PATH) . $course['path'] .'/';
152
            $parents = $this->get_parents($folder);
153
            $children = $this->get_direct_children($folder);
154

    
155
            //new title
156
            $new_title = '';
157
            foreach ($parents as $parent) {
158
                $new_title = $new_title ? $new_title . '_' . $parent->get_title() : $parent->get_title();
159
            }
160
            $folder->set_title($new_title);
161
            $this->writeln("New title: $new_title");
162

    
163
//            //new dir
164
//            $new_dir = '/w' . md5(uniqid());
165
//            while (is_readable($root . $new_dir)) {
166
//                $new_dir = '/w' . md5(unique_id());
167
//            }
168
//            mkdir($root . '/work'. $new_dir, 0777);
169
//            $folder->set_url($new_dir);
170
//            $this->writeln("New dir: $new_dir");
171

    
172
            //attach children
173
            foreach ($children as $child) {
174
//                $from = $root . $child->get_url();
175
//                $basename = basename($from);
176
//                $path = '/work'. $new_dir . '/' . $basename;
177
//                $to = $root . $path;
178
//                copy($from, $to);
179
//                $child->set_url($path);
180
                $child->set_parent_id($folder->get_id());
181
                $this->writeln("Attach child ($from) TO ($to)");
182
            }
183
        }
184
        db::instance()->flush();
185
    }
186

    
187
    function diagnose()
188
    {
189
        $this->diagnose_folders_missing_name();
190
        //$this->diagnose_folders_starting_with_work();
191
        $this->diagnose_sub_folders();
192
    }
193

    
194
    function cure()
195
    {
196
        //$this->cure_folders_starting_with_work();
197
        $this->cure_folders_missing_name();
198
        $this->cure_sub_folders();
199
        db::instance()->flush();
200
    }
201

    
202
    function diagnose_folders_missing_name()
203
    {
204
        $this->writeln();
205
        $this->writeln(strtoupper(__FUNCTION__));
206
        $folder = new Entity\StudentPublication();
207
        $repo = Entity\StudentPublication::repository();
208
        $folders = $repo->findByFiletype('folder');
209
        foreach ($folders as $folder) {
210
            $title = $folder->get_title();
211
            if (empty($title)) {
212
                $url = $folder->get_url();
213
                $this->writeln("Folder missing name: $url");
214
            }
215
        }
216
    }
217

    
218
    function diagnose_sub_folders()
219
    {
220
        $this->writeln();
221
        $this->writeln(strtoupper(__FUNCTION__));
222
        $folder = new Entity\StudentPublication();
223
        $repo = Entity\StudentPublication::repository();
224
        $folders = $this->get_sub_folders();
225
        foreach ($folders as $folder) {
226
            $url = $folder->get_url();
227
            $this->writeln("Subfolder: $url");
228
        }
229
    }
230

    
231
    function diagnose_folders_starting_with_work()
232
    {
233
        $this->writeln();
234
        $this->writeln(strtoupper(__FUNCTION__));
235
        $folders = $this->get_folders_starting_with_work();
236
        foreach ($folders as $folder) {
237
            $url = $folder->get_url();
238
            $this->writeln("folder starting with work: $url");
239
        }
240
    }
241

    
242
}
(2-2/2)